Jvava面试题( “==” 和equals()的区别)

一、“==” 的使用

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)); //true
char ch1 = 'A'; char ch2 = 12;
System.out.println("65 'A' 是否相等? " + (it == ch1));//true
System.out.println(“12 ch2 是否相等? " + (12 == ch2));//true
String str1 = new String("hello");
String str2 = new String("hello");
System.out.println("str1 str2 是否相等? "+ (str1 == str2));//false
System.out.println("str1 是否 equals str2 "+(str1.equals(str2)));//true
System.out.println(“hello” == new java.util.Date()); // 编译不通过

面试题二 

public void test() {
        char [] arr = new char [] { 'a' , 'b' , 'c' };
        System. out .println( arr ); //abc
        int [] arr1 = new int [] { 1, 2, 3 };
        System. out .println( arr1 ); //[I@4459eb14
        double [] 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.0
Object o2 ;
if ( true )
o2 = new Integer(1);
else
o2 = 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 );    //false
        Integer m = 1;  //自动装箱
        Integer n = 1;  //自动装箱
        System. out .println( m == n );//true
Integer里面有一个内部类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
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值