Tuesday, 5 September 2017

10 Important point about final you should know


  1. Making a class final is double edged sword. Some JAVA programmer argue that Final class severely limits client ability to inherit and extends. While some JAVA programmer are in opinion that Final  class which is IMMUTABLE are the best way to design ROBUST system. It's trade-off, James's Gosling suggest that making a class final in java is for Security reason e.g String is justified 
  2. Only Final variable is accessible inside anonymous class in JAVA.
  3. FINAL is different than FINALLY keyword which is used on Exception handling in JAVA.
  4. Final should not be confused with FINALIZE method which is declared in Object Class and called before an Object is collected by JVM.
  5. All variable declared inside java interface are implicitly Final.
  6. FINAL and ABSTRACT are two opposite keyword and Final Class can not be abstract in JAVA.
  7. Making a collection reference variable Final means only reference can not be changed but you can add,remove or change object inside Collection. For e.g.
  8.   
                             private final List loans=new ArrayList();
                                list.add("Home Loan");   //valid
                                list.add("Personal Loan");  //valid
                             loans=new Vector();    // Not Valid
      8. Final Keyword improves performance. Not JVM can cache Final variable but also application can cache frequently use Final Variable.
      9. Final variables are safe to share in MULTI THREADED Environment without additional                     synchronization overhead.
      10. Final keyword allows JVM to optimized.



If you have any doubt or information on this. Please comment!

Monday, 29 May 2017

EXISTENCE OF PRIMITIVE DATA TYPE AND REQUIREMENT OF WRAPPER CLASS


EXISTENCE OF PRIMITIVE DATA TYPE

Primitive data type: Datatype or simply type defines the type of operation allowed on stored data. Primitive data type represent single item of data. They are built in nature i.e. when you define a datatype , range of data allowed, memory size,default value as well as operation allowed on that data is already get decided by the processor.

Datatype supported by JAVA: byte,short,int,long, float,double and char.

We all know that JAVA in not purely Object Oriented because it uses primitive datatype.

However JAVA also has WRAPPER CLASSES( a class representation of primitive datatype) that make JAVA as purely object oriented

So why JAVA support Primitive??

As per as I know, These of the following reason why java support primitive datatype:

1. PERFORMANCE:

down voteaccepted
In Joshua Bloch's Effective Java, Item 5: "Avoid creating unnecessary objects", he posts the following code example:
public static void main(String[] args) {
    Long sum = 0L; // uses Long, not long
    for(long i = 0; i <= Integer.MAX_VALUE; i++) {
        sum += i;
    }
    System.out.println(sum);
}
and it takes 43 seconds to run. Taking the Long into the primitive brings it down to 6.8 seconds... if that's any indication why we use primitives.

2.MEMORY USAGE: Datatype has fixed memory size and the highest memory size is for double that 64 bit. However Instances of an object on the Java heap don't just take up memory for their actual fields. Inevitably, they also require some "housekeeping" information, such as recording an object's class, ID and status flags such as whether the object is currently reachable, currently synchronization-locked etc

  • a normal object requires 8 bytes of "housekeeping" space; 
TO SAVE A HUGE AMOUNT OF MEMORY CHUNK WE NEED PRIMITIVE DATA TYPE.

3. NULL POINTER EXCEPTION(NPE):

Simple although one of the possible reason of choosing primitive, as If primitive datatype give ease of use to programmer as they need not to check that data is null or not they automatically get initialized with their default value .

Integer in = null;
...
...
int i = in; // NPE at runtime

3. EASE OF USE:


class TEST {

    public static void main(String[] args) {
        System.out.println(new Integer(5) == new Integer(5));
        System.out.println(new Integer(500) == new Integer(500));

        System.out.println(Integer.valueOf(5) == Integer.valueOf(5));
        System.out.println(Integer.valueOf(500) == Integer.valueOf(500));
    }
}
RESULT is:

false
false
true
false
WHY true for 3 and false for 4??
Because they are two different objects. The 256 integers closest to zero [-128; 127] are cached by the JVM, so they return the same object for those. Beyond that range, though, they aren't cached, so a new object is 

4. BACKWARD COMPATIBILITY:

Backward compatibility was the one of the issue why java does not removed primitive datatype after launching WRAPPER CLASSES 


WHY REQUIREMENT OF WRAPPER CLASS:

1. SO that null  value will be possible for data type.


2. To include primitive data type in COLLECTION.


3. To get type safety we use generics and generics need objects not primitives.


4. For polymorphism: 


 doSomething(Object object) can take any generic parameter since the attribute is of type Object i.e Superclass of all object. This cannot be achieved with primitive data type.






If you have any doubt or information on this. Please comment!



Monday, 22 May 2017

STATIC METHOD CALL FROM NULL REFERENCE

If I try to call static method from a null reference, Output will be a NullPointerException or it will run successfully?

public class Test{
       public static void testStatic(){
         System.out.println("Test Static method via Null Reference Call");
    
}

public static void main(String args[]){

        Test test =null;
        test.testStatic;

  }

}


Output: Test Static method via Null Reference Call


Wonder!! If  no memory is allocated, then how the method executed successfully. When I decompile the class file I found test.testStatic  is converted into Test.testStatic. As we all know static always belong to class not instance. No matter instance got initialize or not, once the class get compile all the static variable and method got initialize.


If I missed something or you find more information on this, please comment below :)









Wait, notify and notifyAll method in Object Class

We usually get  surprise  when these methods are present in  Object  class however they only used in  Threading  Concepts.   Wonder WHY?...