什么时候可以和不能方法调用更改 Java 中的参数? [英] When can and can't method calls change a parameter in Java?

查看:38
本文介绍了什么时候可以和不能方法调用更改 Java 中的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在java中.我试着在这里寻找/谷歌搜索这个问题的答案,但找不到足够的答案,所以如果这是重复的,我深表歉意.我对更改您输入的参数的方法背后的规则感到困惑.例如,考虑下面的例子

public static void messWithArray(int[] array){数组[0] = 100;数组[0] += 5;//创建新数组int[] array2 = new int[10];for(int i = 0; i <10; i++){数组2[i] = 5+i;}数组 = 数组 2}public static void main(String[] args){int[] a = 新的 int[10]for(int i=0; i <10; i++){a[i] = i*10}messWithArray(a);System.out.println(a[0]);System.out.println(a[1]);

这会打印出 a[0] 是 105 并且 a[1] 是 10,因此在 messWithArray 方法中使 array[0] 等于 100 并添加 5 会产生效果.但是,分配 array = array2 没有做任何事情(因为 a[1] 是 10).我也试过搞乱一个 int 但无法让它工作.

我想更具体/清楚地了解某个方法是否会更改数组属性背后的逻辑.

解决方案

在 Java 中,方法参数总是按值传递.然而,所有对象(包括数组,甚至基元数组)都由一个指针引用,它允许通过其方法(或通过数组访问数组)修改对象,并且指针通过值传递.

你永远不能重新设置调用者中引用的参数,但你总是可以改变一些可变的东西.

public void someMethod(int param1, List param2, double[] param3) {//在这里,我可以更改param1"、param2"或param3"所指的内容:参数 1 = 3;//但这并没有改变打电话给我的人的价值.//执行param2 = new ArrayList<>();"同样不会影响调用者.//指向列表的指针被简单地复制.param2.add("你好");//但是,那个 ^ 通过指针修改了列表.//数组也是如此:参数 3[0] = 0.0;//修改原始数组param3 = 新双 [3];//将 'param3' 更改为引用新数组}

This is in java. I tried looking for / googling for an answer to this question here and couldn't find a sufficient answer, so I apologize if this is a duplicate. I am confused as to the rules behind methods changing the parameter that you input. For example, consider the following example

public static void messWithArray(int[] array)
{
     array[0] = 100;
     array[0] += 5;

     // make new array
     int[] array2 = new int[10];
     for(int i = 0; i < 10; i++)
     {
         array2[i] = 5+i;
     }
     array = array2
 }
 public static void main(String[] args)
 {
     int[] a = new int[10]
     for(int i=0; i < 10; i++)
     {
         a[i] = i*10
     }
     messWithArray(a);
     System.out.println(a[0]);
     System.out.println(a[1]);

This prints that a[0] is 105 and a[1] is 10, so making array[0] equal to 100 and adding 5 to it in the messWithArray method had an effect. However, assigning array = array2 didn't do anything (since a[1] is 10). I also tried messing with an int but couldn't get it to work.

I would like to know more specifically/clearly the logic behind whether or not a method will change attributes of an array.

解决方案

In Java, method parameters are always passed by value. However, all objects (which includes arrays, even arrays of primitives) are referred to by a pointer, which allows modification of the object through its methods (or via array access for arrays), and it's the pointer that's passed by value.

You can never re-seat what the parameter referred to in the caller, but you can always mutate something mutable.

public void someMethod(int param1, List<String> param2, double[] param3) {
  // In here, I can change what 'param1', 'param2', or 'param3' refer to:
  param1 = 3;
  // but this didn't change the value from whoever called me.
  // Doing "param2 = new ArrayList<>();" similarly wouldn't affect the caller.
  // The pointer to the list was simply copied.
  param2.add("Hello");
  // However, that ^ modified the list through the pointer.
  // Same is true for arrays:
  param3[0] = 0.0; // modified the original array
  param3 = new double[3]; // changed 'param3' to refer to a new array
}

这篇关于什么时候可以和不能方法调用更改 Java 中的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆