一、“==” 的使用
1.== 是一个运算符 既可以使用在基本数据类型变量,又可以使用于引用数据类型变量中,对于基本类型就是比较值(不一定类型要相同),对于引用类型就是比较内存地址,即两个引用是否指向同一实体
二、equals()方法
1.是一个方法,而非运算符
2.只适用于引用数据类型
3.Object类中equals()的定义
public boolean equals(Object obj) {
return (this == obj);
}
说明:Object类中定义的equals()方法和==的作用是相同的,比较两个对象的地址值是否相同,即两个引用是否指向同一实体
4.像String、Date、File、包装类等都重写了equals()方法,重写以后,比较的就不是两个引用地址值是否相同,而是比较两个对象的实体内容是否相同
5.通常情况下 ,我们自定义类如果使用equals()的话,也通常是比较两个对象的 “实体内容” 是否相同。那么我们就需要对Object类中的equals()进行重写
@Override
//手动生成
public boolean equals(Object obj) {
if(this == obj) {//如果当前对象的引用地址和形参的引用地址一样 即二者肯定指向同一对象
return true;
}
if(obj instanceof Orders){//判断obj是否是Orders类型 如果是 先强转下来 转成我俩都是Order类型
Orders order = (Orders)obj; //如果不转型 Orders Orders类里面的一些属性无法使用
//最后比较两个对象的每个属性的内容 引用数据类型必须用equals
return this.orderId == order.orderId && this.orderName.equals(orderName);
}
return false;
}
@Override
//自动生成
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Orders other = (Orders) obj;
if (orderId != other.orderId)
return false;
if (orderName == null) {
if (other.orderName != null)
return false;
} else if (!orderName.equals(other.orderName))
return false;
return true;
}
重写 equals() 方法的原则 对称性: 如果 x.equals(y) 返回是“ true ”,那么 y.equals(x) 也应该返回是“ true ”。 自反性: x.equals(x) 必须返回是“ true ”。 传递性: 如果 x.equals(y) 返回是“ true ”,而且 y.equals(z) 返回是“ true ”,那么 z.equals(x) 也应该返回是“ true ”。 一致性: 如果 x.equals(y) 返回是“ true ”,只要 x 和 y 内容一直不变,不管你重复 x.equals(y) 多少次,返回都是“ true ”。 任何情况下, x.equals(null) ,永远返回是“ false ”;x.equals( 和 x 不同类型的对象 ) 永远返回是“ false ”。
int it = 65;float fl = 65.0f;System.out.println(“65 和 65.0f 是否相等? ” + (it == fl)); //truechar ch1 = 'A'; char ch2 = 12;System.out.println("65 和 'A' 是否相等? " + (it == ch1));//trueSystem.out.println(“12 和 ch2 是否相等? " + (12 == ch2));//trueString str1 = new String("hello");String str2 = new String("hello");System.out.println("str1 和 str2 是否相等? "+ (str1 == str2));//falseSystem.out.println("str1 是否 equals str2 ? "+(str1.equals(str2)));//trueSystem.out.println(“hello” == new java.util.Date()); // 编译不通过
面试题二
public void test() {char [] arr = new char [] { 'a' , 'b' , 'c' };System. out .println( arr ); //abcint [] arr1 = new int [] { 1, 2, 3 };System. out .println( arr1 ); //[I@4459eb14double [] arr2 = new double [] { 1.1, 2.2, 3.3 };System. out .println( arr2 ); //[D@5a2e4553}
面试题三
如下两个题目输出结果相同吗?各是什么:Object o1 = true ? new Integer(1) : new Double(2.0);//三目运算符要同一个类型 所以 new Integer(1)自动提升到Double型System. out .println( o1 ); //1.0Object o2 ;if ( true )o2 = new Integer(1);elseo2 = new Double(2.0);System. out .println( o2 ); //1
面试题四
public void method1() {Integer i = new Integer(1);Integer j = new Integer(1);System. out .println( i == j ); //falseInteger m = 1; //自动装箱Integer n = 1; //自动装箱System. out .println( m == n );//trueInteger里面有一个内部类IntegerCache ,IntegerCache中定义了一个Integer[],保存了从-128~127范围的整数。如果我们使用自动装箱的方式,给Integer赋值的范围在-128~127内,可以直接使用数组元素不用再去new了目的是为了方便我们调用,因为这部分数使用的次数比较频繁,提高效率Integer x = 128; //相当于new了一个Integer对象Integer y = 128; //相当于new了一个Integer对象System. out .println( x == y );//false}