简体   繁体   English

System.out.println中使用的字符串是否也会创建新的不可变对象?

[英]Do strings used in a System.out.println also create new immutable objects?

So I'm studying for the SCJP from the Kathy Sierra book. 所以我正在从Kathy Sierra的书中学习SCJP。 In the chapter for strings, this is a question: 在字符串一章中,这是一个问题:

String s1 = "spring ";
String s2 = s1 + "summer ";
s1.concat( "fall ");
s2.concat(s1);
s1 += "winter";
System.out.println(s1+" "+s2);
---------------------------
What's the output, and how many string objects and ref variables are created?

The output is spring winter spring summer and there's two reference variables, that's fine. 输出是春冬春夏,有两个参考变量,没关系。

Then it says there are eight string objects created (spring, summer, spring summer... etc) INCLUDING the ones that are lost due to nothing referencing them. 然后它说创建了八个字符串对象(春天,夏天,春天......等)包括由于没有引用它们而丢失的那些。

However, it does not include anything from the last sysout. 但是,它不包括最后一个sysout中的任何内容。

My question is, in the last line of code, since s1 and s2 are being concat with a space, doesn't this also create new objects? 我的问题是,在最后一行代码中,由于s1和s2正在与空格连接,这是否也会创建新对象? Or will it simply be passed to the string buffer for display, and not create new objects? 或者它只是传递给字符串缓冲区进行显示,而不是创建新对象?

This is obviously very basic and I looked elsewhere but nothing directly answered this. 这显然是非常基本的,我看了别处,但没有直接回答这个问题。 From my understanding, it should create new objects there too, but I need to be sure for the exam! 根据我的理解,它也应该在那里创建新的对象,但我需要确保参加考试! Thoughts? 思考?

Thanks, in advance! 提前致谢!

My question is, in the last line of code, since s1 and s2 are being concat with a space, doesn't this also create new objects? 我的问题是,在最后一行代码中,由于s1和s2正在与空格连接,这是否也会创建新对象?

Yes, it creates a 10th string. 是的,它创建了第10个字符串。

Note that this piece of code in itself only necessarily creates 5 strings - if you run it several times in the same JVM, it will create 5 new strings each time you call it. 请注意,这段代码本身只需要创建 5个字符串 - 如果在同一个JVM中多次运行它,每次调用它时都会创建5个新字符串。 The string literals won't create a new string each time the code runs. 每次代码运行时,字符串文字都不会创建新字符串。 (The same string object is reused for "spring " each time, for example.) (例如,每次重复使用相同的字符串对象为"spring " 。)

There are 10 strings in the code you've given: 您给出的代码中有10个字符串:

  • 5 literals: "spring ", "summer ", "fall ", "winter " and " " 5个文字:“春天”,“夏天”,“秋天”,“冬天”和“”
  • 5 concatenation results: s1 + "summer" , s1.concat("fall ") , s1 + winter (with compound assignment) and s1 + " " + s2 . 5个连接结果: s1 + "summer"s1.concat("fall ")s1 + winter (带复合赋值)和s1 + " " + s2

As I've just commented, a string literal appearing in code doesn't always involve a separate string. 正如我刚才所评论的那样,代码中出现的字符串文字并不总是涉及单独的字符串。 For example, consider: 例如,考虑:

String x = "Foo" + "Bar";

You might expect that to involve three string objects - one for each of the literals, and one for the concatenation. 您可能希望涉及三个字符串对象 - 一个用于每个文字,一个用于连接。 In fact, it only involves one, because the compiler performs the concatenation at compile-time, so the code is effectively: 实际上,它只涉及一个,因为编译器在编译时执行连接,所以代码是有效的:

String x = "FooBar";

I'll anwser to another, clearer question: how many String instances are involved in the following code snippet: 我将回答另一个更清晰的问题:以下代码片段中涉及多少个String实例:

String s1 = "spring ";
String s2 = s1 + "summer ";
s1.concat( "fall ");
s2.concat(s1);
s1 += "winter";
System.out.println(s1+" "+s2);
  1. String literal "spring" 字符串文字“春天”
  2. String literal "summer" 字符串文字“夏天”
  3. Concatenation of s1 and "summer" 串联s1和“夏天”
  4. String literal "fall" 字符串文字“秋天”
  5. Concatenation of s1 and "fall" s1和“fall”的连接
  6. Concatenation of s2 and s1 s2和s1的连接
  7. String literal "winter" 字符串文字“冬天”
  8. Concatenation of s1 and "winter" 串联s1和“冬天”
  9. String literal " " 字符串字面量 ” ”
  10. Concatenation of s1 and s2 in sysout sysout中s1和s2的连接

So 10 in total. 总共10个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM