Add equals and "=" in java, and add equals in java
After JDK1.5, an Integer is automatically packed in the format of Integer I = 5;
The packing process is I = new Integer (4) in the memory. Everyone is familiar with this.
When this form is used, the equals usage remains unchanged, but "=" is slightly different.
See the following example:
(1)
Integer x = 12;
Integer y = 12;
System. out. println (x = y); // true
System. out. println (x. equals (y); // true
(2)
Integer x = 127;
Integer y = 127;
System. out. println (x = y); // true
System. out. println (x. equals (y); // true
Result and(1)Same
(3)
Integer x = 128;
Integer y = 128;
System. out. println (x = y); // false
System. out. println (x. equals (y); // true
(4)
Integer x = 129;
Integer y = 129;
System. out. println (x = y); // false
System. out. println (x. equals (y); // true
Result and(3)Same
Summary:The equals usage is the same as described in the previous blog.
But the usage of "=" has changed. This is because the data is automatically packed. If the data is packed in a byte (note that the data above is 127), the data will be shared, will not open up new space,
So the two new addresses are the same.