The difference between the object class and the String class equals method

Source: Internet
Author: User
the Equals method of the String class and "= ="

I believe many students who have studied Java know that the Equals method is used when comparing the contents of two string objects, such as: String Str1=new string ("A");     String Str2=new string ("B");     String Str3=new string ("B");     Boolean result1= str1.equals (STR2);     Boolean result2= str2.equals (STR3);     System.out.println (RESULT1); System.out.println (RESULT2); The input result1 is false,result2 to true. Because str1 and str2 content is not the same, and str2 and STR3 content is the same "B".
When you use "= =" in a string class, you compare whether a reference to a two string object points to the same object, such as a string Str4=new string ("B");     String Str5=new string ("B");     String STR6=STR5;     Boolean result3= (STR4==STR5);     Boolean result4= (STR5==STR6);     System.out.println (RESULT3); System.out.println (RESULT4); The input RESULT3 is FALSE,RESULT4 to true. Because STR4, STR5 although the content is the same but they are different objects, like two identical cups with the same amount of water, but they are different, result3 false. STR5 and STR6 are pointing to the same string object, so Result4 is true.
The Equals () method is defined in the String class
member variable of string class

<span style= "FONT-SIZE:14PX;" >public Final class String
    implements Java.io.Serializable, Comparable<string>, charsequence
{
    /** The value is used for character storage. *
    Private final char value[];

    /** the ' offset is ' the ' storage ' is used. * *
    private final int offset;

    /** The count is the number of characters in the String. *
    private final int count;

    /** Cache The hash code for the string */
    private int hash;//Default to 0

    /** use Serialversionuid from JDK 1.0 .2 for Interoperability * *
    private static final long serialversionuid = -6849794470754667710l;
</span>


equals method source code
public boolean equals (Object anobject) {
	if (this = = AnObject) {return
	    true;
	}
	if (anobject instanceof string) {
	    string anotherstring = (string) anobject;
	    int n = count;
	    if (n = = anotherstring.count) {
		char v1[] = value;
		Char v2[] = Anotherstring.value;
		int i = offset;
		int j = Anotherstring.offset;
		while (n--! = 0) {
		    if (v1[i++]!= v2[j++]) return
			false;
		return true;
	    }
	return false;
    }
In the Equals method of the string class, use "= =" to compare whether two objects are references to the same object, or, if so, to return true; if not, how to continue comparing the size of two string objects with the contents.

The Equals () method is defined in the object class
equals method source code

<span style= "FONT-SIZE:24PX;" >  </span><span style= "FONT-SIZE:14PX;" >public boolean equals (Object obj) {return
	(this = = obj);
    } </span>

"= =" is used to compare references and comparisons of basic data types with different features:

Compares the base data type, if two values are the same, the result is true, and when the reference is compared, if the reference points to the same object in memory, the result is true. "= =" is here to compare whether two objects point to the same object in memory. The Equals method in the object class is essentially the same as "= =", comparing whether two object references point to the same object (that is, whether two objects are the same object)

We usually customize the object and usually need to override the Equals method.
equals () method

<span style= "FONT-SIZE:14PX;" >class person
{
 private String name;
 private int age;
 
 Public person (String name, int age)
 {
  this.name = name;
  This.age = age;
 }
 public void Show ()
 {
  System.out.println (name + "," + age);
 }
 
 public boolean equals (Object obj)
 {
  if (!) ( obj instanceof person)   //If not the person type, returns false directly
     ;
  This is P1, obj is p2 person
  p = (person) obj; s//due to access to a person-specific member, obj is replaced with the person class.
  if (this.name!= p.name)
  
  if (this.name = = null)
  {
     if (p.name!= null)       //comparison name return
        false;
  Else if (!this.name.equals (p.name)) return
     false;
     
  if (this.age!= p.age) return
        false;
     return true;
  return (this = obj);
 }
</span>



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.