AbhiS
AbhiS

Reputation: 23

How is Java String constructor implemented?

I was going through Java String source code, found a CTOR where I have some doubts:

public String(String original) {
    this.value = original.value;
    this.hash = original.hash;
}

I understood that Original is acting as literal String (with double quotes) but not able to understand how is java/jvm computed original.value as char Array. What is "value" over here?? if value is char Array then how .value function/Field is calculated???

Upvotes: 2

Views: 697

Answers (3)

Joop Eggen
Joop Eggen

Reputation: 109557

String by design holds Unicode text, so all language scripts may be combined. For that the implementation holds an array (field name value), where every char is a two byte UTF-16 value.

You encountered the one and AFAIK only silly point in the Java classes.

The shown copy constructor is senseless, as Strings are immutable objects, and they may be shared by simple assignment. It is a fossile of C++ inheritance, maybe in association with String interning.

To make a copy is senseless. This holds too for the internal char array, which indeed may be assigned by reference. (Not very consequent.)

So the following shows inexperienced java usage:

String s = new String(t);

With the newest java versions, the value of a String might actually be a byte array in some encoding, and hence the chars are lazily provided.


About String literals:

String literals are stored in a datastructure in a .class file called the constant pool. Stored is it as UTF-8 bytes. The JVM ClassLoader ensures that the string is loaded as String.

Imports of final static String constants are copied into the constant pool, and the original class may no longer appear as being imported from. Holding string constant in an other class may require manually doing a clean build, as there might no longer exist a class dependency.

Upvotes: 1

Innokenty
Innokenty

Reputation: 3293

Yes, as already mentioned in the comments, this is very simple. Since you're looking at the String class itself — it has access to its own fields. And that is where the characters this given string consists of are actually stored — in a char array. This method simply references the field by name, very basic interaction.

Upvotes: 2

Lajos Arpad
Lajos Arpad

Reputation: 76564

The docs says

Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string. Unless an explicit copy of original is needed, use of this constructor is unnecessary since Strings are immutable.

Technically the new String will get the value and hash of the original.

enter image description here

which means that this is a copy of another String.

Upvotes: 1

Related Questions