Java Long, Integer, Double, Boolean types cannot be directly compared

Source: Internet
Author: User

Test:

System.out.println (new Long) ==new Long (1000));
System.out.println (New Integer (==new) integer (1000));
System.out.println (new double (1000d) ==new double (1000d));
System.out.println (new float (1000f) ==new float (1000f));
System.out.println (New Boolean (True) ==new Boolean (true));
System.out.println (New short ("+") ==new short ("400");

False
False
False
False
False
False

The long type refers to the   Java.util.Lang object, not the base type long  (note case)
If you use the = = Double equals comparison object in Java, it is equivalent to comparing the memory address of two objects, that is, comparing two objects is the same object
If you compare two long object values for equality, you cannot compare them by using a double equal sign, which can be done in the following way:
1. Use the Equals method
Long a = new long (3);
Long B = new Long (3);
System.out.println (A.equals (b));
2. Use the Longvalue () method in the Long type to compare

Long a = new long (3);
Long B = new Long (3);
System.out.println (A.longvalue () ==b.longvalue ());

Java Long, Integer, Double, Boolean types cannot be directly 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.