1 package test; 2 3 public class Square { 4 long width; 5 public Square(long l) { 6 width = l; 7 } 8 public static void main(String arg[]) { 9 Square a, b, c; 10 a = new Square(42L); 11 b = new Square(42L); 12 c = b; 13 long s = 42L; 14 System.out.println(a == b); // false15 //System.out.println(s == a); //Incompatible operand types long and Square16 System.out.println(b == c); // true17 System.out.println(a.equals(s)); // false18 } 19 }
在上面的例子中,a与b为Square的实例,为两个不同的对象;
s为基本类型,与对象进行比较时会报错,报错类型:Incompatible operand types long and Square