您的位置:首页 > 编程语言 > Java开发

java中==和equals

2015-07-21 10:41 555 查看
<span style="font-size:18px;">/**关于equals和==的区别
* 练习equals和==
* equals比较内容,==比较的是指针
* @author hello
*
*/
public class StringCompete {

public static void main(String[] args) {
/*String str1="hello";
String str2="hello";
String str3=new String("hello");
String str4=new String("hello");
System.out.println(str1.equals(str2));
System.out.println(str1==str2);
System.out.println(str1==str3);
System.out.println(str3==str4);
*/
//int a=500;//a等于5000或者是5都出现true的结果
//int b=500;
//int a=new Integer(5);@3
//	int b=new Integer(5);@4
Integer a= new Integer(5);//@5
Integer b=new Integer(5);//@6
System.out.println(a.equals(b));
System.out.println(a==b);
}

}
/*
* String是个类所以和其他是不一样的在这一点上而其他就没有这么的另类了,如果把它换成
* 是int后依然出现了true说明在java这门语言上出现了共用的现象
* 在用了包装类之后的@3和@4也是出现了true这种现象
* 但是@5和@6就没有出现这种情况
* 以上情况我猜测是因为java编译器进行了自动转换直接把包装类进行了拆箱
*/</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: