String s1 = "123abc"; String s2 = s1+"!"; String s3 = "123abc!"; System.out.println(s2==s3);
时间: 2024-05-24 18:12:00 浏览: 85
false
Explanation:
The operator "==" checks if two variables refer to the same object in memory. In this case, s2 and s3 are two different objects, with s2 being the concatenation of s1 and "!", while s3 is a string literal that contains the same characters. Therefore, s2 and s3 do not refer to the same object in memory, and the expression s2==s3 evaluates to false.
相关问题
String s1 = "abc"; String s2 = "abc"; System.out.println(s1 == s2); String s3 = "abc" + s1 + "def"; String s4 = "abcabcdef"; System.out.println(s3 == s4); String s5 = s3.intern(); System.out.println(s4 == s5);
s1和s2的结果是true,因为它们都指向字符串常量池中的同一个字符串对象"abc"。
s3和s4的结果是false,因为s是通过字符串拼接生成的新的字符串对象,在堆内存中分配。而s4是直接指向字符串常量池中的对象"abcabcdef"。
s5的结果是true,因为调用s3.intern()方法后,会在字符串常量池中查找是否存在"abcabcdef"这个字符串对象。由于之前已经创建过,所以s5会指向它。所以s4和s5都指向了同一个字符串对象。
String s1 = "abc"; String s2 = new String("abc"); System.out.println(s1==s2); String s3 = "你好吗"; String s4 ="你"; String s5 ="好吗"; System.out.println(s3==(s4+s5))
s1 == s2 的结果是 false,因为 s1 是字符串常量池中的字符串,s2 是在堆中新创建的字符串对象,它们的引用地址不同。
s3 == (s4+s5) 的结果也是 false,因为 s4+s5 会创建一个新的字符串对象,而不是从字符串常量池中获取。
阅读全文