码迷,mamicode.com
首页 > 编程语言 > 详细

Java中的equals()与==的区别

时间:2015-04-01 19:25:26      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

1 根类object中的equals()与==没有区别:
	如普通类Person中:
	Person per1 = new Person("张三",20);
	Person per2 = new Person("张三",20);
	System.out.print(per1.equals(per2));的结果为true。
2 String类改写了Object的equals();
	==是指对内存地址的值进行比较
     	equals()是对字符串的内容进行比较 
	如:String str1="hello";
	       String str2=new String("hello");
	       String str3=new String("hello").intern();
	       System.out.print(str1.equals(str2));的结果为true,因为内容相同;
	       System.out.print(str1==str2);的结果为false,因为str1为引用类型;"hello"会直接进入对象池(堆内存)中,而str2中的不会入池。二者的地	 址当然不一样。
		而经手动入池的str3保证了其地址的值相同故:
	        System.out.print(str1==str3);的结果为true。

Java中的equals()与==的区别

标签:

原文地址:http://www.cnblogs.com/yangchuan120120/p/4384635.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!