Comparative ==,equals of ⑦java in essays and the effect of accuracy on comparison

Source: Internet
Author: User

Comparison ==,equals in Java

① for = =, if it is a variable that acts on the base data type , compares the value of its stored values directly, and if it is a variable that acts on a reference type , the address of the object being pointed to is compared.

② for the Equals method, note thatthe Equals method cannot act on a variable of the basic data type ;

If you do not override the Equals method , the address of the object to which the reference type variable points is compared, and classes such as string,date that override the Equals method. The content of the object being pointed to is compared .

Comparison of float type values :

In general, floating-point numbers are not directly sentenced and so on, because the floating-point number has a precision problem . To compare two floating-point numbers, the general comparison is to set a precision that is considered equal when the difference between the two is less than a certain precision . For example, S,t is a float type.

1 if (Math.Abs (S-T) < 0.001) {2     System.out.println ("equal"); 3 }else  {4     System.out.println ("Not Equal"); 5 }

Both the ④float class and the Double class both override the corresponding Equals method, judging whether the same belongs to a float object or double object, if not directly returning false, and if you continue to compare the corresponding numeric size .

The source code of JDK1.8 is as follows:

The 1.Float class overrides the Equals method:

1  Public Boolean equals (Object obj) {2       return instanceof Float) && (Floattointbits ((Float) obj.) = = Floattointbits (value)); 3 }

The 2.Double class overrides the Equals method:

1  Public Boolean equals (Object obj) {2        return instanceof Double) && (Doubletolongbits ((Double) obj) (value) = = Doubletolongbits (value)); 3 }

--------------------------------------------------------------------------------------------------------Use Instances------------- -----------------------------------------------------------------------------------------------------------

1  Public classtest{2      PublicString name= "ABC"; 3      Public Static voidMain (string[] args) {4Test test=NewTest ();5Test testb=NewTest ();6System.out.println (test.equals (TESTB)+","+ test.name.equals (testb.name)); NoteTest.equals (TESTB) is a comparison of two objects; Test.name.equals (Testb.name) is a comparison of string types
7  }
8 }

"Resolve" for object comparisons, use the object class comparer as follows:

1  Public Boolean equals (Object obj) {2         return ( this = = obj); 3 }

When you do not rewrite equals, you are judged directly by = =. The Equals method is overridden in string.

Comparative ==,equals of ⑦java in essays and the effect of accuracy on comparison

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.