<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">import java.util.Arrays;

// == vs .equals

class Equals {

   public static void main(String[] args) {

      /*
       * == just checks for identity of the references, i.e. whether the
       * references refer to the same object. This is called "shallow equality".
       * For reference types, this is almost never what you want.
       * 
       * .equals tests whether the Objects look the same, i.e. so-called "deep
       * equality". For classes you define, if you want to compare them, you
       * usually have to define your own .equals method.
       * 
       * The .equals method of Object just defaults to ==.  So if you don't
       * override it in your class, that is what you get.
       */
      
      // in A, Object.equals is not overridden
      A a1 = new A(2110);
      A a2 = new A(2110);
      System.out.println("A test: ");
      System.out.println(a1 == a2);
      System.out.println(a1.equals(a2));
      System.out.println();

      // in B, it is overridden to give deep equality:
      B b1 = new B(2110);
      B b2 = new B(2110);
      System.out.println("B test: ");
      System.out.println(b1 == b2);
      System.out.println(b1.equals(b2));
      System.out.println();

      // Strings in Java are Objects, and Strings.equals
      // compares the strings character by character
      String s1 = new String("hello");
      String s2 = new String("hello");
      System.out.println("String test: ");
      System.out.println(s1 == s2);
      System.out.println(s1.equals(s2));
      System.out.println();

      // Strings literals are very tricky.  For equivalent string
      // literals, sometimes == answers true, sometimes false.
      // Can you explain what is going on here?
      // the moral is, always use .equals when comparing strings
      System.out.println("String literals == test: ");
      final String s3 = "hel";
      final String s4 = "lo";
      String s5 = "hel";
      String s6 = "lo";
      System.out.println("hello" == "hel" + "lo");
      System.out.println("hello" == s3 + s4);
      System.out.println("hello" == s5 + s6);
      System.out.println();


      // arrays do not have .equals defined as equality of contents,
      // but there is a method in the Arrays class to compare arrays
      // on their contents
      int[] x1 = { 1, 2, 3 };
      int[] x2 = { 1, 2, 3 };
      System.out.println("Array test: ");
      System.out.println(x1 == x2);
      System.out.println(x1.equals(x2));
      System.out.println(Arrays.equals(x1, x2));
   }
}

class A {
   int k;

   A(int k) {
      this.k = k;
   }
}

class B {
   int k;
   
   B(int k) {
      this.k = k;
   }

   public boolean equals(Object b) {
      return b != null &amp;&amp; b instanceof B &amp;&amp; k == ((B)b).k;
   }
}

/* Output:
A test: 
false
false

B test: 
false
true

String test: 
false
true

String literals == test: 
true
true
false

Array test: 
false
false
true
*/
</pre></body></html>