Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Java supports value passing or reference passing

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article focuses on "java support value transfer or reference transfer", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "java support value transfer or reference transfer"!

The purpose of this paper is to verify whether Java language is value passing or reference passing and the implementation principle of Java parameter passing.

The question is introduced:

Read the code snippet first:

Public static void main (String [] args) {Person p=new Person ("Zhang San"); f (p); System.out.println ("actual reference:" + p);} public static void f (Person p) {p.name = "Li Si"; System.out.println ("formal parameter:" + p);}

Running result:

Formal reference: Person {name= "Li Si"}

Argument: Person {name= "Li Si"}

We pass an object variable to the method, then modify the properties of the object in the method, print the actual parameters and formal parameters, and on the surface, the values of the real parameters are modified. Does Java also support reference passing?

The answer is: the Java language only supports value passing, not reference passing.

Before verifying the way Java parameters are passed, we need to understand the difference between function value passing and reference passing.

Value transfer (pass by value) means that a copy of the actual parameters is passed to the function when the function is called, so that if the parameters are modified in the function, the actual parameters will not be affected.

Reference passing (pass by reference) means that the address of the actual parameter is passed directly to the function when the function is called, so the modification of the parameter in the function will affect the actual parameter.

You can see that the difference between value passing and reference passing lies in whether the modification of the parameter will affect the actual parameter.

1. Parameter passing of basic data types

We pass a basic data type to the method, and then modify the value of the parameter in the method, and find that the modification of the parameter does not affect the actual parameter.

Public static void main (String [] args) {int aquifer 1; f (p); System.out.println ("argument:" + a);} public static void f (int a) {aquidus 2; System.out.println ("formal parameter:" + a);} 2. Parameter passing of reference data type

In the example introduced by the problem, it seems that the modification of the parameter affects the parameter, but as long as we create a new object and assign a value to the parameter, then print the parameter and the parameter, the two do not affect each other. Indicates that Java does not conform to reference passing.

Public static void main (String [] args) {Person p=new Person ("Zhang San"); f (p); System.out.println ("actual reference:" + p);} public static void f (Person p) {p=new Person ("Li Si"); System.out.println ("formal parameter:" + p);} 3. Principle

To understand why these phenomena occur, we need to understand the basic principles behind Java:

JVM divides many block areas, the objects we create are placed in the heap, and the basic data types and local variables are placed in the stack. When passing a basic data type, a copy of the data is created and passed to the method, so the argument is not affected by the parameter modification, as shown in the figure:

Because the object is placed in the heap area, we can only use the object's reference to manipulate the object.

When the object reference is passed to the method, it actually creates a copy of the reference and points to the same object at the same time. When the object is manipulated by the formal parameter reference, it is as if the actual parameter has changed. In fact, the object content has changed, but the object variable has not changed. The argument itself has not changed. Therefore, it is not difficult to understand the examples in the question.

When we reassign the parameter, the argument is not affected, and the parameter and the parameter already point to two different objects.

Therefore, Java only supports value passing.

At this point, I believe you have a deeper understanding of "java support value passing or reference passing". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report