Valid XHTML 1.0!

String equality and identity.



Consider the following example. It shows how identity test is performed on java string objects.
The identity test, message1 == message2 is true as both object references are pointing to the same object.

1
 package com.witscale.scjp.examples;        // Package declaration
2
3
 public class Test {                        // class declaration.
4
5
   public static void main(String[] args) {
6
    String message1 = "Hello World!";
7
    String message2 = message1;
8
    boolean isIdentical = (message1 == message2);
9
    System.out.println(isIdentical);        // It will print true on console.
10
    }
11
 }


Consider another example. Here the string literals are used. In this case also, the identity test (==) will return true.
Even if it appears that two separate string objects are created at line 6 and 7, it is not so.
In java, string literals are created on pool of literals. Therefore, the code at line 6 will create new string object if same literal is not already present.
The line 7 will not create new string object, as the literal already exist on pool. Therefore, string reference message2 will point to the string object created at line 6. Thus, identity test (==) will yield true as both object references are pointing to the same object.



1
 package com.witscale.scjp.examples;        // Package declaration
2
3
 public class Test {                        // class declaration.
4
5
   public static void main(String[] args) {
6
    String message1="Hello World!";
7
    String message2="Hello World!";
8
    boolean isIdentical = (message1 == message2);
9
    System.out.println(isIdentical);        // It will print true on console.
10
    }
11
 }




Now consider the following code. Here a new string object is created on both lines 6 and 7. Therefore, isIdentical will yield false as both object references are pointing to the two different objects. Please note that new will create new object each time.



1
 package com.witscale.scjp.examples;        // Package declaration
2
3
 public class Test {                        // class declaration.
4
5
   public static void main(String[] args) {
6
    String message1= new String("Hello World!");   
7
    String message2= new String("Hello World!");
8
    boolean isIdentical = (message1 == message2);
9
    System.out.println(isIdentical);        // It will print false on console.
10
    boolean isEqual = message1.equals(message2);
9
    System.out.println(isEqual);           // It will print true on console.
10
    }
11
 }


Boolean class and equals().



Consider the following example about wrapper class 'Boolean'.
The identity test at line 8 fails even if both boolean are wrapping the same value. This is because line 6 and 7 creates two different objects. Therefore, == will return false even if their contents are same.
On the other hand, the equality test at line 9 returns true. This is because equals() method of java.lang.Object class is overridden in java.lang.Boolean class. The overridden equals() method will be called at line 9. It returns true as the isCold object is of Boolean type and is wrapping the same value as isRaining object.


1
 package com.witscale.scjp.examples;        // Package declaration
2
3
 public class Test {                        // class declaration.
4
5
   public static void main(String[] args) {
6
    Boolean isRaining = new Boolean(true); 
7
    Boolean isCold = new Boolean(true);
8
    System.out.println(isRaining == isCold;);        // It will print false on console.
9
    System.out.println(isRaining.equals(isCold););        // It will print true on console.
10
    }
11
 }