equals()比较的是值,==比较的是地址.
String sa = new String("hello");
String sb = new String("hello");
sa == sb;//false,sa和sb分别存放了两个字符串对象的引用及地址,肯定不同
String sa = "hello";
String sb = "hello";
sa == sb;//true,sa和sb存放的是字符串常量在常量池中的地址,是一样的
String sa = "hello "+"world";
String sb = "hello world";
sa == sb;//true,编译器会直接在编译时对字符串常量进行连接,因此sa和sb存储的是一样的,常量池中只有"hello world"
String s1 = "hello ";
String s2 = "world";
String sa = s1 + s2;
String sb = "hello world";
sa == sb;//false,java程序运行时,对于字符串连接操作符‘+’,是将其转为StringBuilder()类或StringBuffer()类的append()方法进行连接,相当于new了一个新的字符串对象
"a".toLowerCase()=="a";//true,对于字符串的操作如果结果不变,则会返回原来的字符串,所以相等.
("a"+"b").toLowerCase()=="ab";//true,参考上例"hello "+"world"的解释.
String sa = new String("hello");
String sb = new String("hello");
sa == sb;//false,sa和sb分别存放了两个字符串对象的引用及地址,肯定不同
String sa = "hello";
String sb = "hello";
sa == sb;//true,sa和sb存放的是字符串常量在常量池中的地址,是一样的
String sa = "hello "+"world";
String sb = "hello world";
sa == sb;//true,编译器会直接在编译时对字符串常量进行连接,因此sa和sb存储的是一样的,常量池中只有"hello world"
String s1 = "hello ";
String s2 = "world";
String sa = s1 + s2;
String sb = "hello world";
sa == sb;//false,java程序运行时,对于字符串连接操作符‘+’,是将其转为StringBuilder()类或StringBuffer()类的append()方法进行连接,相当于new了一个新的字符串对象
"a".toLowerCase()=="a";//true,对于字符串的操作如果结果不变,则会返回原来的字符串,所以相等.
("a"+"b").toLowerCase()=="ab";//true,参考上例"hello "+"world"的解释.