3

The first code is shown below:

String s = "hello";

The second code is shown below:

String s = new String("hello");

Question: Are the two codes invoking the same constructor of String(char[])?

String has a private final field char value[]. Why is it set to a final field? Such we will create a new String every time we invoke change the value of string. What is the purpose to set the field char value[] to final?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Ryan
  • 213
  • 1
  • 8
  • `What is the purpose to set the field char value[] to final?` -- it makes the string immutable. – Braj May 07 '14 at 19:24
  • [String Constant Pool](http://stackoverflow.com/questions/23505952/java-strings-immutability/23505989#23505989) comes into picture in first case. – Braj May 07 '14 at 19:29
  • Not at all. Visual makes more sense to remember things. – Braj May 07 '14 at 19:58

4 Answers4

4

Question: Are the two codes invoking the same constructor of String(char[])?

No, absolutely not.

The first form bakes the the string content directly into the bytecode, then uses the ldc (load constant) instruction to load it from the string pool.

Importantly, every occurrence of the string constant "hello" will refer to the same object.

Compare that with your second form, which will create a new string every time you execute the statement.

As to the details of the field being final: it's a private field anyway, so you shouldn't care. But it's final because Java strings are immutable - there's no reason why you'd want to change the value of it. (In itself that isn't enough to enforce immutability; anyone with access to that char[] can modify the contents of the array.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

I'm sure this is answered elsewhere, but I'm too lazy to look it up --

When you declare a "string literal" in Java, that literal gets allocated as a Java String object during class loading of the containing class. (And, incidentally, the String is "interned".)

So when you say String s = "Hello";, you're merely assigning a reference to the pre-existing String object to the variable s, and no matter how often in the program you do that assignment, you do not create any more instances of the String.

new String, on the other hand, always creates a new String object.

The char[] array in the String object is where the actual value of the String is stored.

(Note that a String is immutable -- you cannot change it once created. When you assign a new value to as existing String reference, you are assigning a new object, not updating the existing String object.)

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
0
Question: Are the two codes invoking the same constructor of String(char[])?

You are assuming that "hello" is a char[], but it is not, it is a String. You can invoke String methods to it, like "hello".charAt(0)

So in:

String s = "hello";

no constructor is invoked. "hello" is already a String and is assigned to the variable s.

And in

String s = new String("hello");

the constructor String(String) is invoked.

For your second question: value is final because in Java, Strings are immutable.

Cyrille Ka
  • 15,328
  • 5
  • 38
  • 58
0
String s = "hello";

s is a reference to a String Litteral

To quote the referenced JLS section about String litterals:

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

String s = new String("hello"); 

Now this s is a reference to a String object you are creating by calling String constructor with a single String argument.

Are the two codes invoking the same constructor of String(char[])?

No, there isn't a call within this constructor to the overloaded constructor you are referring to.

String has a private final field char value[]. Why is it set to a final field?

So that it cannot be changed once set by the constructor. This is one the Steps needed for String to serve as immutable object.

Community
  • 1
  • 1
dimitrisli
  • 20,895
  • 12
  • 59
  • 63