Java value passing and reference passing

Source: Internet
Author: User

2017-11-02 13:09:00

One, basic types and reference types in-memory storage

Data types in Java fall into two categories, basic types and object types . Accordingly, there are two types of variables: the base type and the reference type.
A variable of a primitive type holds the original value, that is, the value it represents is the value itself;
Whereas a variable of a reference type holds a reference value, the reference value points to the address of the memory space, which represents the reference to an object, not the object itself.
The object itself is stored at the location of the address represented by the reference value.

Basic types include: Byte,short,int,long,char,float,double,boolean,returnaddress,
Reference types include: Class types, interface types, and arrays.

Differences between the basic types of variables and reference types

    • The basic data type allocates space to the system when it is declared:
int i;i = 2;//correct, because the base type has allocated memory space at the time of declaration
    • The reference is different, and it declares that only the variable is assigned a reference space and no data space is allocated:

1. The size of the reference itself is related to the number of bits of the operating system, on 64-bit platforms, which account for 8 bytes and 4 bytes on 32-bit platforms, this should be natural because 32-bit's operating system finds an address in the memory space of 4G (2^32), This address is represented by 4bytes (32bits).

2. The reference itself is retained in the stack

3. Refers to the object that is stored in the heap

4. In general, a reference type is similar to a pointer type in C + +, that is, its bit width is the same as the int type, which occupies 32 bits and 4 bytes in the method area.

Date A, B; Open up two reference spaces in memory A = new date ();//Open the data space for storing the Date object and assign the first address of the space to AB = A; Writes the address in the a storage space to the storage space of B

Iii. reference passing and value passing

Value passing:

When a method is called, the actual parameter passes its value to the corresponding formal parameter, and the function receives a copy of the original value, where there are two equal basic types in memory, that is, the actual parameter and the formal parameter , and the operation in the subsequent method is the modification of the parameter value. , and does not affect the value of the actual parameter .

Reference delivery:

Also known as a pass-through address. When a method is called, the actual argument's reference (the address, not the value of the parameter) is passed to the corresponding formal parameter in the method, and the function receives the memory address of the original value;
In the execution of the method, the parameters and the arguments are the same, pointing to the same memory address, and the operation of the reference in the method execution will affect the actual object .

Here special consideration is given to string, and several basic types, such as Integer, double, are immutable types,
Because there is no function to provide its own modification, each operation is a new object, so special treatment, it can be considered to be similar to the basic data type, transfer value operation.

Java value passing and reference passing

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.