equals与==与string类创建对象的方式
首先:通常情况来说equals == 比较的都是引用
在Java中游8种基本数据类型:
浮点型:float(4 byte), double(8 byte)
整型:byte(1 byte), short(2 byte), int(4 byte) , long(8 byte)
字符型: char(2 byte)
布尔型: boolean 这8种基本数据类型的变量,变量直接存储的是“值”,因此在用关系操作符==来进行比较时,比较的就是 “值” 本身
equals方法不能作用于基本数据类型的变量 String Double,Date,Integer等,都对equals方法进行了重写用来比较指向的对象所存储的内容是否相等
所以在以上该类的对象调用equals方法时比较的是两个对象的值是否相同
String str1 = new String("hello");
String str2 = new String("hello");
System.out.println(str1==str2); //false
System.out.println(str1.equals(str2)); //true
String str = new String("hello"); //堆中第一个对象
String str1 = new String("hello"); //堆中第二个对象
String str2 = new String("hello"); //堆中第三个对象
System.out.println(str1==str2); //明显的false,str1与str2是堆中的两个不同对象
str1 = str;
str2 = str;
System.out.println(str1==str2); //明显的相等。引用都指向了第一个对象
string创建对象的两种方式以及区别:
String a="hellow";
String a=new String("java"); ![]()
如果:String a="hellow"
a="hellowworld";
system.out.println(a); //输出的是hellowworld 是否表示string 可以被改写呢?
a从hello变为java不是hello被改成了java,而是系统又创建了一个对象,内容为java,a只是从指向hello变为指向java而已

浙公网安备 33010602011771号