How to pass value in java?
We all know that there are basically two ways to pass by value: pass by value and pass by reference. So in JAVA, is it pass by value or pass by reference? The following article will introduce it to you, I hope it will be helpful to you.
Value passing: refers to copying a copy of the actual parameters and passing them to the formal parameters when calling the function, so that the formal parameters can be modified in the function Will not affect the actual parameter value.
Pass by reference: refers to passing the address of the actual parameter directly to the formal parameter when calling the function. Then the modification of the parameter in the function will affect the actual parameter. value.
How to pass value in java?
Java's value-passing method: value-passing (all changes are only limited to the method body, outside the method body, any modification operations are no longer valid). [Recommended learning: java course]
We can use a program to verify that only value is passed in Java
/** * 验证java中只有值传递 */ class User{ private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }public class TestValue { public static void change(User user2,int a2){ System.out.println("改变之前:"+user2.getName()+",a2="+a2); user2.setName("李四"); //改变 user2 的 name 值 a2 = 10; //改变 a2 的值 System.out.println("改变之后:"+user2.getName()+",a2="+a2); user2 = new User(); //将 user2 重新指向一个新对象 user2.setName("王五"); System.out.println("重新指向一个新对象后:"+user2.getName()); } public static void main(String[] args){ User user1 = new User(); user1.setName("张三"); //初始化 user1 的 name 为张三 int a1 = 5; //初始化 a1 的值为 5 change(user1,a1); //调用方法验证传值方式 System.out.println("调用方法后:"+user1.getName()+",a1="+a1); } }
Run this program, the output result is:
改变之前:张三,a2=5 改变之后:李四,a2=10 重新指向一个新对象后:王五 调用方法后:李四,a1=5
Result Analysis
User class, and then instantiate a
User object in the test class, named
user1, and assign it a value of
name = 'Zhang San'.
Figure 1, instantiating an object is equivalent to opening a piece of memory in the heap, and the memory address is
017 , at this time, the reference of this object is
user1, and the memory address is
001. It saves the address of the object in the memory, which means it points to the object.
change() to try to change the
name value of
user1 to verify the value passed in java Way.
user1 as an actual parameter to the
change() method, and the formal parameter
user2 accepts this actual parameter, which is reflected here. The difference between the two methods of passing parameters is revealed. If passed by value, then it is as defined.
Figure 2,
user2 is a copy of
user1, which means that when passing parameters,
user1 (itself a reference to an object), made a copy named
user2, which is also a reference to an object, and
user1 and
user2Points to the same object at this time.
Figure 5, when passing parameters,
user1 is directly passed to the formal parameter , just changed the name to
user2, but essentially
user1 and
user2 are actually the same. It is a reference to an object.
张三, because they all point to the same object. For the output in line 2, we still can't tell which method is used, because the same object is changed, and the value will also change; the key lies in the output in line 3 and line 4.
user2 to a new object and assign a value to this object
name = '王五', if it is passed by reference , then
user1 will also change the pointer to point to the new object. The output result after calling the method in the last line will be the same as the third line,
王五, but in fact the output It’s
李思. This shows that
user1 and
user2 are not actually the same.
Figure 2~
Figure 4, so that
user2 points to a new object,# The object pointed to by ##user1
has not changed, it is still the original object. For basic type parameters, the value of
has not changed at the end, indicating that a2
is one of a1
when the method is executed. copy. For reference type parameters, such as the
object, when calling the method, its reference user1
is actually used as the actual parameter, then it is passed to the form The reference will be a copy of the reference reference user2
, although this is two references (such as the relationship between a1
and a2
). But it points to the same object, and all operations are for the same object.
EndWe can know through the above analysis.
There is only one way of passing by value in Java, but for reference types, the parameters passed are references to objects.
The above is the detailed content of How to pass value in java?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.
