0

I am trying to concat three strings using the '+' operator

String s = "a"+"b"+"c";

can somebody tell me how many objects will be created in the string constant pool. Also, in case there are more strings then, how many objects will be there in the string constant pool. for ex:-

String s1 = "a"+"b"+"c"+"d"+"e"+"f"+"g"..........100 objets
Vishal Sharma
  • 192
  • 1
  • 3
  • 17
  • 7
    Just one. These are compile-time constants, so they are exactly equivalent to `"abc"` and `"abcdefg"`. – Andy Turner Jun 21 '19 at 06:53
  • Not sure about an exact duplicate, but answers here are relevant : https://stackoverflow.com/questions/11989261/does-concatenating-strings-in-java-always-lead-to-new-strings-being-created-in-m – Arnaud Jun 21 '19 at 06:54
  • I am quite interested by this, what if this is in a method `concatenate(String a, String b, String c){ return a + b + c;}` ? – AxelH Jun 21 '19 at 06:54
  • 1
    @AxelH that's an entirely different question, and one perhaps best answered by [Peter Lawrey](https://vanillajava.blogspot.com/2015/10/common-misconception-how-many-objects.html?m=1). – Andy Turner Jun 21 '19 at 06:54
  • The method is just here to remove the compile time constants, @AndyTurner . The part about the string-pool is still the same... Thanks for the link! – AxelH Jun 21 '19 at 06:56
  • 1
    @AxelH the string pool isn't involved if they aren't constants. – Andy Turner Jun 21 '19 at 06:57
  • Maybe someone could dig up the JLS link, it explains it pretty well. – Zabuzard Jun 21 '19 at 06:57
  • Pool is an improving memory consumption feature : it avoids create multiple times String with same values. So why do you want the JVM adds in the pool distinct string values while a single one is referencable here and potentially latter ? – davidxxx Jun 21 '19 at 06:58
  • 2
    @Zabuza https://docs.oracle.com/javase/specs/jls/se12/html/jls-3.html#jls-3.10.5 "A long string literal can always be broken up into shorter pieces and written as a (possibly parenthesized) expression using the string concatenation operator + (§15.18.1)." – Andy Turner Jun 21 '19 at 06:58
  • 1
    @AndyTurner I am dumb! I can't think today, I should not be allowed to post on a friday!! – AxelH Jun 21 '19 at 06:59
  • @Andy Turner No completely. `String.intern()` allows to act on the pool too. – davidxxx Jun 21 '19 at 07:00
  • 1
    @davidxxx that would be relevant if anybody was invoking it. – Andy Turner Jun 21 '19 at 07:02
  • 1
    @Andy Turner. I answered to "the string pool isn't involved if they aren't constants". – davidxxx Jun 21 '19 at 07:03

1 Answers1

5

Only one string is created in total, also for the string pool.

What you have here are all just literals. The JLS describes that a constant string expression like this is already completely computed at compile-time. So while your .java source file still contains the string concatenations, the .class file does not anymore, its just directly the result already.

See JLS 3.10.5. String Literals :

Strings concatenated from constant expressions (§15.28) are computed at compile time and then treated as if they were literals.

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, using the method String.intern (§12.5).

Zabuzard
  • 25,064
  • 8
  • 58
  • 82