5

all, i faced a problem when a write the code below

String hello = "Hello";
String str5 = "Hel" + "lo";
String str8 = "Hel";
String str9 = "lo";
String str10 = str8 + str9;
System.out.println("str10==hello?" + (str10 == hello)); 
System.out.println("str5==hello?" + (str5 == hello));
System.out.println("str10==str5?" + (str10 == str5));

then i run my code and the console print this

str10 == hello ? false
str5 == hello ? true
str10 == str5 ? false

this confused me a lot. why the second print TRUE but the first print FALSE?? in my comprehension of String literal pool,when a string defined and JVM will check if the pool contains that string,if not ,put the string into the pool.
in my code,variable hello exists in string pool,"Helo" and "lo" also in the pool,my question is

  1. if the result of the concatenation of "Helo" and "lo" exists in the pool.
  2. what's the difference between the definition about str5 and str10s',and why they are not "=="? does str5 and str10 refer to the diffrent "Hello” that in the string pool?("==" seems to mean the reference is the same object)

my jdk version :1.6.0_29
my IDE:Intellij Idea 11.2

anyone can point it out? thank you very much

4

4 回答 4

7

它的行为应有尽有。它在 JLS 的两个部分中得到解决。

JLS #3.10.5

作为常量表达式的值的字符串(第 15.28 节) - 使用 String.intern 方法“内部”以便共享唯一实例。

JLS #15.28列出了被视为常量表达式的内容。特别是,字符串文字是常量表达式(“Hel”和“lo”),但要使变量被视为常量,它必须是最终的。

在您的情况下,如果您将代码稍微更改为 makestr8str9constant,您将获得true三倍:

final String str8 = "Hel";
final String str9 = "lo";
于 2013-03-15T08:23:43.993 回答
1
String hello = "Hello";       // at compile time string is known so in String Constant Pool

String str5 = "Hel" + "lo";   // at compile time string is known so in String Constant Pool same object as in variable hello

String str8 = "Hel";          // at compile time string is known so in String Constant Pool

String str9 = "lo";           // at compile time string is known so in String Constant Pool

String str10 = str8 + str9;   // at runtime don't know values of str8 and str9 so in String Constant Pool new object different from variable hello

str10 == hello ? false        // as str10 has new object and not the same as in hello

str5 == hello ? true          // both were created at compile time so compiler know what's the result in str5 and referenced the same object to str5 as in hello

str10 == str5 ? false         // str10 is a different object, hello and str5 are referenced same object as created at compile time.
于 2015-07-23T17:30:47.263 回答
0

代码有以下几点需要考虑:

String hello = "Hello";

这里“Hello”是分配给引用 hello 的文字,因此文字有自己的哈希码

String str5 = "Hel" + "lo";

这里 "Hel"+"lo" 是 2 个文字组合并分配给引用 hello 因此新文字与第一个文字相同,因此哈希码相同

String str8 = "Hel";
String str9 = "lo";

这里 str8 + str9 是 2 个引用,它们组合并指向一个新的引用 hello,因此新的文字有自己的哈希码

String str10 = str8 + str9;
System.out.println("str10==hello?" + (str10 == hello)); 
System.out.println("str5==hello?" + (str5 == hello));
System.out.println("str10==str5?" + (str10 == str5));

当您使用 == 时,它通过哈希码和值匹配。因此不匹配。

尝试使用

string_1.equals(string_2)

代替

字符串_1 ==字符串_2

你只会得到价值匹配。因此都是真的

另请参阅以下答案(来自Java中== vs equals()之间的区别是什么?):

equals() 方法比较 String 实例(在堆上)内的“值”,无论两个(2)对象引用是否引用相同的 String 实例。如果任何两 (2) 个 String 类型的对象引用引用同一个 String 实例,那就太好了!如果两(2)个对象引用引用了两(2)个不同的字符串实例..它没有区别。它是每个被比较的 String 实例中的“值”(即:字符数组的内容)。

另一方面,“==”运算符比较两个对象引用的值,以查看它们是否引用同一个 String 实例。如果两个对象的值引用“引用”同一个 String 实例,那么布尔表达式的结果将是“true”..duh。另一方面,如果两个对象引用的值“引用”不同的 String 实例(即使两个 String 实例具有相同的“值”,即每个 String 实例的字符数组的内容相同),则布尔表达式的结果将是“假”。

于 2013-03-15T08:30:19.960 回答
-1

如果您比较两个字符串,请使用string.equalsnotstring1 == string2

试试看:

System.out.println("str10==hello?" + (str10.equals(hello));
于 2013-03-15T08:25:57.377 回答