总结几句话:
==双等号始终是
1.基本数据类型:比较存储的值是否相等。
2.引用数据类型:比较所指对象的地址值是否相等。
equals
1.没有被重写过:
相当于==;但是不能比较基本数据类型,比较的是引用对象的所指对象的地址值。
2.被重写过(一般被String重写):比较的是对象的(内容)。
代码示例参考
public static void main(String[] args) {
//基本数据类型的比较
int num1 = 10;
int num2 = 10;
System.out.println(num1 == num2); //true
//引用数据类型的比较
//String类(重写了equals方法)中==与equals的比较
String s1 = "hello";
String s2 = "hello";
System.out.println(s1 == s2); //true,比较地址值:内容相同,因为常量池中只有一个“hello”,所以它们的地址值相同
System.out.println(s1.equals(s2));//true,比较内容:内容相同,因为常量池中只有一个“hello”,所以它们的地址值相同
System.out.println(s1.equals("hello")); //true
String s3 = new String("hello");
String s4 = new String("hello");
System.out.println(s3 == s4); //false,比较地址值:s3和s4在堆内存中的地址值不同
System.out.println(s3.equals(s4)); //true,比较内容:内容相同
//没有重写equals方法的类中==与equals的比较
People p1 = new People();
People p2 = new People();
People p = p2;
System.out.println(p1);//People@135fbaa4
System.out.println(p2);//People@45ee12a7
System.out.println(p); //People@45ee12a7
System.out.println(p1.equals(p2)); //false,p1和p2的地址值不同
System.out.println(p.equals(p2)); //true,p和p2的地址值相同
}
附加
String类对equals()方法进行了重写。
String s1 = "hello";//在字符串常量池中创建"hello",并将地址值赋值给s1;
String s2 = new String("world");//通过new关键字在堆中创建对象,并将对象地址值赋值给s2.
对于String s2 = new String(“world”);
首先在堆内存中申请内存存储String类型的对象,将地址值赋给s2;
在方法区的常量池中找,有无hello:
若没有,则在常量池中开辟空间存储hello,并将该空间的地址值赋给堆中存储对象的空间;
若有,则直接将hello所在空间的地址值给堆中存储对象的空间。
对于String s1 = “hello”;
在方法区的常量池中找,有无hello,如果没有,就在常量池中开辟空间存储hello。
然后只需要将hello所在空间的地址值赋给 s1。
字符串作为最基础的数据类型,使用非常频繁,如果每次都通过 new 关键字进行创建,会耗费高昂的时间和空间代价。Java 虚拟机为了提高性能和减少内存开销,就设计了字符串常量池. 在JDK1.7之前字符串常量池是存储在方法区的。JDK1.7之后存储在堆中了。