6

As per my understanding

String s = new String("abc") will create two objects-

  1. one in String constant pool (if "abc" is not already in constant pool)
  2. one in Heap memory

Although more than understandings exist about how many objects will actually be created and where.

Anyway I have following questions -

  1. Why is it not covered in java docs of String class about how many objects will be created and where?
  2. Why new String(String) is provided anyway in String class provided Strings are immutable?.Also can it can be assumed that all strings, created by either String s = "abc" or String s = new String("abc"), will be available in String constant pool?
  3. The String literals used in creating or appended in StringBuilder or StringBuffer,do they also go in String constant pool or they remain in heap memory only.

Edit

java docs for intern() method clearly talks about constant pool but nothing is said like that in String(String).

hard coder
  • 5,449
  • 6
  • 36
  • 61
  • WRT "the need": It's close to 0. I don't remember using it in the past few years. Maybe for testing that there is no flawed `==` comparison or other odd reasons like wanting a safe lock object that also prints itself. Automatic code analysis may even warn you about it. – zapl Oct 17 '15 at 08:41
  • @zapl - historically, the constructor had a real purpose - to make a copy with a smaller char array. see http://stackoverflow.com/q/16123446/2158288 – ZhongYu Oct 17 '15 at 15:58

2 Answers2

7

Why is it not covered in java docs of String class about how many objects will be created and where?

It is covered in Docs of String

The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class. Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared. For example:

 String str = "abc";

is equivalent to:

 char data[] = {'a', 'b', 'c'};
 String str = new String(data);

And from the Java language specification

A String object has a constant (unchanging) value.

String literals (§3.10.5) are references to instances of class String.

And from JSL # 3.10.5. String 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.

.

Why new String(String) is provided anyway in String class provided Strings are immutable?.Also can it can be assumed that all strings, created by either String s = "abc" or String s = new String("abc"), will be available in String constant pool?

Since String is object the valid way of declaration is

String s = new String("abc");

But where String s = "abc"; is designed for other reasons

The designers of Java decided to retain primitive types in an object-oriented language, instead of making everything an object, so as to improve the performance of the language.

Since it is the most useful class For performance reason, Java's String is designed to be in between a primitive and a class.

The String literals used in creating or appended in StringBuilder or StringBuffer,do they also go in String constant pool or they remain in heap memory only.

Consider the example

StringBuilder sb = new StringBuilder("abc");

The literal "abc" available in constant pool and the object sb created in heap.

Give a shot to read my old answer : How can a string be initialized using " "?

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • 2
    Downvoted. Doesn't really answer the question and is erroneous. Creating a String from a char array is not really equivalent to using a constant. Example: ```String a = "x"; String b = "x"; char[] ca = {'x'}; String c = new String(ca);``` Variables a and b now point to the same object, but c points to a different object. Also, Java Strings are objects. They are not "between" primitive types and reference types. Yes, you can use the + operator, but that's just syntactic sugar and has nothing to do with "performance reasons". The stuff about "valid way of declaration" doesn't make sense. – jcsahnwaldt Reinstate Monica Oct 17 '15 at 09:26
  • 1
    Will try to answer your questions: 1) I never said that it's equal to char array. So from your point of, the official API docs linked or wrong ? 2) Java strings are Object's. No doubt about it. Just it's look a like a primitive type of declaration and for the constant pool (performance optimisation by language). 3)`but that's just syntactic sugar and has nothing to do with "performance reasons"`. I never mentioned that in my answer. 4) Valid way id about the object declaration like any other object unlike literal style. Hope that clears. – Suresh Atta Oct 17 '15 at 09:48
  • 1
    1) Yes, strictly speaking, the official API doc is wrong. The example in the doc is meant to illustrate immutability, and in that respect, the strings are equivalent, but in other ways, they aren't. 2) I don't understand. 3) I assumed you felt that strings are like primitives because they can be operands. Sorry. 4) I don't understand. – jcsahnwaldt Reinstate Monica Oct 17 '15 at 10:55
  • 2
    @JonaChristopherSahnwaldt That is fine. – Suresh Atta Oct 17 '15 at 11:12
1

String s = new String("abc") will create two objects:

  • one in String constant pool (if "abc" is not already in constant pool)

No. It is in the constant pool. It was put there by the compiler.

  • one in Heap memory

Correct.

Although more than understandings exist about how many objects will actually be created and where.

Many misunderstandings: only one correct understanding.

Why is it not covered in java docs of String class about how many objects will be created and where?

The premiss of the question is not correct. It is covered. Not possibly in the exact form 'how many strings are created', but as the question has been asked millions of time in the last 20 years the answer isn't exactly a secret. Or shouldn't be.

Why new String(String) is provided anyway in String class provided Strings are immutable?.

So you can create a new one.

Also can it can be assumed that all strings, created by either String s = "abc" or String s = new String("abc"), will be available in String constant pool?

No. Only string literals and the result of String.intern() are in the constant pool. And that is documented as well.

The String literals used in creating or appended in StringBuilder or StringBuffer,do they also go in String constant pool or they remain in heap memory only.

All String literals are placed in the constant pool by the compiler.

Community
  • 1
  • 1
user207421
  • 305,947
  • 44
  • 307
  • 483
  • To your first point, `No. It is in the constant pool. It was put there by the compiler.` How could compiler? As per java docs of `intern()` method `A pool of strings, initially empty, is maintained privately by the class String.` Clearly Object creation is a process of run time and in actual JVM instance. And to your 5th point `So you can create a new one.` Why would I create a new string if I already have that immutable String? – hard coder Oct 19 '15 at 07:25
  • 1
    @sarvesh It is put into the constant pool of the .class file by the compiler, and the string literals in the constant pools of all the .class files are consolidated into the runtime string pool by the classloader. – user207421 Oct 19 '15 at 09:26
  • This looks convincing. Thanks. – hard coder Oct 19 '15 at 09:29