开发者

java - passing a double value by reference

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.out.println("a: "+a+" b: "+b);

this code prints:

a: 3 b: 3
a开发者_如何学编程: 5 b: 3

and my problem is to get it to print:

a: 3 b: 3
a: 5 b: 5

my goal: I'm writing an expert system in jess, in that application a segment would have a double value for it's length, now that double value isn't in a single segment; it's in many other segments, proportionality classes..etc, all of which are referencing to it, waiting it to change so that they could possibly meet some rules.

if I can't get that double to change, I can't have a certain rule fire which contains an object that references to that double value.


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


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


You can try MutableDouble from commons-lang.

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


@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.


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜