java中==和equals()的区别

1.==用来比较两个基本数据类型数值是否相同

 

public class StringDemo03{
	public static void main(String args[]){
		int x = 30 ;
		int y = 30 ;
		System.out.println("两个数字的比较结果:" + (x==y)) ;
	}
}

程序运行结果:

 

两个数字的比较结果:true

 

如果使用==比较字符串的内容,会出现以下错误:

 

public class StringDemo04{
	public static void main(String args[]){
		String str1 = "hello" ;					// 直接赋值
		String str2 = new String("hello") ;		// 通过new赋值
		String str3 = str2 ;					// 传递引用
		System.out.println("str1 == str2 --> " + (str1==str2)) ;	//(a)
		System.out.println("str1 == str3 --> " + (str1==str3)) ;	//(b)
		System.out.println("str2 == str3 --> " + (str2==str3)) ;	//(c)
	}
};

 

程序运行结果:

str1 == str2-->false

str1 == str3-->false

str2 == str3-->true

虽然以上程序中String内容都相同,但比较结果有时相同又是不同,如图

(a)对str1和str2来说,其内容分别保存在不同的空间,所以即使内容相等,地址值也是不相等的。==是用来进行数值比较,所以str1和str2比较不相等。str1 == str2-->false

(b)对str1和str3来说,其内容分别保存在不同的空间,所以即使内容相等,地址值也是不相等的。==是用来进行数值比较,所以str1和str3比较不相等。str1 == str3-->false

(c)对str2和str3来说,它们指向了同一个空间,是同一个地址,所以str2和str3得地址值相等str2 == str3-->true

 

如果要比较字符串的内容可以使用equals方法

 

public class StringDemo05{
	public static void main(String args[]){
		String str1 = "hello" ;					// 直接赋值
		String str2 = new String("hello") ;		// 通过new赋值
		String str3 = str2 ;					// 传递引用
		System.out.println("str1 equals str2 --> " + (str1.equals(str2))) ;	
		System.out.println("str1 equals str3 --> " + (str1.equals(str3))) ;	
		System.out.println("str2 equals str3 --> " + (str2.equals(str3))) ;	
	}
};

 

程序运行结果:

str1 equals str2-->true

str1equals  str3-->true

str2equals  str3-->true

 


2.equals方法用来比较两个对象是否相同,即两个对象的内容是否相等。

 

class Student{
	private String name;
	private int age;
	public Student(String name, int age){
		this.name = name;
		this.age = age;
	}
	
	public boolean equals(Object obj){  //覆写Object类中的equals()方法
		if(this == obj){         //如果两个对象地址相等,则肯定是同一对象
			return true;
		}
		
		if(!(obj instanceof Student)){      //判断传进来的对象是否是Student的实例
			return false;
			
		}
		
		Student stu =(Student)obj;      //向下转型
		if(stu.name.equals(this.name) && stu.age == this.age){   //逐个属性比较
			return true;
		}else{
			return false;
		}
	}
	
	public String toString(){
		return "姓名" +this.name + "年龄" +this.age;
	}
}
public class ObjectDemo{
	public static void main(String[] args) {
		Student stu1 = new Student("小明",20);
		Student stu2 = new Student("小明",20);
		System.out.println(stu1 == stu2);
		System.out.println(stu1.equals(stu2));

	}

}

程序运行结果:

 

false

true

Object类的equals方法源码:

 

 public boolean equals(Object obj) {
 return (this == obj);}


       在用Java的类描述对象的时候,如果要判断该对象是否相同时,比如学生对象有自己的姓名和年龄,希望根据姓名和年龄的相同来判断学生对象是否相同。这时使用Object类的equals就不能满足需求了,就需要通过覆盖equals的方式,建立学生对象比较相同的具体内容。这也就是大家常听到的“==是比较地址的,equals是用来比较对象内容”的原因。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值