0

I know that line one is the same as line two in the code below

  String s1 = new String("a") + new String("b");
  String s2 = new Stringbuilder().append("a").append("b").toString();

But I'm unsure whether either version will put the resulting string "ab" into the string constant pool.

Does new String("ab") create a new object or does it just return "ab" from the constant pool?

  String s3 = new String("ab");
rustyx
  • 80,671
  • 25
  • 200
  • 267
cole
  • 11
  • 1
  • See this https://stackoverflow.com/questions/15746018/when-does-stringbuffer-adds-strings-to-the-string-pool – Siddhivinayak Shanbhag Dec 10 '18 at 09:12
  • @shanbhagsv The accepted answer there is dated 2013. It could be outdated, considering there were some changes in String pool. – Teddy Dec 10 '18 at 09:16
  • String s1 = "Pool"; String s2 = new StringBuilder().append("Po").append("ol").toString(); System.out.println(s1==s2); //False so nothing was changed from 2013 – Akceptor Dec 10 '18 at 09:18
  • 1
    @Teddy what changes in the String pool are relevant here? a `new` will always create a new Object, compile-time constant Strings are still added to the constant pool and a StringBuffer still does not `intern` anything. – Thilo Dec 10 '18 at 09:18
  • @Thilo I agree the change is only distantly related. It is indirectly related. In those days we needed to worry about constant pool because it was not GCed. Now the question is immaterial because constant pool is GCed ;) – Teddy Dec 10 '18 at 09:24
  • @shanbhagsv //JDK1.7 String s=new String("q"); System.out.println(s.intern()==s); String s3=new String("y")+new String("u"); System.out.println(s3.intern()==s3); it is not corresponding to what high voted said.For the first result,it is true,but the seccond is false.when i use new operator to create string object,constant pool has a string object corresponding to string object.But the seccond resule show no string was put into constant pool – cole Dec 10 '18 at 09:30
  • @cole: There is no StringBuilder in there. If you use `+` (as you should), the compiler can be clever about it. In fact, it has gotten a lot cleverer in recent releases. No more direct calls to StringBuilder, instead some deep invoke_dynamic/code-generation magic. https://stackoverflow.com/questions/46512888/how-is-string-concatenation-implemented-in-java-9 – Thilo Dec 10 '18 at 09:31
  • @cole Why does it matter? Constant Pool is GC'ed now. So, you don't have to worry about it. And, intern() is widely recognised as a bad practice. – Teddy Dec 10 '18 at 09:43
  • @Thilo the high voted say only if intern() was invoked,string will put into constant pool.It contradict with the first result.Cause intern() return reference alredy stored in costant pool – cole Dec 10 '18 at 09:43
  • The first result has a compile-time String constant `"q"`. That is the one that will be in the constant pool, NOT the `new String("q")`. All constants are already "interned" (if you will) by the compiler before your program even runs. – Thilo Dec 10 '18 at 09:50

1 Answers1

1

To answer your question whether it will put String "ab" into string constant pool when stringbuilder.toString() was used to return a new string

No string builder will not create any object inside string constant pool

Vicky
  • 1,135
  • 1
  • 17
  • 37