如果我从方法调用或从中获取字符串对象StringBuilder.toString(),该字符串是否会添加到字符串池中?
String类是否仅在类加载期间添加到池中?
任何人都可以解释为什么toString()和name()引用相同的字符串?当我使用==将它们与字符串文字进行比较时,它们都会通过!枚举名如何与JVM中的String池一起使用?
static enum User
{
BASIC, PREMIUM;
}
System.out.println("BASIC" == User.BASIC.toString()); // true
System.out.println("BASIC" == User.BASIC.name()); // true
Run Code Online (Sandbox Code Playgroud) 注意:我不是要比较角色是否等于.因为我知道如何使用String.equals()方法.这个问题是关于字符串引用的
当我开始学习类字符串及其属性作为不变性等时,我正在学习OCA考试.根据我读到或可能理解的字符串池是在创建字符串时,Java将此对象存储在他们调用的内容中字符串池,如果创建一个具有相同值的新字符串,它将引用字符串池上的字符串,除非我们使用new关键字,因为这会创建一个新引用,即使两个字符串包含相同的值.
例如:
String a = "foo";
String b = "foo";
String c = new String("foo");
boolean ab = (a == b); // This return true
boolean ac = (a == c); // This return false
Run Code Online (Sandbox Code Playgroud)
要清楚这段代码是在第一行代码中创建String a = "foo"并将其存储在String池中,而在第二行代码中它将创建String b和引用,"foo"因为它已存在于String池中.但是第3行将创建此字符串的新引用,无论它是否已存在.这是一个关于发生了什么的图形示例:

问题出现在以下代码行中.当串联创建字符串时,java会做出不同的东西或简单= =比较器有另一种行为吗?
例A:
String a = "hello" + " world!";
String b = "hello world!";
boolean compare = (a == b); …Run Code Online (Sandbox Code Playgroud) 在Java中,当我们写
String S1 = "TestString";
String S2 = "TestString";
Run Code Online (Sandbox Code Playgroud)
然后与 进行比较if(S1==S2),我们得到 true 作为布尔结果。对此的解释是,字符串常量是在字符串池中创建的,因此 S1 和 S2 在这里引用的是相同的字符串常量。另外,如果我们写类似的东西
String S1 = new String("TestString");
String S2 = new String("TestString");
Run Code Online (Sandbox Code Playgroud)
然后与 进行比较if(S1==S2),我们得到 false。原因是 S1 和 S2 的引用不同,因为字符串文字是在堆中创建的。
我的问题是,在创建的构造函数中传递的字符串文字“TestString”在哪里?它与字符串文字/常量相同吗?因此应该像情况 1 一样在池中创建?如果是那么当我们在上面两个语句之后写类似的内容时
String S3 = "TestString";
Run Code Online (Sandbox Code Playgroud)
这不应该创建一个新的字符串文字,比较if(S1==S3)应该给我 true,但它给我 false。
所以我无法弄清楚这个字符串文字是在何时何地传递到构造函数中创建的。
任何帮助将不胜感激。谢谢
我对Java的字符串池有一些了解。网络中的所有示例都明确地创建了变量。但是,如果我从方法返回硬编码的字符串,将会发生什么。是使用字符串池还是为每个方法调用一次又一次创建字符串。我找不到有关此案的任何信息。
我有这样的例子:
public class TestService {
@Override
protected Optional<String> getPayTypeCode() {
return Optional.of("LDV");
}
//...
}
Run Code Online (Sandbox Code Playgroud)
常量示例:
public class TestService {
private static final String PAY_CODE = "LDV";
@Override
protected Optional<String> getPayTypeCode() {
return Optional.of(PAY_CODE);
}
//...
}
Run Code Online (Sandbox Code Playgroud)
我想知道,我的第一种情况是使用字符串池吗?哪种情况会更快?
假设一个方法返回一个字符串
String str = methodCall();
methodCall() 的返回类型是 String。
我们会得到一个字符串文字,它会在池内存中还是只是一个字符串对象?
java garbage-collection memory-management string-pool java-heap
最近学习了Java String Pool,有一些不太明白的地方。
使用赋值运算符时,如果字符串池中尚不存在新字符串,则会在该字符串池中创建一个新字符串。
String a = "foo"; // Creates a new string in the String Pool
String b = "foo"; // Refers to the already existing string in the String Pool
Run Code Online (Sandbox Code Playgroud)
使用 String 构造函数时,我知道无论 String Pool 的状态如何,都会在 String Pool 之外的堆中创建一个新字符串。
String c = new String("foo"); // Creates a new string in the heap
Run Code Online (Sandbox Code Playgroud)
我读的地方,使用构造函数,即使,字符串池中被使用。它将字符串插入到字符串池和堆中。
String d = new String("bar"); // Creates a new string in the String Pool and in the heap
Run Code Online (Sandbox Code Playgroud)
我没有找到关于此的任何进一步信息,但我想知道这是否属实。
如果这确实是真的,那么 - …
下面显示了几种创建字符串的方法.在注释的表达式后面添加问题.
String str = "test";
String str1 = new String(str); //Will it invoke the Constructor of String(String)?
String str2 = new String("test");//Will it invoke the Constructor of String(String)?
String str3 = str; //Which Constructor will it invoke? Or str3 only reference to str and "test" without being constructed?
String str4 = "test";//Which Constructor will it invoke? Or str4 only reference to str and "test" without being constructed?
String strnew = new String("testnew");//Will this expression create "testnew" in String Constant Pool …Run Code Online (Sandbox Code Playgroud)