Java Learning notes = = and equals

Source: Internet
Author: User
Tags instance method

First, the problem introduced

Java tests whether two variables are equal in two ways: the = = operator and the Equals method.

But is it exactly the same? Consider the following procedure:

1  Public classtestequal2 {3      Public Static voidMain (string[] args)4     {5         intit = 65;6         floatF1 = 65.0F;7System.out.println (is "65 and 65.0 equal?" + (it = =F1));8         Charch = ' A ';9System.out.println ("65 and ' A ' are equal?" + (it = =ch));Ten          OneString str1 =NewString ("Hello"); AString str2 =NewString ("Hello"); -System.out.println ("str1 and str2 are equal?" + (str1 = =str2)); -System.out.println ("str1 and equals str2?" +(Str1.equals (STR2))); the     } -}

Output:

are 65 and 65.0 equal? True
are 65 and ' A ' equal? True
are str1 and str2 equal? False
Str1 and is equals str2? True

The results of the program can be seen:

    • Use = = To determine the 2 basic types of variables (not the same as the two variable types, but both must be the basic type), as long as the two variable literals are equal, use = = To determine the return of true, 5-9 lines of code and its output illustrates the problem
    • For reference type variables , you must have two variables pointing to the same object , = = will not return ture, otherwise the FALSE,11-13 line code and its output illustrate the problem

For the code 13 line of judgment, obviously "not very reasonable", because intuitively, the two are the same ah, this is what we often encounter when the two reference variables are equal, it is not strictly required two reference variables to point to the same object, such as the above program two string variables, As long as the strings contained in the string object contain the same sequence of characters to be considered equal, it is necessary to use the Equals method, which is the last line of the preceding code, and the result returns TRUE.

Second, see Equals again

The Equals method is an instance method provided by the object class, so all reference variables can call the method to determine if they are equal to other reference variables.

The Truth is cruel: In fact, theequals method is the same as = = when judging two reference variables (which are not variables of the basic type, in fact, equals cannot be used to judge the basic type variables). That is, only two reference variables will return true if they point to the same object. this ... How do you explain the above situation?

As we all know, all classes are subclasses of the object class, so all subclasses can overload the parent class's methods , in fact, string overrides the Equals method in the parent class of the object class, and it has its own whole standard: as long as two strings contain the same sequence of characters return true , otherwise false. (children who have learned C + + may say, I overload the = = operator, do not overload the Equals method, and unfortunately Java is not overloaded with operators ).

It is easy to see because the Equals method provided by the object class is essentially the same as = =, which leads to the fact that this method has little practical meaning, if the standard comparison reference variable is as strict as the = = operator, it is estimated that very few of the two reference variables in practice will be equal. So this method, if necessary, often overrides the method when we create a class, and defines a reasonable set of equality criteria.

Java Learning notes = = and equals

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.