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!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于设计模式的相关问题,主要将装饰器模式的相关内容,指在不改变现有对象结构的情况下,动态地给该对象增加一些职责的模式,希望对大家有帮助。


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

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

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!
