Comparison of objects in java

Source: Internet
Author: User

Let's take a look at this Code:

Public class IntergerError {public static void main (String [] args) {// TODO Auto-generated method stubInteger num1 = new Integer (100); Integer num2 = new Integer (100 ); system. out. println (compare (num1, num2);} private static boolean compare (Integer num1, Integer num2) {boolean a = (num1 = num2 ); boolean B = (num1> num2); boolean c = (num1 <num2); return a | B | c ;}}

 

What is your answer, and the return value must be true? Haha, think about it again. The return value of this Code is false, which makes it hard to understand. Why? Since we use or, it is not big, small, or equal, it must be true. The above is also my idea. Later I found some information on the Internet and concluded that "=" is a relatively simple type value or whether the comparison object references the same object, as for "" and "", it is meaningless for objects. Therefore, it is best to use the equal () method or the de-object value to compare the object values. In fact, this issue has not caught much attention before. Today, we have encountered a disaster that is caused by poor foundation. Hope you can criticize and correct your experience.

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.