If you want to determine whether two class instances are the same instance, you use
equals
, which is a method rather than an operator. Note that
the equals
method determines whether two instances are the
same instance, rather than equivalent instances:
public class Demonstrate { public static void main (String argv[]) { Movie m1 = new Movie(3, 4, 5); Movie m2 = new Movie(3, 4, 5); Movie m3 = new Movie(4, 5, 6); System.out.println(m1.equals(m2)); System.out.println(m2.equals(m3)); m3 = m2 = m1; System.out.println(m1.equals(m2)); System.out.println(m2.equals(m3)); } } --- Result --- false false true true