Java String Instantiation

被刻印的时光 ゝ 提交于 2019-12-19 09:17:38

问题


Why is this code returning "false" instead of "true":

package com.company;


public class Main {

    public static void main(String[] args) {

        String fullName = "Name Lastname";
        String name = "Name ";
        String lastName = "Lastname";
        String firstNamePlusLastName = name + lastName;

        System.out.println(fullName == firstNamePlusLastName);

    }
}

If I remember correctly:

String firstNamePlusLastName = name + lastName;

Should create a String that points to an existing address in memory (String pool) because we already have declared a String with value: "Name Lastname", shouldn't it be automatically interned into String pool and shouldn't the output be "true" since we already have string with same value in String pool as String firstNamePlusLastname ?

EDIT:

I know I should use .equals() method when testing two strings for equality of content, but I don't want to do that in the example above, I actually do want to test for reference equality, and based on Java interning the two strings that I compare in the code above should be equal, but they are not, they should point to the same address in memory as they have same content and neither of the Strings was created using "new" keyword. I want to know why is this happening.


回答1:


String Constant Pool create at compiling-time. it only using strings from pool when you concat String literals / final variables / final fields except final parameters, for example:

Concat Literals

String fullName = "Name Lastname";
String firstNamePlusLastName = "Name " + "Lastname";

System.out.println(fullName == firstNamePlusLastName);// true

Concat Final Variables

String fullName = "Name Lastname";
final String name = "Name ";
final String lastName = "Lastname";
String firstNamePlusLastName = name + lastName;

System.out.println(fullName == firstNamePlusLastName);//true



回答2:


Because you must use .equals() to strings

fullName.equals(firstNamePlusLastName)

== tests for reference equality (whether they are the same object), meaning exactly the same, the same reference even.

.equals() refers to the equals implementation of an object, it means, for string, if they has the same chars in the same order.

Consider this:

    String a = new String("a");
    String anotherA = new String("a");
    String b = a;
    String c = a;

a == anotherA -> False;
b == a -> True
b == c -> true
anotherA.equals(a) -> True



回答3:


String literals are interned. When you create a string through any method that isn't a literal (calling new String(), reading from input, concatenation, etc) it will not be automatically interned. When you called String firstNamePlusLastName = name + lastName; You concatenated name and lastName creating a new string. This is not a literal and you didn't call intern so this string is not added to the string pool.



来源:https://stackoverflow.com/questions/44121813/java-string-instantiation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!