Site Search:

Test equality between Strings and other objects using == and equals ()

Back OCAJP



a==b is true when a and b are primitive data with same value or object reference variables pointing to the same object. When comparing two numeric primitive types, the values are automatically promoted. 5 == 5.00 is true. String reference variables a, b that points to the same String object in the string pool also satisfy.

a.equals(b)
Object class have method equals(), which returns true only when a == b. Since any class inherits from Object class, they by default have equals method, while primitive data types don't have equals method.

array class don't override equals method, so if a!= b, a.equals(b) will returns false even though they have same contents.
ArrayList override equals() method, if a and b have the same contents, a.equals(b) will returns true.

String override equals method. StringBuilder didn't override equals method.


OCAJP>cat test.java 
import java.util.*;
class test{
  public static void main(String[] args) {
    String a = "java";
    //StringBuilder b = StringBuilder("java");  //not compile
    //StringBuilder b1 = "java";  //incompatible types: String cannot be converted to StringBuilder
    StringBuilder b = new StringBuilder("java");
    //System.out.println(a==b);  //incomparable types: String and StringBuilder
    a = a.concat(" test");
    b = b.append(" test");
    a.concat("...");  //String is immutable
    b.append("...");
    System.out.println(a+b);
    b.delete(b.indexOf("."), b.length());
    System.out.println(b);
    System.out.println("String == only when they point to same object");
    System.out.println("java" == "java");  //same object in string pool
    System.out.println(new String("java") == new String("java"));  //2 objects not in string pool
    System.out.println("java" == new String("java"));  //1 object not in string pool
    System.out.println(a=="java test");  //a made from String operation, not in string pool
    //System.out.println(b=="java test");  //incomparable types: StringBuilder and String
    System.out.println("String overide equals method");
    System.out.println(a.equals("java test"));
    System.out.println(new String("java").equals(new String("java")));
    System.out.println(a.equals(b));
    System.out.println(a.equals(b.toString()));
    System.out.println("StringBuilder didn't override equals method");
    System.out.println(b.equals(a));
    System.out.println(new StringBuilder("java").equals(new StringBuilder("java")));
    System.out.println(b.equals("java test"));
    System.out.println("ArrayList override equals method");
    List l1 = new ArrayList();
    List l2 = new ArrayList();
    System.out.println(l1.equals(l2));
    System.out.println("array didn't overrid equals method");    
    int[] c = new int[]{1,2};
    int[] d = new int[]{1,2};
    System.out.println(c.equals(d));
  }
}
OCAJP>javac test.java 
OCAJP>java test
java testjava test...
java test
String == only when they point to same object
true
false
false
false
String overide equals method
true
true
false
true
StringBuilder didn't override equals method
false
false
false
ArrayList override equals method
true
array didn't overrid equals method
false
Back OCAJP