[Turn] the difference Barney Java and equals the difference between == == in Java equals and the

First on the original: the Java equals and == in the difference between :

Ha ha ha ha, as to why I always turn people's blog, to be honest, it felt like a thief, stealing the labor of others success is simply a "brazen", however, encountered before, to see some very good blog, collections , but over a period of time to see, I do not know what the reason, the original author deleted!

So, I can only steal the collection by this way.

 

data type in java can be divided into two categories: 
1. Basic data types, also known as primitive data types. byte, short, char, int,  long, float, double, boolean
  comparison between them, the application of double equal sign (==), comparison is their value. 
2. The complex data types (classes) 
  when compared with their (==), the comparison is that they store address in memory, so unless the same is out of a new object, the results of their comparison is true otherwise, the comparison result is false. Among all JAVA classes are derived from the Object base class defines a method of equals in Object base class, initial behavior of this method is to compare the memory address of the object, but in some libraries which this method is overwritten, such as a String, Integer, Date equals among these classes has its own implementation, rather than comparing the address stored in the class in the heap memory.
  For comparison performed between equals complex data types, in the absence of the override equals method, or comparison between them based on their address values stored in memory locations, since the equals method of Object is a double equal sign ( ==) comparison, the result of a comparison with the results of the double equal sign (==) is the same.

 

public class TestString {
 public static void main(String[] args) {
String s1 = "Monday";
String s2 = "Monday";
if (s1 == s2)
{
System.out.println("s1 == s2");}
else{
System.out.println("s1 != s2");}
}
}

Compile and run the program, output: s1 == s2 Description: s1 and s2 refer to the same String object - "catalog on Monday"!
2. little more about the changes to the program, there will be more surprising findings:

 

public class TestString {
public static void main(String[] args) {
String s1 = "Monday";
String s2 = new String("Monday");
if (s1 == s2)
{System.out.println("s1 == s2");}
else
{System.out.println("s1 != s2");}
if (s1.equals(s2)) {System.out.println("s1 equals s2");}
else{
System.out.println("s1 not equals s2");}
}
}

我们将s2用new操作符创建
程序输出:
s1 != s2
s1 equals s2
说明:s1 s2分别引用了两个"Monday"String对象

3. 字符串缓冲池
原来,程序在运行的时候会创建一个字符串缓冲池当使用 s2 = "Monday" 这样的表达是创建字符串的时候,程序首先会在这个String缓冲池中寻找相同值的对象,在第一个程序中,s1先被放到了池中,所以在s2被创建的时候,程序找到了具有相同值的 s1
将s2引用s1所引用的对象"Monday"
第二段程序中,使用了 new 操作符,他明白的告诉程序:"我要一个新的!不要旧的!"于是一个新的"Monday"Sting对象被创建在内存中。他们的值相同,但是位置不同,一个在池中游泳一个在岸边休息。哎呀,真是资源浪费,明明是一样的非要分开做什么呢?

4.再次更改程序:

public class TestString {
public static void main(String[] args) {
String s1 = "Monday";
String s2 = new String("Monday");
s2 = s2.intern();
if (s1 == s2)
{System.out.println("s1 == s2");}
else
{System.out.println("s1 != s2");}
if (s1.equals(s2)) {System.out.println("s1 equals s2");}
else{
System.out.println("s1 not equals s2");}
}
}

这次加入:s2 = s2.intern();
程序输出:
s1 == s2
s1 equals s2
原 来,(java.lang.String的intern()方法"abc".intern()方法的返回值还是字符串"abc",表面上看起来好像这个方 法没什么用处。但实际上,它做了个小动作:检查字符串池里是否存在"abc"这么一个字符串,如果存在,就返回池里的字符串;如果不存在,该方法会 把"abc"添加到字符串池中,然后再返回它的引用。

 

原文出处:https://www.cnblogs.com/zhxhdean/archive/2011/03/25/1995431.html

Guess you like

Origin www.cnblogs.com/Guhongying/p/11145121.html