In Java, the String would have different usage.
Example:
public class Test {public static void Main (string[] args) {String S1 = "ACCP"; String s2 = "ACCP"; String s3 = new String (S1), if (S1 = = s2) {System.out.print ("true,");} else {System.out.print ("false,");} if (S1 = = S3) {System.out.println ("true"),} else {System.out.println ("false");}}} /* Output:true, false*/
For statement String S1 = "ACCP";
Inside Java, this statement is translated into the following steps:
- First define an object reference variable named S1 to the String class: string S1;
- Look in the stack for an address with a value of "ACCP".
If not, open an address with a literal value of "ACCP", create a new object o for the string class, point the string value of O to the address, and note the referenced object o next to this address in the stack.
If you already have an address with a value of "ACCP", look for the object o and return the address of O.
So when the value of the variable that you want to create is present in the stack, it is actually referencing the address in the stack that already exists, not the new one.
- Point S1 to the address of the object o.
String S1 = "ACCP"; Its string value is a reference to the data in the existing stack! So the reference address of S1 and S2 is the same, and the S1==S2 is true.
and string s3 = new String (S1); is to create an object, that is, to create a new reference, so the S3 reference address is newly created, and of course the reference address of the S1 is different. So S1 = = S3 is false.
= = is the reference address of the comparison object, if you want to compare the value of the object with equal!
Java NULL String