0

When I define a StringBuffer variable with new, this string is not added to the String pool, right?

Now, when I define another StringBuffer but not with new, I define it as StrPrev.append("XXX") suddenly it is.(or so says my college teacher). Why is that? What makes this string to suddenly become a string-pool string?

5
  • All string constants are going into the pool like "XXX" Commented Apr 1, 2013 at 15:14
  • 2
    StringBuffer never adds to the string pool. In your question, the literal string "XXX" is recognized by the compiler at compile time and is added to the string pool. Commented Apr 1, 2013 at 15:16
  • You are confusing Strings (which may or may not be in the string pool) and StringBuffer (which is a way to build strings without having the intermediate result as a string object). Commented Apr 1, 2013 at 15:16
  • I'm sorry but I did not understand. StringBuffer str=new StringBuffer("abcd") -> abcd is not in the string pool. now, StringBuffer str2=str.append("efgh") -> now abcdefgh is the string they both refers to and this string is in the string pool. (by my teacher). Why Is That?? Commented Apr 1, 2013 at 15:23
  • Your teacher is wrong, or you're misunderstanding. Whenever you have a string literal -- anything between " -- that string goes into the literal pool. So "abcd" and "efgh" will both be in the string pool, but "abcdefgh" won't be. Commented Apr 1, 2013 at 16:32

4 Answers 4

3

When I define a StringBuffer variable with new, this string is not added to the String pool, right?

Creating a StringBuffer does not create a String at all.

Now, when I define another StringBuffer but not with new, I define it as StrPrev.append("XXX") suddenly it is.

This is totally confused:

  • When you call strBuff.append("XXX") you are NOT defining a new StringBuffer. You are updating the existing StringBuffer that strBuff refers to. Specifically, you are adding extra characters to the end of the buffer.

  • You only get a new String from the StringBuffer when you call strBuff.toString().

  • You only add a String to the string pool when you call intern() on the String. And that only adds the string to the pool if there is not already an equal string in the pool.

  • The String object that represents the literal "XXX" is a member of the string pool. But that happens (i.e. the String is added to the pool) when the class is loaded, not when you execute the append call.

(If you teacher told you that StringBuffer puts strings into the Java string pool, he / she is wrong. But, given your rather garbled description, I suspect that you actually misheard or misunderstood what your teacher really said.)

Sign up to request clarification or add additional context in comments.

4 Comments

my teacher said after I define the first StringBuffer and the second, then the text (that was changed) is in the String Pool. is it false?
If your teacher did say that, it is false. But I think your teacher did not say that.
Maybe a pool vs buffer confusion?
@MarkRotteveel - that is a plausible explanation.
1

"XXX" in StrPrev.append("XXX") is a string literal that is interned at class loading time (class loading time of the class that contains the code).

"XXX" is not added to the pool by the StringBuffer.

From the JLS section 3.10.5:

Moreover, a string literal always refers to the same instance of class String. This is because string literals - or, more generally, strings that are the values of constant expressions (§15.28) - are "interned" so as to share unique instances

From the JLS section 12.5:

Loading of a class or interface that contains a String literal (§3.10.5) may create a new String object to represent that literal. (This might not occur if the same String has previously been interned (§3.10.5).)

Comments

0

buf.append("XXX") followed by buf.toString(), and then returning the string to the pool. With the pool in place, only one StringBuffer object is ever allocated.

Comments

0

Actually your teacher is referring to XXX . which goes to StringPool because all string literals written in java program goes to StringPool while execution...

1 Comment

downvoter pls care to leave some comment before leaving away from here...Or I consider your this downvote to be mere fun..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.