How can you extend Java to introduce passing by reference?

前端 未结 10 2226
天命终不由人
天命终不由人 2020-12-24 06:19

Java is pass-by-value. How could you modify the language to introduce passing by reference (or some equivalent behavior)?

Take for example something like

<         


        
10条回答
  •  死守一世寂寞
    2020-12-24 07:15

    Using AtomicReference class as holder object.

    public static void main(String[] args) {
        String variable="old";
        AtomicReference at=new AtomicReference(variable);
        passByReference(at);
        variable=at.get();
        System.out.println(variable);
    }
    
    public static void passByReference(AtomicReference at) {
      at.set("new");
    }
    

提交回复
热议问题