gpt4 book ai didi

java: readLine 方法将字符串放入字符串文字池?

转载 作者:行者123 更新时间:2023-11-30 05:53:50 25 4
gpt4 key购买 nike

我知道 String a = "hello";会将“hello”放入字符串文字池。我的问题是:
1.

String a = "hello";
String b = "hell"+"o";

字符串文字池是否有“hello”、“hell”、“o”三个对象?

2.

String a = "hello";
String b = new String("hello");

那么在字符串文字池中会有一个“hello”对象,在堆中会有一个字符串对象?

3.

BufferedReader br = new BufferedReader(new FileReader("names"));
String line = br.readLine(); //(the value of line is "hello" now, for example)

那么在字符串文字池中会有一个“hello”对象,在堆中会有一个字符串对象?

最佳答案

3) readLine() 不会使用字符串池中的值。您必须调用 intern()

line = br.readLine().intern();

来自String Javadoc :

All literal strings and string-valued constant expressions are interned. String literals are defined in §3.10.5 of the Java Language Specification.

关于java: readLine 方法将字符串放入字符串文字池?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10135071/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com