Baidu's interviewer asked
String a= "ABC";
String B=new string ("ABC");
These two values, a, B are equal, if all go inside HashSet, can put down?
A: A==b, but A.equals (B) is equal, because it is equal, so all go to hashset inside, can only put a
This problem involves the concept of a constant pool,
Q: What is the difference between string Str=new string ("a") and string str = "a"?
For:
The difference between = = and Equals ():
- = =: Compare the reference type comparison is the address value is the same
- equals: Comparing reference types By default is also the same as comparing address values, and the string class overrides the Equals () method to compare whether the content is the same.
String str = "a"; memory will go to find the permanent generation (Chang), if not, in the permanent generation in a memory space, the address to the stack pointer, if already have "a" memory, directly to the stack pointer to the address;
So
String str1= "AA";
srting str2= "AA";
String str3= "AA";
....
This goes on, STR1==STR2==STR3; it will always be equal, (a) = = Judgment, (b) equals (), all equal, because they are equal, so only one memory space in the constant pool, the address is all the same;
String str = new String ("a"), which constructs a string object from the string object of "a", and the memory in the heap from new new, and assigns the pointer to the stack pointer.
Assigns a reference to the newly constructed string object to Str. So whenever it is new String (), the address in the stack is the address in the latest new heap.
(a) "= =" "is a judgment of the address, of course, not the same;
(b) The equals,string type overrides the Equals () method to determine whether the values are equal and are obviously equal, so equals is equal;
This is the equals of string rewrite:
* @see #compareTo(String)
* @see #equalsIgnoreCase(String)
*/
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String) anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
The following is a diagram of the memory:
public class StringDemo2 {
public static void main (String [] args) {
String s1 = new String ("hello");
String s2 = "hello";
System.out.println (s1 == s2); // false
System.out.println (s1.equals (s2)); // true
}
}
**operation result:**
> false
> true
Code explanation
- First, the stack is entered through the main () method.
- Then in the stack to define an object S1, go to the heap to open up a memory space, the memory space reference to S1, "hello" is a constant, and then go to the string constant pool to see if there is a Hello string object, no words allocated a space to store Hello, and put its space address into the new space in the heap.
- Define an object S2 in the stack, and then go to the string constant pool to see if there is a "hello" string object, with the address of "hello" directly assigned to S2.
- That is, the space allocated in the heap is stored in the S1, and the space allocated in the heap is the address value of the space in the string constant pool where the allocated space holds "Hello". In S2, there is the address value of the space in the string constant pool where the allocated space holds "Hello".
- The output is false because the S1 is different from the address stored in the S2. Because the class string overrides the Equals () method, it compares the value of the reference type to equality, so the output is true. That is, the result is false, true.
Demo1
public class StringDemo1 {
public static void main(String[] args) {
String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1 == s2);// false
System.out.println(s1.equals(s2));// true
String s3 = new String("hello");
String s4 = "hello";
System.out.println(s3 == s4);// false
System.out.println(s3.equals(s4));// true
String s5 = "hello";
String s6 = "hello";
System.out.println(s5 == s6);// true
System.out.println(s5.equals(s6));// true
}
}
Demo1 detailed
S1~S6 is not explained by comparison with Equals (), both are values that are compared and are true. The following explanation = =
- S1, S2: Both are new, each in the heap allocated space, and each of the memory address assigned to S1, S2. The space address is different, = = is compared to false. However, each of the values stored in the heap space is the address of the same object in the string constant pool. It is not difficult to understand according to the diagram at the demo.
- S3, S4 Ibid demo explanation.
- Both S5 and S6 are values in the constant pool, both pointing to the same object in the constant pool with the same address value, so the result is true.
Demo2
public class StringDemo4 {
public static void main(String[] args) {
String s1 = "hello";
String s2 = "world";
String s3 = "helloworld";
System.out.println(s3 == s1 + s2);// false
System.out.println(s3.equals((s1 + s2)));// true
System.out.println(s3 == "hello" + "world");//false
System.out.println(s3.equals("hello" + "world"));// true
}
}
Demo2 detailed
The Equals () comparison method does not explain that the comparison values are equal and are true.
- S1 and S2 Add is first in the string constant pool open a space, and then splicing, the address of this space is S1 and S2 after stitching the address. The output is false because it differs from the address of the S3.
- S3 and "Hello" + "world" For comparison, "Hello" + "world" first stitching into "HelloWorld", and then go to the string constant pool to find whether there is "HelloWorld", there is, so and s3 a string object is shared, true.
Summarize
- string s = new string ("Hello") creates a 2 (1) object, and string s = "Hello" creates 1 (0) objects.
Note: When the string constant is in the pool there is an object hello in parentheses!
- String if the variable is added, first open space, in stitching.
- If the string is a constant addition, it is added first, then the constant pool is searched, if there is a direct return, otherwise, it is created.
Reference: What is the difference between string Str=new string ("a") and string str = "a"?
What is the difference between string literals in Java and the new string, such as String Str=new string ("a") and string str = "a"?