简体   繁体   English

将params传递给Java中的方法

[英]Passing params to methods in Java

I am confused with one trivial thing - passing parameters to the method and changing their values... I'll better give you some code: 我对一件简单的事情感到困惑 - 将参数传递给方法并更改它们的值......我最好给你一些代码:

public class Test {
  public static void main(String[] args) {
    Integer val = new Integer(41);
    upd(val);
    System.out.println(val);

    Man man = new Man();
    updMan(man);
    System.out.println(man.name);
  }

  static void upd(Integer val) {
    val = new Integer(42);
  }

  static void updMan(Man man) {
    man.name = "Name";
  }

  static class Man {
    String name;
  }
}

Could you explain why the Integer object I've passed is not updated while the Man object is? 你能解释一下为什么我传给的Integer对象在Man对象时没有更新吗? Aren't the Integer and Man objects passed by reference (due to their non-primitive nature)? 是不是通过引用传递的Integer和Man对象(由于它们的非原始性质)?

Because for Integer your are creating a new Object. 因为对于Integer您正在创建一个新对象。 For Man you just change one of its value, the object man stays the same. 对于Man你只需更改其中一个值,对象man保持不变。

Consider the following: 考虑以下:

static void updMan(Man man) {
  man = new Man();
  man.name = "Another Man";
}

This would also not change your initial man . 这也不会改变你的初始man

--EDIT - 编辑

You can "simulate" the mutability of Integer by this: 您可以通过以下方式“模拟” Integer的可变性:

static void upd(Integer val) {
    try {
        Field declaredField = val.getClass().getDeclaredField("value");
        declaredField.setAccessible(true);
        declaredField.set(val, 42);
    } catch (Exception e) {
    }
}

It is called Pass by value . 它被称为Pass by value When you pass some object to dome method, Java creates a copy of reference to that object. 当您将一些对象传递给dome方法时,Java会创建对该对象的引用副本。 If you check object's hashcode right after going into method's body it will be the same as passed object's one. 如果在进入方法体后立即检查对象的哈希码,它将与传递的对象的哈希码相同。 But when you change it inside of method, object changes and reference is no longer points to same object. 但是当你在方法中更改它时,对象更改和引用不再指向同一个对象。

EDIT : code sample 编辑 :代码示例

public class TestPass {

    public static void main(String[] args) {
        String ss= "sample";
        System.out.println("Outside method: "+ss.hashCode());
        method(ss);

    }

    static void method(String s){
        System.out.println("Inside method: "+s.hashCode());
        s+='!';
        System.out.println("Inside method after object change: "+s.hashCode());
    }

}

Output: 输出:

Outside method: -909675094
Inside method: -909675094
Inside method after change: 1864843191

objects as parameters in Java are transferred as a params by copy of reference - so if you change the object via reference copy - you will have your object updated. 作为参数在Java中的对象通过引用副本作为参数传输 - 因此,如果您通过引用副本更改对象 - 您将更新对象。

in case of your upd(Integer val) method you create a new Integer object so that reference copy now point to a new object, not the old one. 如果使用upd(Integer val)方法,则创建一个新的Integer对象,以便引用副本现在指向一个新对象,而不是旧对象。 So changing that object will not affect the original one. 因此,更改该对象不会影响原始对象。

man.name = "Name"; 

实际上,您正在更改同一对象中的某些内容,而不是像在整数情况下那样创建新对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM