The difference between the Java and equals the detailed ==

the difference

== : For basic data types, "= =" is the comparison whether the data values are equal; for reference data types, == equality comparison target address. Boolean result type, the same is true, different to false.
equals: Object Class in Comparative address is the same, while the String class overrides the equals method to compare the contents of two strings are the same.

Code analysis shows

== code section

public class Test {

	public static void main(String[] args) {
	
	    double a = 10.0;
	    int b = 10;
	    System.out.println(a==b);
	    
	    String str1 = "Jim";
	    String str2 = "Tim";
	    System.out.println(str1==str2);
	}

}

Code results:
to true
to false
in a first class comparison, the same values of a and b, the output true;
in the second category comparison, str1 str2 content is stored in the constant pool, and since the contents are different, so the stack stored in different addresses in the comparison due to the different addresses, the output false.

code portion equals

public class Test {
	String name;
		
	public static void main(String[] args) {
	
	    String name1="Tim";
	    String name2="Tim";
	    System.out.println(name1.equals(name2));
	    
	    Test test = new Test();
	    test.name ="Tim";
	    System.out.println("1+"+name1.equals(test));
	    System.out.println("2+"+name1.equals(test.name));
	    
	    String name3=new String ("Tom");
	    System.out.println(name1.equals(name3));
	}

}

Code results:
to true
. 1 to false +
2 + to true
to false
in a first class comparison, the same content NAME1 name2, and thus the same data is stored in the same address space as the constant pool, the corresponding address. Output true.
In the second category, to help better understand the equals method String being rewritten, the definition of a name outside the main function method and to define a test object so that it calls the method name as "Jim". As can be seen by comparing the two: test object and the address of name1 different, different content output 1 + false, but its method invocation name assigned to "Jim", so you can see, although the difference between the two addresses, but because the contents of the string are identical outputs 2 + true.
In a third category, it can be seen, due to the different results with both the name1 name3 address, the content is different, so the output of the determination is false.

For the case of the above-described method of determination and, it might be rewritten String equals the extraction operation is determined to analyze the principle, an example to the last group is determined.

	  public boolean equals(Object anObject) {//anObject储存上转型对象地址
	        if (this == anObject) {//比较地址是否相等,地址相等则储存数据相同,输出true
	            return true;
	        }
	        if (anObject instanceof String) {//判断anObject变量所指向的地址是否为String类型
	            String anotherString = (String)anObject;//多态的体现,将上转型对象转化为下转型对象;
	            int n = value.length;//给n赋值value的长度(value为name1的数组)
	            if (n == anotherString.value.length) {//判断value与anotherString.value(name2的数组)的长度是否相等
	                char v1[] = value;//name1的数组
	                char v2[] = anotherString.value;//name3的数组
	                int i = 0;//逐位判断name1和name3的字符是否相等
	                while (n-- != 0) {
	                    if (v1[i] != v2[i])//不同即输出false,结束判断
	                        return false;
	                    i++;
	                }
	                return true;
	            }
	        }
	        return false;
	    }*/

The block consists of two parts, the first if statement determines whether the addresses of the two strings of the same, if the same, the output true, end determination; if different from entering the second if statement determines whether two objects point type String Without pointing to a String, the output false. If the point of type String, then proceed as follows: the need to use .length () method of the String subclass, the transition needs to operate at anotherObject be to achieve the same length of the string comparison whether two, if we are not the same, then the output false; if the same, then the two strings are converted into the form of an array, and were assigned to the character array v1 [], v2 [], while loop to enter is determined by bit the corresponding characters are the same, if there are different characters, output false; If it is not a different character, the character described which are equal respectively, an output true, the end of the determination.

Published 19 original articles · won praise 0 · Views 459

Guess you like

Origin blog.csdn.net/zzu957/article/details/104863577
Recommended