Java string instantiation

Why does this code return "false" instead of "true":

If I remember correctly:

A string (string pool) pointing to an existing address in memory should be created because we have declared a string with a value: "name LastName", which should not be automatically inserted into the string pool, and the output should not be "true". Since we have used the same string as string firstnamepluslastname in the character string pool?

Edit:

I know I should use when testing two strings for equality The equals () method, but I don't want to do so in the above example. I actually want to test reference equality, and based on Java practice, the two strings I compare in the above code should be equal, but they are not. They should point to the same address in memory because they have the same content, and neither string is created with the "new" keyword I wonder why

Solution

The string constant pool is created at compile time When you connect string literals / final variables / final fields other than final parameters, it only uses strings in the pool, for example:

Concat Literals

Concat final variable

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>