String s = new String("abcd");
-
(Fun fact: If you have `String abcd = "Xabcd".substring(1);`, then typically (implementation dependent), `String s = new String(abcd);` will create two objects.) – Tom Hawtin - tackline Mar 04 '11 at 10:45
-
1All the answers are actually wrong as far as **objects** are concerned. The creation of a `String` includes sometimes a creation of a `char[]` containing it's data. Here it happens for the first String only (the others share the array), so you need to add 1. – maaartinus Mar 06 '11 at 01:10
2 Answers
There's one string in the intern pool, which will be reused every time you run the code.
Then there's the extra string which is constructed each time you run that line. So for example:
for (int i = 0; i < 10; i++) {
String s = new String("abcd");
}
will end up with 11 strings with the contents "abcd" in memory - the interned one and 10 copies.

- 1,421,763
- 867
- 9,128
- 9,194
-
2+1: I would argue "abcd" is created when the class is loaded so is not created when this line is executed. – Peter Lawrey Mar 04 '11 at 10:51
-
@Peter: Is it guaranteed at what point the String object is actually created? I've had a very quick skim of the JVM spec and couldn't see anything - any references would be handy. – Jon Skeet Mar 04 '11 at 11:38
-
The String literals are part of the class definition, but you are right it might not load the literal until it is needed. – Peter Lawrey Mar 04 '11 at 11:51
-
1@JonSkeet Sad to say Mr.JonSkeet, I can't understand can you explain how many strings created for this `String s = new String("abcd");` – Selva May 15 '14 at 06:15
-
@Selva: Well that will always create one new string (it's calling a constructor) and it also requires "abcd" to be in the string pool. – Jon Skeet May 15 '14 at 06:27
-
@JonSkeet If `"abcd"` is not in the string pool it will create 2 objects am I correct!!!! – Selva May 15 '14 at 06:35
-
@Selva: Yes, although *precisely* when the string is created (class load time? class initialization time? first method execution time?) is either unspecified or at least I'm not aware of it. – Jon Skeet May 15 '14 at 07:10
-
@jonSkeet Thank you, Mr. JonSkeet by your valuable explanation, my long term `String ==` doubt also cleared in good order..... – Selva May 15 '14 at 12:31
You're creating one object. The JVM will create another object behind-the-scenes because it interns the string created by the constant at class load, but that's a JVM thing (you haven't asked it to intern
). And more to the point, you can be fairly certain that having done:
String s1 = new String("abcd");
once, then
String s2 = new String("abcd");
will only create one object.
The JVM creates the the other (first) String
object at class load: The compiler puts the string in the string constants area in the .class
file. Those are read into the class's constant pool and interned when the class is loaded.
So when that line of code executes, a single String
is created. But the fact of having that line in the class creates two: One for the constant which is created when the class is loaded, and one for that line of code.

- 1,031,962
- 187
- 1,923
- 1,875