= = and equals

Source: Internet
Author: User

= = compares the values of two variables for equality, for a reference variable represents whether the storage address of two variables is the same in the heap, that is, whether the contents of the stack are the same

equals represents whether two variables are a reference to the same object, that is, whether the contents of the heap are the same

= = compares the addresses of 2 objects, while equals compares the contents of 2 objects.

Basic data types are placed inside the stack, so the basic data types can be compared with = =

When Equals is true, = = is not necessarily true

= = True to indicate that a, B is a no-reference to an instance of the same class

Equals is true to also indicate that a, B is a separate reference to a different instance of the same class, that is, the contents of different addresses in the heap are the same

Report:
When new is a pair of like classes, it creates a space in the heap and then points the space to your new handle, which is placed on the stack, like a list,
The CPU sends instructions to the stack to operate. (Stacks like a book's directory, heap like a detailed chapter in a book) are stored in the stack of the object's specific physical address.

The Java heap is a run-time data area in which the objects of the class allocate space. These objects are established through directives such as new, NewArray, Anewarray, and Multianewarray, and they do not require program code to be explicitly released. Heap is responsible for garbage collection, the advantage of the heap is the ability to dynamically allocate memory size, the lifetime does not have to tell the compiler beforehand, because it is at runtime to allocate memory dynamically, Java garbage collector will automatically take away these no longer use data. However, the disadvantage is that the access speed is slower due to the dynamic allocation of memory at run time.

The advantage of the stack is that the access speed is faster than the heap, after the register, the stack data can be shared . However, the disadvantage is that the size and lifetime of the data in the stack must be deterministic and inflexible. The stack mainly contains some basic types of variables (, Int,short,long,byte,float,double,boolean,char) and object handles.

= = 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.