Thursday, June 14, 2007

Java Points

Introduction
  • Java 1.5 or Java 5.0 or Tiger, all are same.
  • Java 1.0.2 was first official version.
Classes and Objects
  • A class is a blueprint for an object. It tells the virtual machine how to make an object of that particular type.
  • Each time an object is created in Java, it goes into an area of memory known as The Heap.
  • Java is pass by value; that is only bit pattern of a variable is passed. If it is a primitive then its exact value is passed, if it is a reference the bit pattern of the reference is copied.
  • Arrays are objects too.
  • A reference variable value is the bits representing a way to get to an object on the heap.
  • Java doesn't require you to acknowledge a return value.
  • You don't have to initialize the instance variable because they always have a default value.
  • Local variables do not get a default value.
  • The "==" operator is used to compare the bits in two variables.
  • When you cast from big to small, bits on the left side are cut off.
  • ArrayList can't hold primitives. But you can put a primitive in an ArrayList, as long as it's wrapped in primitive wrapper class. And as of Java 5.0, that wrapping happens automatically.
Inheritance
  • Instance variables cannot be overridden.
  • There's no such thing as a private class.
  • A non-public class can be subclassed only be classes in the same package as the class.
  • The thing that stops a class from being subclassed is the keyword modifier final.
  • A lot of classes in the Java API are final i.e. String class.
Method overriding
  • Arguments must be the same, and return types must be compatible.
  • Method can't be less accessible.
Polymorphism
  • Concrete classes are those that are specific enough to be instantiated.
  • An abstract class can have static members.
  • An abstract method has no body.
  • If you declare an abstract method, you MUST mark the class abstract as well. You can't have an abstract method in a non-abstract class.
  • A concrete class must implement all abstract methods.
  • The compiler checks the class of the reference variable, not the class of the actual object at the other end of the reference.
  • You can call a method on an object only if the class of the reference variable has that method.
  • An abstract class can have both abstract and non-abstract methods.
  • Multiple inheritance is not allowed in Java.
  • An interface is like a 100% pure abstract class. It defines only abstract methods.
  • All interface methods are implicitly public and abstract.
Constructors
  • Objects live on the heap, and method invocations and local variables live on the stack.
  • Local variables are also known as stack variables.
  • Method at the top of the stack is always the currently-running method for that stack.
  • If the local variable is a reference to an object, only the variable goes on the stack.
  • Instance variables live on the Heap, inside the object they belong to.
  • If an object has an instance variable declared as the non-primitive type, Java makes space within the object only for the reference variable but not the other object.
  • Java lets you declare a method with the same name as your class. That doesn't make it a constructor.
  • Constructors are not inherited.
  • Be sure you have a no-arg constructor.
  • All the constructors in an object's inheritance tree must run when you make a new object.
  • Even abstract classes have constructors.
  • The compiler-inserted call to super() is always a non-arg call. If the superclass has overloaded constructors, only the no-arg one is called.

0 comments: