java - passing a double value by reference

后端 未结 6 2140
后悔当初
后悔当初 2021-01-04 13:58

how can I pass a double value by reference in java?

example:

Double a = 3.0;
Double b = a;
System.out.println(\"a: \"+a+\" b: \"+b);
a = 5.0;
System.         


        
相关标签:
6条回答
  • 2021-01-04 14:10

    The wrapper types are immutable in Java. Once they are created, their value cannot be changed (except via reflection magic ofcourse). Why are you trying to do what you're doing?

    Edit: Just to elaborate, when you set:

    a = 5.0;
    

    What actually ends up happening is something like:

    a = new Double(5.0);
    

    If double were mutable, you could have done something like

    a.setValue(5.0);// this would have worked for mutable objects, but not in this case
    

    You can of course write your own class, but there are a lot of pitfalls here, so it's best to explain you're goal

    0 讨论(0)
  • 2021-01-04 14:15

    You can try MutableDouble from commons-lang.

    But not that this has nothing to do with pass-by-reference - you are not passing anything.

    0 讨论(0)
  • 2021-01-04 14:18

    Java doesn't support pointers, so you can't point to a's memory directly (as in C / C++).

    Java does support references, but references are only references to Objects. Native (built-in) types cannot be referenced. So when you executed (autoboxing converted the code for you to the following).

    Double a = new Double(3.0);

    That means that when you execute

    Double b = a;
    

    you gain a reference to a's Object. When you opt to change a (autoboxing will eventually convert your code above to this)

    a = new Double(5.0);
    

    Which won't impact b's reference to the previously created new Double(3.0). In other words, you can't impact b's reference by manipulating a directly (or there's no "action at a distance" in Java).

    That said, there are other solutions

    public class MutableDouble() {
    
       private double value;
    
       public MutableDouble(double value) {
         this.value = value;
       }
    
       public double getValue() {
         return this.value;
       }
    
       public void setValue(double value) {
         this.value = value;
       }
     }
    
     MutableDouble a = new MutableDouble(3.0);
     MutableDouble b = a;
    
     a.setValue(5.0);
     b.getValue(); // equals 5.0
    
    0 讨论(0)
  • 2021-01-04 14:21

    Double is immutable in Java. The assignment a = 5.0 is not changing the Double value of a. It is actually creating an entirely new Double object and changing the value of the pointer that a holds to point at that new object. If you want to be able to change the value without changing the reference, you need to make a mutable Double.

    public class MutableDouble {
      private Double value;
    
      public Double getValue() {
        return value;
      }
    
      public void setValue(Double value) {
        this.value = value;
      }
    
      @Override
      public String toString() {
        return value != null ? value.toString() : "null";
      }
    }
    

    Then you would call a.setValue(5.0); and both would change.

    0 讨论(0)
  • 2021-01-04 14:29

    @Title itself: You can't without "dirty" tricks. The easiest method is to pass an array - or a class containing the field. Changes to those will be reflected just fine.

    But your problem is something completely different: Doubles are immutable, so every change to it will return a new value. There's nothing you can do about that, apart from implementing your own class.

    0 讨论(0)
  • 2021-01-04 14:31

    You can not. In java you can not select the way (by value/by ref) by which parameters will be passed to a method.

    0 讨论(0)
提交回复
热议问题