Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is need of String s = new String("abc") when we can create String s = "abc"

Tags:

java

string

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).

like image 397
hard coder Avatar asked Oct 17 '15 08:10

hard coder


2 Answers

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 " "?

like image 90
Suresh Atta Avatar answered Oct 09 '22 06:10

Suresh Atta


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.

like image 35
user207421 Avatar answered Oct 09 '22 06:10

user207421