关于对象与类型

 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); // false
15         //System.out.println(s == a); //Incompatible operand types long and Square
16         System.out.println(b == c); // true
17         System.out.println(a.equals(s)); // false
18     } 
19 }

在上面的例子中,a与b为Square的实例,为两个不同的对象;

s为基本类型,与对象进行比较时会报错,报错类型:Incompatible operand types long and Square

猜你喜欢

转载自www.cnblogs.com/huanghy/p/10502189.html