Java Equality comparison: = = and equals

Source: Internet
Author: User

There are two types of comparisons: the comparison between variables, the comparison of variables and values,

The reference type value is divided into two types: "ABCD", New String ("ABCD"); "ABCD" is equivalent to a in a= "ABCD", and the new string ("ABCD") is equivalent to B in b= new string ("ABCD");

New to allocate a newer memory address

1. The basic type variable is used = =; Because the basic data type has no equal method

2. Reference type variable: (Reference type: Class, interface, array)

= =: Compare their in-memory addresses, i.e. reference equal

equals:string integers are compared with equal values other than the Equals method comparison is reference equality

Note:

int a = 3;

int b = 3;

int c = 4;

Because the basic data type size is known, the life cycle is known (defined in the method, the method exits after the automatic destruction), so the pursuit of speed, stored in the stack (only refers to local variables), when the definition of the field 3 to a, then define B, first find there is no 3 for the field address, if so, let B also point to That is, the address of A and B is the same, the last two lines of code is equivalent to int b = A; When defining C, found that there is no field 4, then re-open the address of the field 4, let reference c point to field 4. String (If a string is strings s = "abc"; it is placed in the stack, if a string is used to create the object in the way string s = new String ("abc"); it's in the heap, and if it's a simple "abc", the input string constant is placed on the static segement/stack/string dominant), when executing = There is also a procedure similar to first looking for the same string. ToString () method?

Java Equality comparison: = = and equals

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.