Equals in Java compares, pits one

Source: Internet
Author: User

In recent work, it is often necessary to compare the values of 2 objects. There is a problem often encountered, is the following 2 kinds of situations.

public static void Main (string[] args) {

Integer a = 11;

Object B=a;

System.out.println (B.equals ("one"));//false

String C = "11";

Object d = c;

System.out.println (D.equals ("one"));//true

}

To view the JDK document in Chinese:

equals of Integer

Compares this object with the specified object. When and only if the parameter is notnull, and is an object that contains the sameintValue ofIntegerobject, the result istrue


equals of String

Compares this object with the specified object. When and only if the parameter is notnull, and is an object that contains the samestringValue ofStringobject, the result istrue


Both string and integer overload the Equals method of object, which requires that two object comparisons must first be of the same type.

This type should refer to the "actual type", the actual type of Object D is "string", so the Equals method of string is called.


Multi-State ~

Original starting: http://fansunion.cn/article/detail/536.html

Equals in Java compares, pits one

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.