黑马程序员技术交流社区
标题:
字符串的面试题
[打印本页]
作者:
嘿→那个谁㊣
时间:
2018-5-12 17:04
标题:
字符串的面试题
字符串的拼接
String s1 = "hello";
String s2 = "world";
String s3 = "helloworld";
System.out.println(s3 == s1 + s2);// false
System.out.println(s3.equals((s1 + s2)));// true
System.out.println(s3 == "hello" + "world");// false 这个我们错了,应该是true
System.out.println(s3.equals("hello" + "world"));// true
作者:
heimaxxm
时间:
2018-5-18 19:43
System.out.println(s3 == "hello" + "world");
这句是true的原因:
因为编译器会把"hello"+"world"直接合并为"helloworld"常量,s3引用的是常量池里的"helloworld",所以比较地址值就true
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2