java = = and equals and differences in detail + case

Source: Internet
Author: User

First met = = and equals I was not clear, and later read a lot of blogs, revenue bandits Shallow,

Worry later to forget, so write this, later review can use.

(There is where the wrong write, hope can leave a message to help improve, everyone together progress)

Java data types are divided into two main categories: basic data types, reference data types:

1. Basic Data type:

Integer type:

Integer: Byte, short, int, long

Float type: float, double

Character type: Char

Boolean Type: Boolean

2. Reference data type:

arrays, classes, interfaces

Ii. Comparison of basic data types

   The basic type of comparison can only be compared with = =, there is no way to compare with equals, there is no such method, packaging class only

  

Iii. comparison of basic types and packaging types

  Box: Int-->integer, unboxing: integer-->int

  1.基本型和基本型封装型进行“==”运算符的比较,基本型封装型将会自动拆箱变为基本型后再进行比较

  2. For the comparison of equals, let's look at its source code first.

  This is the equals source of object, it is clear that the two address is the same

  

  This is the equals of the integer, indicating that he rewrote the source of the object, he is the first comparison type is the same,

If the type is not the same, return false directly, and if the type is the same, he will first split the box before comparing.

  

  comparison instances of int and integer

    

Iv. Comparison of string types

   1.String the comparison of = = is to see if his address is the same, but the string he is more special, he has a string constant pool, directly define the string (not the case of new)

He will first go to the string constant pool to see if there are any, there is direct pointing to the previous. Not created (refers to new).

2.equals: Because string inherits equals of object and writes equals again. Look at the following source code, we can see that he is first to judge is not the same object, if it is directly return true,

If it's not him, compare the content.

    string comparison instance

  

V. Comparison of reference types

  The comparison of reference types is most straightforward, whether = = or equals is the comparison address

This is the equals source

  

Compare Cases

    

java = = and equals and differences in detail + case

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.