例如
String s = "Hello" + " World";
Run Code Online (Sandbox Code Playgroud)
我知道池中有两个字符串"Hello"和"World"但是,"Hello World"进入字符串池吗?
如果是这样,怎么样?
String s2 = new String("Hola") + new String(" Mundo");
Run Code Online (Sandbox Code Playgroud)
每种情况下池中有多少个字符串?
我的问题是关于Java处理字符串文字的方式.从Java语言规范(JLS)可以清楚地看出,字符串文字是隐式实现的 - 换句话说,就是在堆的String常量池部分中创建的对象,与调用时创建的基于堆的对象形成对比new String("whatever").
似乎与JLS所说的不一致的是,当使用字符串连接创建一个新的String时,使用一个转换的常量String类型,根据JLS应该将其视为一个常量String,显然JVM正在创建一个新的String对象而不是隐式实现它.我感谢有关此特定行为的任何解释以及这是否是特定于平台的行为.我在Mac OSX Snow Leopard上运行.
public class Test
{
public static void main(String args[])
{
/*
Create a String object on the String constant pool
using a String literal
*/
String hello = "hello";
final String lo = "lo"; // this will be created in the String pool as well
/*
Compare the hello variable to a String constant expression
, that should cause the JVM to implicitly call String.intern()
*/
System.out.println(hello == ("hel" + …Run Code Online (Sandbox Code Playgroud) public static void main(String[] args){
one();
two();
three();
}
public static void one() {
String s1 = "hill5";
String s2 = "hill" + 5;
System.out.println(s1==s2);
}
public static void two() {
String s1 = "hill5";
int i =5;
String s2 = "hill" + i;
System.out.println(s1==s2);
}
public static void three() {
String s1 = "hill5";
String s2 = "hill" + s1.length();
System.out.println(s1==s2);
}
Run Code Online (Sandbox Code Playgroud)
输出是
true
false
false
Run Code Online (Sandbox Code Playgroud)
字符串文字使用实习过程,然后为什么two()而且three()是假的.我可以理解three()但是two()不清楚.但是需要对这两种情况进行适当的解释.
有人可以解释正确的理由吗?
public class Foo {
public static void main(String[] args) {
foo();
bar();
}
public static void foo() {
String s = "str4";
String s1 = "str" + s.length();
System.out.println("(s==s1)" + (s1 == s));
}
public static void bar() {
String s = "str4";
String s1 = "str" + "4";
System.out.println("(s==s1)" + (s1 == s));
}
}
Run Code Online (Sandbox Code Playgroud)
OUTPUT
(s==s1)false
(s==s1)true
Run Code Online (Sandbox Code Playgroud)
在String s1 ="str"+ s.length(); s1 = str4的值但在double equal(==)检查期间在下一个sysout语句中结果为false
*/
在这段代码中,每次我调用 goodMethod() 时,它都会使用在堆空间中创建的唯一对象和静态字。
我的问题是:当我调用 badMethod() 时,是否会在每次调用此方法时在堆空间中创建一个新的 String 对象?因此,如果我调用我的方法 1_200_000 次,它是否会在堆空间中创建 1_200_000 字符串对象?
毫无疑问,第一种方法更好(为了代码的可读性和可维护性)。我只是在这里询问内存中创建的对象数量
谢谢
我在谷歌上阅读了很多关于此的内容,但没有找到带有论点或证据的回复。如果你知道我如何测试这个,也请谢谢分享。
public class Main {
private static final String HELLO = "hello";
private static final String WORLD = "world";
public static void main(String[] args) {
for (int i = 0; i < 1_200_000; i++) {
goodMethod();
badMethod();
}
}
private static void goodMethod(){
System.out.println(HELLO);
System.out.println(WORLD);
}
private static void badMethod(){
System.out.println("hello");
System.out.println("world");
}
}
// an other example
Map<String, Object> map = new HashMap<>();
map.put("myKey", xxx.getYYY()); …Run Code Online (Sandbox Code Playgroud) String s5="Ram";
String s6="Ram";
System.out.println(" s5==s6 is " + s5==s6); // false
System.out.println(s5==s6); // true
Run Code Online (Sandbox Code Playgroud)
为什么第一行是false第二行true总是?