简体   繁体   English

void ref方法中的Java参考变量

[英]java reference variable in void ref method

I am trying to figure out how to access the sb variable in the void ref method. 我试图弄清楚如何在void ref方法中访问sb变量。 Is it possible? 可能吗? This problem came up as I prepare for a test. 我准备测试时出现了这个问题。

public class X{
  public void ref(String refStr, StringBuilder refSB){
    refStr = refStr + refSB.toString();
    refSB.append(refStr);
    refStr = null;
    refSB = null;
    //how can I access main variable sb here?  Not possible?
    //sb.append(str);
  }
  public static void main(String[] args){
    String s = "myString";
    StringBuilder sb = new StringBuilder("-myStringBuilder-");
    new X().ref(s, sb);
    System.out.println("s="+s+" sb="+sb);
  }
}

You're assigning a null to the reference value, this makes it point nowhere. 您正在为参考值分配一个null ,这使其指向无处。 When you pass a parameter, you're passing it by reference (memory pointer). 传递参数时,将通过引用(内存指针)传递它。 Assigning a new value or null changes the reference, but not the memory object it points to. 分配新值或null更改引用,但不会更改它指向的内存对象。

So, you can work with the StringBuilder within the method and it will keep your changes outside the method, but you cannot assign something else to the pointer (because the pointer itself is local to the method). 因此,您可以在方法中使用StringBuilder ,它将更改保留在方法之外,但是您不能将其他内容分配给指针(因为指针本身是方法的局部变量)。

For example: 例如:

public static void ref (StringBuilder refSB) {
  refSB.append("addedWithinRefMethod");  // This works, you're using the object passed by ref
  refSB = null;  // This will not work because you're changing the pointer, not the actual object
}

public static void main(String[] args) {
  StringBuilder sb = new StringBuilder();
  ref(sb);
  System.out.println(sb.toString());  // Will print "addedWithinRefMethod".
}

To make the code do what you want, you need to use one more derreference, for example using an array: 为了使代码能够执行您想要的操作,您需要再使用一次引用,例如使用数组:

public static void ref(StringBuilder[] refSB) {
  refSB[0] = null;  // This works, outside the method the value will still be null
}

public static void main(String[] args) {
  StringBuilder[] sb = new StringBuilder[] { new StringBuilder() };
  ref(sb);
  System.out.println(sb[0]);  // Will print "null"
}

However, keep in mind that side effects (a method that changes the objects defined outside it) is generally considered bad practice and avoided when possible. 但是,请记住,副作用(一种更改其外部定义的对象的方法)通常被认为是不好的做法,并在可能的情况下避免使用。

Yes,it is possible to use sb reference in void ref() method.You are actually passing sb reference to ref() using new X().ref(s, sb); 是的,可以在void ref()方法中使用sb引用。您实际上是使用new X().ref(s, sb);sb引用传递给ref() new X().ref(s, sb); And in 而在

 public void ref(String refStr, StringBuilder refSB){
    refStr = refStr + refSB.toString();
    refSB.append(refStr);
    refStr = null;

    //USe refSB variable here
    refSB.append(str);
  }

Don't do this refSB = null;. 不要这样做refSB = null;.

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

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