虚拟机栈 中存放了局部变量,基本数据类型boolean、byte、char、short、int、float、double、long、对象引用(不是对象本身) 堆 中存放了对象实例
==比较的是对象内存地址 String类中的equals()比较的是对象的内容
public void testString() {
String test1 = "mytest";
String test2 = "mytest";
String test3 = new String("mytest");
String test4 = new String("mytest");
String test5 = "mytes";
test5 = test5 + "t";
String test6 = "mytes" + "t";
assertEquals(test1, test3);
**assertFalse(test1 == test3);**
assertTrue(test1 == test2);
**assertFalse(test3 == test4);
assertFalse(test1 == test5);**
assertTrue(test1 == test6);
}
参考资料: https://www.cnblogs.com/wskb/p/10970787.html
|