String str = new String ( "a") and String str = "a" What's the difference?

The interviewer asks

A = String "ABC";
String B = new new String ( "ABC");
these two values, A, B are equal, if all go inside with HashSet, you can put down?

A:
Analyzing (a) A == B is to false;
(B) a.equals (B) is true; because of equal value, have to fit inside HashSet, only put a

String A = "ABC"; the memory will look for a permanent-generation (constant pool), if not, together in open space in the permanent generation memory, the address of the stack pointer paid, if you already have "ABC" of memory, directly to assign the address of the stack pointer; therefore 
String str1 = "AA";
Srting str2 = "AA";
String str3 = "AA";
....
go on like this, str1 == str2 == str3; would have been equal to it, (a) == determination, (b) equals () determination; are equal, are equal because of their addresses, and therefore only have a constant pool of memory space, address all the same; 
and String str = new String ( " a "); it is an" a "configuration of the String object a String object again; new new stack together in memory, assign a pointer to the stack, it references the new configuration of the object are assigned String str. So long as the new String (), then, stack addresses are pointing to the heap of the latest new address out 

 1 public class Test1 {
 2     public static void main(String[] args) {
 3         String s1 = new String("hello");
 4         String s2 = new String("hello");
 5         System.out.println(s1 == s2);// false
 6         System.out.println(s1.equals(s2));// true
 7 
 8         String s3 = new String("hello");
 9         String s4 = "hello";
10         System.out.println(s3 == s4);// false
11         System.out.println(s3.equals(s4));// true
12 
13         String s5 = "hello";
14         String s6 = "hello";
15         System.out.println(s5 == s6);// true
16         System.out.println(s5.equals(s6));// true
17     }
18 
19 }

Test1 Detailed
s1 ~ s6 using equals () is less explanation, the value of all comparison, are true. The following explanation ==

s1, s2: out of both of which are new, each space allocated on the heap and to assign the respective address to memory s1, s2. Different address space, == comparison is false. But each stored on the heap space values are the same address in a string constant pool of objects. The Demo FIG i.e. at understandable explanation.
s3, s4 Demo explanatory supra.
s5, s6 are values in the constant pool, both of the same object point to the constant pool, which is the same address value, the result is true

 

 1 public class Test2 {
 2     public static void main(String[] args) {
 3          String s1 = "hello";
 4             String s2 = "world";
 5             String s3 = "helloworld";
 6             System.out.println(s3 == s1 + s2);// false
 7             System.out.println(s3.equals((s1 + s2)));// true
 8             System.out.println(s3 == "hello" + "world");//true
 9             System.out.println(s3.equals("hello" + "world"));//threaten 
10      }
 11 }

Detailed Test2

equals () method does not interpret the comparison, the comparison value, are equal, are true.

s1 and s2 are summed in space to open a string constant pool, then splicing, the address space is s1 and s2 after splicing address. S3 with different addresses, so the output is false.
s3 and "hello" + "world" comparison, "hello" + "world" first stitched "helloworld", then go to find whether there is a string constant pool "helloworld", there are, and therefore share a string s3 Object, then true.

Summary
1, String s = new String ( "hello") creates 2 (1) object, String s = "hello" to create 1 (0) objects.
Note: Established in brackets when there is a string constant pool objects hello!
2, if the string variables together, first open space in splicing.
3, if the string is a constant sum, it is the first increase, and then find the constant pool, if there is direct return, otherwise, it is created.

Guess you like

Origin www.cnblogs.com/linliquan/p/11259483.html