JAVA face questions String s = new String ( "xyz"); produced several objects?

Interviewer Q1: Will the String s = new String ( "xyz"); produces several objects?

For this Java interview questions, first on the old routines of code:

the StringTest {class public 
    public static void main (String [] args) { 
        String S1 = "the Hello"; 
        String s2 = "the Hello"; 
        String S3 = new new String ( "the Hello"); 
        System.out.println ( "S1 and s2 the reference address is the same: "+ (S1 == s2)); 
        System.out.println (" S1 and s2 are the same values: "+ s1.equals (s2)); 
        System.out.println (" references S1 and s3 address is the same: "+ (S1 == s3)); 
        System.out.println (" S1 and s3 are the same values: "+ s1.equals (s3)); 
    } 
}

Print results are as follows:

s1 and s2 reference address is the same: true 
s1 and s2 are the same values: true 
s1 and s3 reference address is the same: to false 
s1 and s3 are the same values: true

The above program "==" is the determination of whether two object references address the same, that is, determines whether the same object, s1 and s2 returns to true, s1 and s3 are returns false. Description s1 and s2 of the same object referenced address, s3 of the two reference is not the same address with other objects.

In order to avoid a large number of Java String object, we designed a string constant pool. Works like this, when you create a string, JVM first check whether there is a string constant pool string equal value, if there is no longer created, directly back to the reference to the string address, if not, create, and then into a string constant pool, and returns a reference to the newly created string address. Therefore, the above referenced s1 and s2 same address.

Why s3 and s1, s2 quoted string is not the same address? String s3 = new String ( "Hello"); JVM first is to find "Hello" string in the string constant pool, if you have not created a string constant, and then into the constant pool, If there is already, you do not need to create; when faced with new, but also in the memory (not a string constant pool, but in the heap) on the creation of a new string object storage "Hello", string object on the return address and the memory reference, so s3 and s1, s2 referenced string is not the same address. Memory structure is as follows:

 

Seen from FIG memory, s1 and s2 are directed string constants in the constant pool, they are compared with a memory address, and s3 points to an address of the heap, specific points of the heap should be Eden region, s1 with s3, s3 comparison with S2 are not equal, an address is not the same.

 

Understand the working principle of the String class, return to the question itself:

String in the works, it has been mentioned, new a String object, is the need to find the same value in a string literal or create a string constant, then create a String object in memory, so the String str = new String ( "xyz"); creates two objects.

 

The following two Java interview questions can be placed in the message area reply yo:

String str1 = new String ( "A " + "B"); how many objects would be created? 
String str2 = new new String ( "ABC") + "ABC"; how many objects would be created?

 

Author: the Java ant

Source: https://www.cnblogs.com/marsitman/p/11248001.html

Copyright: reproduced in the article clearly indicate the position of the author and source.   

Guess you like

Origin www.cnblogs.com/marsitman/p/11248001.html