Java stack and stack, java stack

Source: Internet
Author: User
Tags java reference

Java stack and stack, java stack

Java basic type:

Variables are stored in the stack.

 

Java reference type:

The variable name is stored in the stack area, and the variable content is stored in the heap area.

 

Instance 1:

Int a = 6;

Int B = 9;

Swap (int x, int y)

{

Int tmp = x;

X = y;

Y = x;

}

Exchange between a and B fails because Stack a and B are copied in the method. In this case, x y and a B are not in the same group.

 

Example 2:

Class Data

{

Int a = 6;

Int B = 9;

}

Data data_1 = new DataWrap ();

Data_1.a = 6;

Data_1. B = 9;

Swap (Data data_2)

{

Int tmp = data_2.a;

Data_2.a = data_2. B;

Data_2. B = tmp;

}

A and B are switched successfully, because data_2 only copies a data_1 reference in the stack memory, but data_1 and data_2 both point to the same heap memory.

 

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.