String Interning and String Builder

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
nerdchieftain
Posts: 5
Joined: Sun Mar 08, 2020 3:55 pm
Contact:

String Interning and String Builder

Post by nerdchieftain »

I have consistently found this explanation of String Interning:

Only strings created with the new keyword are placed on [normal] heap, not the string pool.

I find this to be vague. Perhaps we mean only String created using the String class and using the new keyword go on the heap.

Searching the internet has not improved my confusion.

Several things are clear (I think) -- feel free to correct me.
  • Any call to new String goes on the heap.
  • String literals (i.e. compile time constants) go to string pool.
  • Any methods creating a new String by using methods that modify the string (like strip(), substring(), etc), will always use the String pool.
But I am unsure about StringBuilder.toString() and StringBuffer.toString(). And then there's Object.toString().

It seems to me that whether these methods use new String() could be implementation specific. new String() may or may not be invoked.

(QUESTION) How do these functions work in regard to the String pool?

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: String Interning and String Builder

Post by admin »

Not sure what you mean by "using the String class". Every string in Java is an instance of the String class.

The rule is that if you create a string using the new keyword (of course, new keyword can only be used when you put the String class i.e. new String("argument") next to new), then that string is put on the heap instead of the string pool.

>Any methods creating a new String by using methods that modify the string (like strip(), substring(), etc), will always use the String pool.
There is no such rule, although it is very likely.

>But I am unsure about StringBuilder.toString() and StringBuffer.toString(). And then there's Object.toString().
You should be unsure because there is no rule that governs these methods. To get clarity about what a method does or returns, you should read the JavaDoc API description of that method. If the JavaDoc says it returns an interned string, then you can assume that it does. If it doesn't say anything about that, then you should not assume either way.

>It seems to me that whether these methods use new String() could be implementation specific.
Absolutely correct.

>How do these functions work in regard to the String pool?
You will have to read the JavaDoc description for that method. There is no standard rule.
If you like our products and services, please help us by posting your review here.

nerdchieftain
Posts: 5
Joined: Sun Mar 08, 2020 3:55 pm
Contact:

Re: String Interning and String Builder

Post by nerdchieftain »

Thank you for answering my questions in detail.

To reply to your question:
Not sure what you mean by "using the String class"
My point is that saying "creating a String using the new keyword" is inaccurate and possibly misleading. Definitely a poor explanation.

What we mean exactly is "use a String constructor." The fact that the String constructor uses the new keyword is a semantic detail, and has nothing to do with the rule.

For example, we previously established the following code may or may not produce a String on the String pool. Yet it does use the new keyword to create a String.

Code: Select all

new StringBuilder("starts on String pool").toString()
I think the only hard rules we have are:
Compile time constant Strings go on String pool
Strings created by String constructor do NOT go on String pool.
Any other method (even String class methods) are implementation dependent.

To illustrate the confusion:
>Any methods creating a new String by using methods that modify the string (like strip(), substring(), etc), will always use the String pool.
There is no such rule, although it is very likely.
This seems to contradict the text, which seems to say the principle is binary.
Any time you create a new string without using the new keyword, Java checks whether the same string already exists in the string pool.
Reference: "Working with String APIs":"String interning" heading, Deshmukh, Hanumant. OCP Oracle Certified Professional Java SE 11 Programmer I Exam Fundamentals)

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: String Interning and String Builder

Post by admin »

>My point is that saying "creating a String using the new keyword" is inaccurate and possibly misleading. Definitely a poor explanation.
Actually, whether you are using a constructor or not is not always clear (as you observed with several methods). So, I think the usage of the new keyword is a clear giveaway that a new non-interned string is being created. If you see "new" in your code, you know what it means. That statement is not stating a programming language rule but a way to determine what is happening in a given situation and I, personally, think it is fine. But since you think it is inaccurate and misleading, I will bring this to the author's attention.

BTW, In your example of new StringBuilder, you are using a new keyword to create a StringBuilder and not to create a String.

2.
Any time you create a new string without using the new keyword, Java checks whether the same string already exists in the string pool.
This statement is actually correct. Among the ways to create a string, you seem to be including invoking a method of some class that returns a String! If you do that then there will be infinite ways to create a String. How that method creates a String obviously depends on the code written in that method (which is what I also wrote in my response above). That is clearly not what this section is referring to. The statement is talking about the ways in which a String is created i.e. the three ways discussed in the previous Section 5.1.2.

Overall, I think you are taking each statement in isolation, which works only if you are going through a formal specification and not if you are going through explanatory text. If you go through section 5.1.2 before reading 5.1.3, it is clear what it is trying to explain.

Even so, I will bring this to the author's attention. The whole point of the book is to make stuff easier for the reader to understand and since you were confused by this, obviously, there is scope for the text to be improved.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 48 guests