迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Java >

在 Java 中数组是以值传递还是以引用传递

作者:迹忆客 最近更新:2023/09/11 浏览次数:

本教程介绍了在 Java 中按值或引用传递的数组。

当我们调用一个方法时,它的参数可以作为值或引用传递。在讨论这两个问题之前,让我们先了解两个术语:

  • callee:这是另一个方法调用的方法
  • caller:这是调用另一个方法的方法

现在让我们了解什么是按值传递和按引用传递。


什么是 Java 中的按值传递

当我们说参数是按值传递时,实际参数的值在内存中被复制了。

调用者和被调用者维护两个具有相同值的自变量。如果被调用者修改了参数值,我们看不到调用者的变化。

要点:

  • 被调用者无权访问调用代码中的原始元素。
  • 将数据(参数值)的副本发送给被调用者。
  • 对传递变量的更改不会影响最终值。

什么是 Java 中的按引用传递

当我们说参数通过引用传递(也称为按地址传递)时,它意味着将调用函数中的参数的引用传递给相应的形参。

在这种情况下,会制作实际参数地址的副本。调用者和被调用者对参数使用相同的变量,因此,如果被调用者修改参数变量,我们可以看到调用者变量的变化。

要点:

  • 被调用者引用调用代码中的编程元素。
  • 传递存储数据的内存地址而不是值。
  • 对值的任何更改都会影响原始数据

Java 是按值传递的

与 C++ 不同,在 Java 中,我们只能将参数作为值传递。但是有一个问题:当对象作为参数传递时,它们的引用作为值传递给被调用者。

换句话说,即使 Java 严格按值传递,它的行为与对象的引用传递一样。让我们用一些例子来更好地理解这个概念。

在 Java 中传递原始数据类型

在下面的代码中,我们将两个整数作为参数传递给函数,然后我们更改它们的值。

由于在 Java 中参数是按值传递的,因此该函数维护了一个单独的参数副本。我们对参数所做的任何更改都只在函数副本中进行,而不是在主函数(调用者)维护的副本中。

因此,我们在函数调用完成后打印值时得到原始值。看下面的示例代码:

public class SimpleTesting {
    static void JE_function(int n1, int n2){
        n1 = -232;
        n2 = -90;
    }
    
    public static void main(String args[]) {
        int JE_numa = 6932;
        int JE_numb = 8934;
        System.out.println("Numbers before function call :" +JE_numa + " and " +JE_numb);
        JE_function(JE_numa, JE_numb);
        System.out.println("Numbers after the JE_function call :" +JE_numa + " and " +JE_numb);
    }
}

输出:

Numbers before function call:6932 and 8934
Numbers after the JE_function call:6932 and 8934

在 Java 中传递数组

当涉及到对象时,对对象的引用是通过值作为参数传递的。

在下面的代码中,我们传递一个数组作为参数,当我们打印对象(数组)的值时,会打印一个内存位置。这与对象变量存储存储对象的内存位置有关。

看下面的示例代码:

public class SimpleTesting {
    static void JE_function(String[] sentence){
        System.out.println(sentence);
    }
    
    public static void main(String args[]) {
        String[] sent = {"I", "Love", "Delftstack"};       
        JE_function(sent);       
    }
}

输出:

[Ljava.lang.String;@442d9b6e

使用上面的代码,我们尝试传达对作为参数传递的数组的引用。因此,如果我们更改数组的值(不是内存位置),更改将反映在被调用方中。

看下面的示例代码:

public class SimpleTesting {
    static void JE_function(String[] sentence){
        sentence[1] = "Visit";
    }
    
    public static void main(String args[]) {
        String[] sent = {"I", "Love", "Delftstack"};
        System.out.println("Sentence before calling the function");
        for( String word: sent){
            System.out.println(word);
        }
        JE_function(sent);
        System.out.println("Sentence after calling the function");
        for( String word: sent){
            System.out.println(word);
        }
        
    }
}

输出:

Sentence before calling the function
I
Love
Delftstack
Sentence after calling the function
I
Visit
Delftstack

从上面的代码可以看出,对数组的任何更改都会反映在主块中。

把它想象成两个朋友正在做一个项目。朋友所做的任何工作都会反映在整个项目中。

在这种情况下,该函数引用主块使用的相同数组。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

Pandas 追加数据到 CSV 中

发布时间:2024/04/24 浏览次数:352 分类:Python

本教程演示了如何在追加模式下使用 to_csv()向现有的 CSV 文件添加数据。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便