How many string objects will be created in memory?

How many string objects will the following code create?

String s=""; s+=new String("a"); s+="b"; 

I had this question on the exam. I want to know the correct answer. I said 2 objects.
An object from the pool containing "," b "and an object created by a new line (" a ");

+6
source share
4 answers

I will answer another, clearer question: how many instances of String are involved in the following code fragment:

 String s=""; s+=new String("a"); s+="b"; 

And answer 6:

  • empty string literal: "" ;
  • String literal "a" ;
  • copy of the string literal "a": new String("a") ;
  • A string created by concatenating s and a copy of "a" ;
  • String literal "b"
  • String created by concatenating s and "b" .

If you assume that three string literals are already created by previously loaded code, the code snippet creates 3 new String instances.

+9
source

String s = "";

does not create objects.

s + = new line ("a");

creates five objects. new String , StringBuilder and its char [] and the resulting string and char []

S + = "b";

creates four objects: StringBuilder and its char [], and string and char []

So, I get a total of nine objects, of which three String objects

Note. You can be sure that "" is already loaded, as it appears in many system classes, including ClassLoader and Class.

Lines "a" and "b" may or may not be considered new lines for the purpose of this question. IMHO I would not count them, because they will be created no more than once, and if this code is run only once, it is hardly important how many lines are created. It is most likely useful to know how many objects are created each time the code is executed.

+8
source

The number of objects actually created in the JITC situation is uncertain. JITC may well recognize that new String("a") is an identifier and that no intermediate s values ​​are referenced, so only StringBuilder is created. There are several potential side effects that should be mimicked in the general case (for example, if the new String() argument may be invalid), but they cannot occur with literals.

In fact, javac understood very well that the result is "ab", without any potential side effects, and simply produce a string literal of this value. (It concatenates strings in slightly less complex cases.)

0
source

Create new lines

Earlier, we promised to talk more about the subtle differences between the various ways of creating String. Let's look at a couple of examples of how you can create a String, and suppose there are no other String objects in the pool:

 String s = "abc"; // creates one String object and one reference variable 

In this simple case, "abc" will go into the pool and s will refer to it.

 String s = new String("abc"); // creates two objects, and one reference variable 

In this case, since we used the new keyword, Java will create a new String object in normal (non-empty) memory, and s will reference it. In addition, the literal "abc" will be placed in the pool.

From the SCJP Sun Certified Programmer for Java 6 Study Guide (Exam 310-065) .pdf

0
source

Source: https://habr.com/ru/post/918894/


All Articles