Get the App
SLTechnology News&Howtos  ›  Development  › 

How to understand value passing and reference passing in java

Shulou Source: shulou.com Published: 2022-06-02 18:14:08 09月21日 Update

This article introduces the knowledge of "how to understand java value transfer and reference transfer". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Value transfer

During the call of the method, the argument passes its actual value to the parameter, which is passed by copying the value of the parameter to the function, so that if the value (the value of the parameter) is manipulated in the function, the value of the parameter will not be affected. Because of direct replication, this approach is particularly inefficient when transferring large amounts of data.

When the basic type of java is passed as an argument to the function body, the passed parameter becomes a local variable inside the function body, which is a copy of the input parameter. After all the operations within the function body are executed, the local variable completes its mission, and it does not affect the variable as a parameter.

Reference transfer

Reference transfer makes up for the deficiency of value transmission. If a large amount of data is passed, it will take up a lot of memory space, and reference transfer is to pass the address value of the object, and the function receives the first address value of the original value. During the execution of the method, the content of the parameter and the argument are the same, pointing to the same memory address, that is to say, the operation is actually the source data, so the execution of the method will affect the actual object.

Passing an object as an entry parameter in java defaults to "reference passing", passing only a reference to the object. When the body of the function changes the input variable, it is essentially a direct operation on the object.

Examples of value transfer:

Public static void main (String [] args) {

Int num1 = 10

Int num2 = 20

Swap (num1, num2)

System.out.println ("num1 =" + num1); / / 10

System.out.println ("num2 =" + num2); / / 20

}

Public static void swap (int a, int b) {

Int temp = a

A = b

B = temp

System.out.println ("a =" + a); / / 20

System.out.println ("b =" + b); / / 10

}

Application delivery example:

Public static void main (String [] args) {

Int [] arr = {1, 2, 3, 4, 5}

Change (arr)

System.out.println (arr [0]); / / 0

}

/ / change the first element of the array to 0

Public static void change (int [] array) {

Int len = array.length

Array [0] = 0

}

So much for the introduction of "how to understand java value passing and reference passing". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Function parameter variable object procedure content address actual that is local data method formal parameter influence median example memory more knowledge input Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Technology macOS Redmi Apple