0

In case of string literal :-

String s = "happ"

s = s.concat("y")  //line1
16
  • 1
    You know you can verify that easily using == comparison right? If a == b while a and b have been obtained through different code paths it probably means this string has been interned. Commented Sep 22, 2018 at 21:44
  • As a general rule, if it is a literal (wrapped in quotes) it is in the string pool, if it is an object created at runtime (new String("x")) that variable will not be interned. Commented Sep 22, 2018 at 21:52
  • "on heap or in string constant pool" object is stored in heap, string constant pool is just part of it for string literals (which also are objects). Anyway result of s.concat("y") will not be automatically placed in string constant pool (there is no need as string pool was created to reuse literals since they have high chance to be reused). "So here, "Birth" is in constant pool," only "Birth" literal used as argument passed to new String(...) constructor. That constructor will become separate copy of "Birth" but will not be in string constant pool. Commented Sep 22, 2018 at 21:56
  • @user10402056 note that the constant pool is also inside the heap. It's just a reserved space that doesn't get GC'ed (to my knowledge). Commented Sep 22, 2018 at 22:04
  • 1
    "If the concatenation can occur statically at compile time" is probably referring to situations like "ab"+"c" (notice it is + not concat method) then compiler will change it into "abc" which will be interned. This should interest you Comparing strings with == which are declared final in Java Commented Sep 22, 2018 at 22:14

2 Answers 2

3

In your first example:

  String s = "happ";
  s = s.concat("y");

By the time these statement have been executed1, String objects have been created in the string pool to represent (respectively) the "happ" literal, and the "y" literal.

The execution of the second statement creates a new String object that represents the string "happy". This object is NOT in the string pool.


I wanted to specifically clear this doubt on where string stores the result of concat method when operated on strings from pool vs heap.

It is created in the heap2, not the string pool. Specifically.

The ONLY method in the String API that creates objects in the string pool is String.intern(). (That includes constructors.)


1 - Note my careful choice of words here. If you are executing the statements for the first time, the creation of the objects in the string pool may have happened during the execution of the statements. Or it may have happened before. The exact timing is implementation specific. However, the JLS guarantees that it won't happen more than once for the same literal.

2 - Note that for a modern HotSpot JVM, the string pool is in the regular heap. It is not a separate space. The string pool is effectively just a (JVM private) data structure.

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

1 Comment

I know, strictly speaking, that was illegal, but I very much enjoyed pushing you over the 500K boundary. I guess I will never get there myself, but helping others to make that ... just feels good. Always a pleasure to look at your content!
1

Let's just try it out.

String s = "happ";
s = s.concat("y");

System.out.println(s == "happy"); // false
s = s.intern();
System.out.println(s == "happy"); // true

String s1 = new String("Birth");
s1 = s1.concat("day");

System.out.println(s1 == "Birthday"); // false
s1 = s1.intern();
System.out.println(s1 == "Birthday"); // true

So yeah, it just doesn't matter. Only literals are being interned here, not dynamically constructed values (unless explicitly interned).

2 Comments

s.equals("happy") is true because it is value equality but == is reference equality. For example, "a" == new String("a") is always false while "a".equals(new String("a")) is always true. Check this answer for more details: stackoverflow.com/questions/513832/…
one side-note, besides these proofs about interning a String, there are zero profit calling intern explicitly in an application...

Your Answer

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