Tuesday, April 16, 2013

Blank final variable and static blank final variable


Blank final variable:-

The final variable that is not initialized during the time of declaration is called blank final variable. These variables need to be initialized either in intializer block or in Constructor.

Static blank final variable:-

The static final variable that is not initialized during the time of declaration. These variables need to be initialized in static initializer block. Static initializer block is the only place where a static blank final variable can be initialized.

public class Test {

       static final int varStatic;
       final int var1;
       final int var2;
       static
       {
              System.out.println("In static initializer block");
              varStatic = 0;
       }
       //Initializer block
       {
              System.out.println("In initializer block");
              var1 = 0;
       }
       Test()
       {
              System.out.println("In constructor");
              var2 = 0;
       }
       public static void main(String[] args)
       {
              System.out.println("In Main");
       }

}



Order of execution of Java class

Below is the order of execution of a java class

1)Static initializer block
2)main method
3)Initializer block
4)Constructor

Initializer block and Constructor will be executed only when we create an object.

public class Test {

       static
       {
              System.out.println("In static initializer block");
       }
       //Initializer block
       {
              System.out.println("In initializer block");
       }
       Test()
       {
              System.out.println("In constructor");
       }
       public static void main(String[] args)
       {
              System.out.println("In Main");
       }

}


O/P:-
In static initializer block
In Main

public class Test {

       static
       {
              System.out.println("In static initializer block");
       }
       //Initializer block
       {
              System.out.println("In initializer block");
       }
       Test()
       {
              System.out.println("In constructor");
       }
       public static void main(String[] args)
       {
              System.out.println("In Main");
              Test t = new Test();
       }
}

 O/P:-
In static initializer block
In Main
In initializer block
In constructor


Thursday, April 11, 2013

Difference between == and equals()


== operator compares the object reference and returns true if the reference of both the objects are same. i.e. Both are same object.
Integer i1 = new Integer(10);
Integer i2 = new Integer(10);
Integer i3 =  i1;
System.out.println(i1==i2);
System.out.println(i1==i3);

O/P:-
false
true

equals() method of Object class is same as == operator. i.e. it compares the reference. But we can override it to write our business logic and return TRUE or FALSE accordingly. For example String class and Integer class have overridden equals methods to return true if the value of objects are equal.
Integer i1 = new Integer(10);
Integer i2 = new Integer(10);
             
System.out.println(i1==i2);
System.out.println(i1.equals(i2));

O/P:-
false
true

Monday, April 8, 2013

Equal assignment operator vs clone()


Equal assignment operator

When we use assignment operator to assign one object to another a new object does not get created. Both the objects point to same reference. Only reference gets copied. Changes in any object will change the object reference and will be reflected for the other one. Comparison using (==) operator will return TRUE since both points to same reference. Needless to mention equal() also returns TRUE.
Default hashCode() of super class will return same result for both.

package com.deva;

public class Student implements Cloneable {
      
       private int age;
       private String name;
       private String dept;
       public String getDept() {
              return dept;
       }
       public void setDept(String dept) {
              this.dept = dept;
       }
       public int getAge() {
              return age;
       }
       public void setAge(int age) {
              this.age = age;
       }
       public String getName() {
              return name;
       }
       public void setName(String name) {
              this.name = name;
       }
      
       public static void main(String[] args) throws CloneNotSupportedException {
             
              Student student =new Student();
              student.age=25;
              student.name="deva";
              student.dept="CS";        
              Student student2 = student;
              student2.age = 35;
             
              System.out.println("Student1 detail "+student);
              System.out.println("Student2 detail "+student2);
              System.out.println("Student1 hashcode="+student.hashCode());
              System.out.println("Student2 hashcode="+student2.hashCode());
              System.out.println("== operator returns "+(student==student2));
              System.out.println("equals method returns "+(student.equals(student2)));         
                                 
       }
      
       @Override
       public String toString() {

              String s = "Age="+this.age+"  Name="+this.name+"  Dept="+this.dept;
              return s;
       }
}


O/P:-
Student1 detail Age=35  Name=deva  Dept=CS
Student2 detail Age=35  Name=deva  Dept=CS
Student1 hashcode=1523239031
Student2 hashcode=1523239031
== operator returns true
equals method returns true


Creation of Object using clone()

In case when we create an object using clone() method a separate object gets created with a different reference but with same properties. Changes to one object will not be reflected for other one since both the object has different reference. Comparison using (==) operator will return FALSE.
Default equals() method of parent class will also return FALSE and hashCode() of super class will return different result for both.

package com.deva;

public class Student implements Cloneable {
      
       private int age;
       private String name;
       private String dept;
       public String getDept() {
              return dept;
       }
       public void setDept(String dept) {
              this.dept = dept;
       }
       public int getAge() {
              return age;
       }
       public void setAge(int age) {
              this.age = age;
       }
       public String getName() {
              return name;
       }
       public void setName(String name) {
              this.name = name;
       }
      
       public static void main(String[] args) throws CloneNotSupportedException {
             
              Student student =new Student();
              student.age=25;
              student.name="deva";
              student.dept="CS";        
              Student student2 = (Student) student.clone();
              student2.age = 35;
             
              System.out.println("Student1 detail "+student);
              System.out.println("Student2 detail "+student2);
              System.out.println("Student1 hashcode="+student.hashCode());
              System.out.println("Student2 hashcode="+student2.hashCode());
              System.out.println("== operator returns "+(student==student2));
              System.out.println("equals method returns "+(student.equals(student2)));         
                                 
       }
      
       @Override
       public String toString() {

              String s = "Age="+this.age+"  Name="+this.name+"  Dept="+this.dept;
              return s;
       }
}


O/P:-
Student1 detail Age=25  Name=deva  Dept=CS
Student2 detail Age=35  Name=deva  Dept=CS
Student1 hashcode=925838130
Student2 hashcode=2042428395
== operator returns false
equals method returns false