Java String Instantiation

后端 未结 3 1693
梦如初夏
梦如初夏 2021-01-13 19:18

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

package com.company;


public class Main {

    public static void main(String[] args) {

        S         


        
3条回答
  •  一整个雨季
    2021-01-13 20:00

    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.

提交回复
热议问题