Java uses heap and stack concepts to understand equals () "= =" and Hashcode ()

Source: Internet
Author: User

When learning Java basic data types and complex data types, especially equals () "= =" and the Hashcode () part, not very understand, also stayed a long time, finally a bit of a look.

To understand equals () "= =" and Hashcode (), it's a good idea to first understand the heap and stack in Java memory:

The following paragraph is for reference from http://www.cnblogs.com/whgw/archive/2011/09/29/2194997.html

You can also click to see a more detailed explanation.

Heap and Stack in Java

Java divides memory into two types: one is stack memory, and the other is heap memory.

  1, some basic types of variables defined in the function and reference variables of the object are allocated in the function's stack memory. For example:

int a =10; String str;

These are allocations of memory in the stack.

 2 . Both arrays and classes are allocated in the heap memory of the function. For example:

String str =new string (); int []num=int [2]{2,3};

These are allocations of memory in the heap. Heap memory is used to hold objects and arrays created by new.

The pros and cons of stack memory and heap memory in Java:

The Java stack is a static data area that allocates space while the program compiles.

The Java heap is a run-time data area where classes of objects and arrays allocate space from them. These objects are created with the new directive, and they do not require program code to be explicitly released. There is a garbage collection mechanism in the JVM virtual machine, and the heap is responsible for the garbage collection mechanism.

The advantage of the heap is that the memory can be allocated dynamically, and the lifetime does not have to tell the compiler beforehand because it allocates memory dynamically at runtime, and the Java garbage collector automatically collects the data that is no longer used. 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 handle.

The stack has a very important property, that is, the data in the stack can be shared. For example:

int a =5; int b = 5;

Both A and B are allocated memory in the stack.

The program allocates a memory to a and deposits 5

When the program allocates memory to B, first check that there is 5 in memory,

found that there is 5, direct b to the memory of storage 5 where the address.

That is to realize the sharing of data, non-repeatability.

If you add one more sentence:

int a =5; int b =5= 4;

The first two sentences are executed in the same way as before,

The third sentence, because the function of the stack memory is not stored in 4,

Allocate memory to B and deposit 4

Understanding Equals () "= =" and Hashcode () with the concept of heaps and stacks

1, for (int, short, long, byte, float, double, Boolean, char) basic data type, compare whether it points to the same object with "= ="

double a = 2.5; double B = 2.5; System.out.println (a==b);

The result is: true

Cause: For a double base data type, its memory is allocated on the stack, because of the shareable nature of the stack memory, a, B points to the same memory space, and the same object.

The function of "= =" is to compare whether the two variables point to the same object and therefore true

2, for classes, arrays and other complex data types, compare their content is equal when using equals () and Hashcode ()

        New String ("Hello");         New String ("Hello");        System.out.println (String.hashcode ());        System.out.println (String2.hashcode ());        System.out.println (String.Equals (string2));         = = string2);    

The result is:

99162322
99162322
True
False

Reason:

There is a hashcode () method for the data types encapsulated by the String,integer class.

Hashcode () is actually doing some sort of algorithm on its content, and finally a numerical value (which may not be the case).

So, like the content, the hash value is the same.

For data types such as String,integer, the same equals () method, which acts on the same as "= =", is used to determine whether the reference type points to the same object.

But

When comparing with the Equals () method, it is the same object for the class file, String, date, and wrapper class (Wrapper Class) to compare types and contents regardless of the reference;

Cause: The Equals () method is overridden in these classes to compare whether the content is the same.

So although it's not the same object here, the Equals () method still returns True

Because both string and string2 are complex data types, their memory is allocated in the heap,

The data stored in the heap can be duplicated, so the string and string2 are not pointing to the same object.

So the result of String = = String2 is False


 

Java uses heap and stack concepts to understand equals () "= =" and Hashcode ()

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.