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

Is Java value passing or reference passing?

2025-02-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "Java is value passing or reference passing", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "Java is value passing or reference passing"!

1. value type

In layman's terms, value types refer to the eight basic data types in Java:

integer type: byte, int, short, long

Float: float, double

Character type: char

Boolean type: boolean

From the JVM level: the so-called value type refers to the type of value generated directly in the stack (Java virtual machine stack) when assigning, as shown in the following figure:

2. reference type

Reference types refer to data types other than value types, such as:

class

interface

array

string

wrapper class (Integer, Double...)

从 JVM 的层面来讲,所谓的引用类型是指,在初始化时将引用生成栈上,而值生成在堆上的这些数据类型,如下图所示:

3.值传递

值传递(Pass By Value)指的是方法传参时,传递的是原内容的副本,因此对副本进行如何修改都不会影响原内容。

实现代码如下:

public class PassTest { public static void main(String[] args) { int age = 18; System.out.println("调用方法前:" + age); intTest(age); System.out.println("调用方法后:" + age); } private static void intTest(int age) { age = 30; System.out.println("方法中修改为:" + age); } }

程序的执行结果为:

调用方法前:18

方法中修改为:30

调用方法后:18

从上述结果可以看出,在方法中修改参数并未影响原内容,我们把这种传参方式称之为值传递。

4.引用传递

引用传递(Pass By Reference)指的是方法传参时,传递的是参数本身,因此对参数进行任意修改都会影响原内容。

模拟"引用传递"的实现代码如下:

public class PassTest { public static void main(String[] args) { char[] name = {'磊', '哥'}; System.out.println("调用方法前:" + new String(name)); paramTest(name); System.out.println("调用方法后:" + new String(name)); } private static void paramTest(char[] n) { n[1] = '神'; System.out.println("方法中修改为:" + new String(n)); } }

程序的执行结果为:

调用方法前:磊哥

方法中修改为:磊神

调用方法后:磊神

从上述的结果可以看出在 paramTest 方法中修改了参数之后,在 main 方法中再打印参数时,发现参数的值也跟着发生了改变,那么似乎我们可以得出结论,Java 中貌似也有"引用传递",然而实事并如此,我们接着看。

5.真假"引用传递"

我们给上面的代码添加一行,如下所示:

public class PassByValue { public static void main(String[] args) { char[] name = {'磊', '哥'}; System.out.println("调用方法前:" + new String(name)); paramTest(name); System.out.println("调用方法后:" + new String(name)); } private static void paramTest(char[] n) { n = new char[2]; // 添加此行代码 n[1] = '神'; System.out.println("方法中修改为:" + new String(n)); } }

程序的执行结果为:

调用方法前:磊哥

方法中修改为:神

调用方法后:磊哥

从上述结果可以看出,当我们在 paramTest 方法中添加 new char[] 之后,"引用传递"就突然变值传递了?为什么?

这是因为,在 Java 语言中本质上只有值传递,也就说 Java 的传参只会传递它的副本,并不会传递参数本身。

前面那个带引号的"引用传递"其实只是传递了它的引用副本,如下图所示:

所以我们在调用 new char[] 之后,可以看出 n 对象有了新地址,而原内容并未被修改,如果按照引用传递的思路来看的话,不管执行任何方式的修改都会改变原内容,因此我们可以更加确认 Java 语言中只有值传递,如下图所示:

到此,相信大家对"Java是值传递还是引用传递"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

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