Comparison of equals and = in java and equals in java

Source: Internet
Author: User

Comparison of equals and = in java and equals in java
1. equals usage

Summary:

  • For comparison between String types, the equals () method of the String class is called, the content of the String is compared, not the address, and can only be used to compare the String type, because neither the StringBuffer nor the StringBuilder class has the equals () method.
  • For comparison between other types (including cases with a Sting type), take o1.equals (o2) as an example: if o1 is of the String type, the equals method of the String class is called, the return value is false. If o1 is not of the String type, the equals () method of the Object class is called (which is the same as =), and the address of the referenced variable instead of the content is compared.
 1     private static void equalsAnal() { 2         String s1 = "hello"; 3         String s2 = "hello"; 4         String s3 = new String("hello"); 5         String s4 = new String("hello"); 6         String s5 = new String(new StringBuffer("hello")); 7          8         StringBuffer s6 = new StringBuffer("hello"); 9         StringBuffer s7 = new StringBuffer("hello");10         11         System.out.println(s1.equals(s2));//true12         System.out.println(s3.equals(s4));//true13         14         System.out.println(s1.equals(s3));//true15         16         System.out.println(s1.equals(s5));//true17         System.out.println(s3.equals(s5));//true18         19         System.out.println(s6.equals(s7));//false20         System.out.println(s1.equals(s6));//false21         System.out.println(s3.equals(s6));//false22         System.out.println(s5.equals(s6));//false23         System.out.println(s6.equals(s1));//false24     }

For the results of the above code, we can combine the source code of the String class and Object class for analysis:

1 // equals method 2 public boolean equals (Object anObject) {3 if (this = anObject) {4 return true; 5} 6 if (anObject instanceof String) in the String class) {7 String anotherString = (String) anObject; 8 int n = value. length; 9 if (n = anotherString. value. length) {10 char v1 [] = value; 11 char v2 [] = anotherString. value; 12 int I = 0; 13 while (n --! = 0) {14 if (v1 [I]! = V2 [I]) 15 return false; 16 I ++; 17} 18 return true; 19} 20} 21 return false; 22}
1 // equals method 2 public boolean equals (Object obj) in the Object class {3 return (this = obj); 4}
2. = usage

= Is a relational Operator UsedBasic Data TypeCompare whether the values of the two variables are equal.Reference TypeCompare whether the addresses of the two referenced variables are the same.

 1         int a = 10, b = 10; 2         char ch1 = 'A', ch2 = 'A'; 3         String str1 = "hello"; 4         String str2 = "hello"; 5         Integer n1 = new Integer(10); 6         Integer n2 = new Integer(10); 7          8         System.out.println(a == b);//true 9         System.out.println(ch1 == ch2);//true10         System.out.println(str1 == str2);//true11         System.out.println(n1 == n2);//false
3. Usage comparison
 1         String s1 = "hello"; 2         String s2 = "hello"; 3         String s3 = new String("hello"); 4         String s4 = new String("hello"); 5         String s5 = new String(new StringBuffer("hello")); 6          7         StringBuffer s6 = new StringBuffer("hello"); 8         StringBuffer s7 = new StringBuffer("hello"); 9         10         System.out.println(s1.equals(s2));//true11         System.out.println(s1==(s2));//true12         13         System.out.println(s3.equals(s4));//true14         System.out.println(s3==(s4));//false15         16         System.out.println(s1.equals(s3));//true17         System.out.println(s1==(s3));//false        18         19         System.out.println(s1.equals(s5));//true20         System.out.println(s1==(s5));//false21         22         System.out.println(s3.equals(s5));//true23         System.out.println(s3==(s5));//false24         25         System.out.println(s6.equals(s7));//false26         System.out.println(s1.equals(s6));//false27         System.out.println(s3.equals(s6));//false28         System.out.println(s5.equals(s6));//false29         System.out.println(s6.equals(s1));//false

To sum up, equals and = are in the same usage:

  • A String is a variable of the basic data type String;
  • Two non-String reference variables are compared.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.