For object references, the == operator returns true only if both references are to the same object. : Comparison Operators « Operators « SCJP






public class MainClass {
  public static void main(String[] argv) {
    Integer x1 = new Integer(5);
    Integer x2 = x1;
    Integer x3 = new Integer(5);
    if (x1 == x2)
      System.out.println("x1 eq x2");
    if (x2 == x3)
      System.out.println("x2 eq x3");

  }
}
x1 eq x2








2.6.Comparison Operators
2.6.1.Comparison Operator Summary
2.6.2.Comparison operators <, <=, >, >=, = =, and != return a boolean result.
2.6.3.Comparison operators are commonly used to form conditions
2.6.4.The Ordinal Comparisons Operators: <, <=, >, and >=
2.6.5.It is acceptable to compare the float value to the char variable c.
2.6.6.Ordinal comparisons are not applicable to any non-numeric types.
2.6.7.The Equality Comparison Operators: == and != for primitive types
2.6.8.The Equality Comparison Operators: == and != for variables of object type
2.6.9.You should not use these operators to compare the contents of objects.
2.6.10.To do a content comparison, use equals() method rather than the == or != operator.
2.6.11.For object references, the == operator returns true only if both references are to the same object.
2.6.12.Define your own equals Method
2.6.13.The == with Strings Trap
2.6.14."Equality" Operators
2.6.15.Equality for Reference Variables
2.6.16.Equality for Enums