CSC300 / CSC402: Objects and equality [1/24] Previous pageContentsNext page

int[] x = new int[] { 11, 21, 31 };
int[] y = new int[] { 11, 21, 31 };
x=[I@8807e25, y=[I@2a3046da
x=[11, 21, 31], y=[11, 21, 31]
                  x==y : false
   Objects.equals(x,y) : false
           x.equals(y) : false
    Arrays.equals(x,y) : true
eqa01
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
  package algs12;
  import stdlib.*;
  import java.util.*;
  public class Hello {
    public static void main (String[] args) {
      Trace.showBuiltInObjects (true);
      Trace.run ();   
      int[] x = new int[] { 11, 21, 31 };
      int[] y = new int[] { 11, 21, 31 };
      Trace.draw ();
      StdOut.println ("x=" + x + ", y=" + y);
      StdOut.println ("x=" + Arrays.toString(x) + ", y=" + Arrays.toString(y));
      StdOut.println ("                  x==y : " + (x == y));
      StdOut.println ("   Objects.equals(x,y) : " + (Objects.equals(x,y)));
      StdOut.println ("           x.equals(y) : " + (x.equals(y)));
      StdOut.println ("    Arrays.equals(x,y) : " + (Arrays.equals(x,y)));
    }
  }

Previous pageContentsNext page