Monday, July 14, 2014

Java Basic Conceptions

Difference between final, finally and finalize() in Java
final - constant declaration.
· Variable: cannot be changed once initialized
· Method:  cannot be overridden by a subclass.
· Class: cannot be subclassed.

finally - handles exception.
Used with a try/catch block, guarantees that a section of code will be executed.

finalize() - method helps in garbage collection. A method that is invoked before an object is destroyed by the garbage collector. Can be overridden to define custom behavior during garbage collection.

Static Class loading and Dynamic Class loading
Static Class Loading : Using new operator you load classes statically in java.
In static class loading NoClassDefFoundException can be thrown if the loaded class is not available in run time.
Dynamic Class Loading: This can be achieved by invoking the Class loader functions programmatically. Typically we use Class.forName(String className) to get the class first and then we will call the newInstance() method on the returned class to get the instance.

What is Java garbage collection?
It frees memory allocated to objects that are not being used by the program any more.
In Java the GC runs automatically, but you can also call it explicitly with System.gc() and try to force a major garbage collection.

Strong reference, weak reference, soft reference, phantom reference
Strong reference never be collected by GC.
Weak reference can be collected when there is no strong reference points to the same object.
Soft reference can be collected when there is not enough memory.
Phantom’s get method will always return null.

Difference between StringBuffer and StringBuilder 
Java String is immutable and Final.  StringBuffer creates an array of all the strings, copying them back to a string only when necessary.

StringBuffer is synchronize(thread safe)
StringBuilder is not, which makes StringBuilder fast.

Java Static keyword
The  static method or field is not tied to any particular object instance of that class.  So you can call that method or field without creating any object.

Interface and Abstract Class



Overloading vs. Overriding
Overloading: describe when two methods have the same name but differ in the type or number of arguments.
Overriding: when a method shares the same name and function signature as another method win its super class.

No comments:

Post a Comment