Skip to main content
Lesson 12 - Object References
Lesson MenuPreviousNext
  
The == operator with Variables and Object References page 7 of 10

  1. The == operator is used to look at the contents of two reference variables. If the contents of both reference variables are the same, then the result is true. Otherwise the result is false.

    public class EqualsEquals
    {
      public static void main (String[] args)
      {
        String strA;  // reference to the first object
        String strB;  // reference to the second object
       
        // create the first object and save its reference
        strA = new String("same characters"); 
        System.out.println(strA); 
    
        // create the second object and save its reference
        strB = new String("same characters");   
        System.out.println(strB);
    
        if (strA == strB) 
          System.out.println("This will not print.");
      }
    }
    
    Run Output:
    
    same characters
    same characters
  2. In this program, there are two completely separate objects, strA and strB, each of which happens to contain the same character data. Each object consists of a section of main memory completely separate from the memory that makes up the other object. The variable strA contains information on how to find the first object, and the variable strB contains information on how to find the second object.

    Since the information in strA is different from the information in strB, (strA == strB) is false. Since there are two objects, made out of two separate sections of main memory, the reference stored in strA is different from the reference in strB. It doesn't matter that the data inside the objects looks the same.

  3. The == operator does not look at objects. It only looks at references (information about where an object is located.)

  4. For primitive types, the == operator looks only at the variables. For example:

    int x = 32, y = 48;
    
    if (x == y)                             // false, 32 != 48
      System.out.println("They are equal");
    
    x = y;
    
    if (y)                             // true, 48 == 48
      System.out.println("Now they are equal");
    
    Run Output:
    
    Now they are equal

    In this code, only the contents of the variables x and y are examined. But with primitive types, the contents of a variable is the data, so with primitive types == looks at data.

    The following code fragment contains one additional example; note the different way that the strings are instantiated:

    String strA = "Hello"
    String strB = "Hello"

    When strA and strB are compared with == the result will be true because strA and strB point to the same location in memory, where "Hello" is stored. Java saves memory by not creating "Hello" in two locations.

  5. With primitive and reference types, == looks at the contents of the variables. However, with reference types, the variables contain object references and with primitive types, the variables contain the actual data values.


Lesson MenuPreviousNext
Contact
 ©ICT 2003, All Rights Reserved.