Which constructor of the String class is called when creating a String object using a string literal

which string class constructor is called when a string object is created using a string literal.

Example:

String str = "hello"; 

In this case, which constructor of the string class will receive?

+6
source share
3 answers

When the JVM loads a class containing a string literal

 String str = "hello"; 

it reads a string literal from a UTF-8 encoded class file and creates a char array from it

 char[] a = {'h', 'e', 'l', 'l', 'o'}; 

then it creates a String object from this char array using the String constructor (char [])

 new String(a) 

then the JVM places the String object in the String pool and assigns a reference to this String object to the str variable.

+11
source

According to JVM 5.1 specification

To obtain a string literal, the Java virtual machine checks the sequence of code points specified by the CONSTANT_String_info structure.

  • If the String.intern method was previously called on an instance of the String class containing a sequence of Unicode code points identical to the sequence specified by the CONSTANT_String_info structure, then the result of the string literal is a reference to this same instance of the String class.

  • Otherwise, a new instance of the String class is created containing the sequence of Unicode code points specified by the CONSTANT_String_info structure; the reference to this instance of the class is the result of a string literal. Finally, the intern method of the new String instance is called.

From here from this paragraph we can conclude that the constructor can be:

String (int [] codePoints, int offset, int count)

Selects a new line containing characters from the submatrix of the array of arguments to the array of Unicode characters. The offset argument is the index of the first code point of the subarray, and the argument count indicates the length of the subarray. The contents of the subarray are converted to characters; subsequent modification of the int array does not affect the newly created string.

Or there might even be a private constructor :

 // Package private constructor which shares value array for speed. String(int offset, int count, char value[]) { this.value = value; this.offset = offset; this.count = count; } 
+3
source

The Java string contains an unchangeable Unicode character sequence. Unlike C / C ++, where a string is just an array of char, Java String is an object of class java.lang. Java String, however, is special. Unlike the regular class:

A string is associated with a string literal in the form of double-quoted texts, such as "Hello, world!" You can assign a string literal directly to a String variable instead of calling the constructor to create an instance of String.

 String s1 = "Hello"; // String literal String s2 = "Hello"; // String literal String s3 = s1; // same reference String s4 = new String("Hello"); // String object String s5 = new String("Hello"); // String object 

How to initialize a string using ""?

+1
source

Source: https://habr.com/ru/post/949457/


All Articles