简体   繁体   English

java - 通过引用传递double值

[英]java - passing a double value by reference

how can I pass a double value by reference in java? 如何在java中通过引用传递double值?

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; 我的目标:我正在jess中编写一个专家系统,在该应用程序中,一个段的长度将具有双倍值,现在双值不在单个段中; 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. 如果我不能改变那个双,我就不能有一个包含引用该double值的对象的某个规则fire。

Java doesn't support pointers, so you can't point to a's memory directly (as in C / C++). Java不支持指针,因此您无法直接指向内存(如在C / C ++中)。

Java does support references, but references are only references to Objects. Java确实支持引用,但引用只是对象的引用。 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); 双a =新双(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时(自动装箱最终会将您的代码转换为此代码)

a = new Double(5.0);

Which won't impact b's reference to the previously created new Double(3.0) . 这不会影响b对先前创建的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). 换句话说,你不能通过直接操作(或者在Java中没有“远处的动作”)来影响b的引用。

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. 包装器类型在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 如果double是可变的,你可以做类似的事情

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. 您可以从commons-lang尝试MutableDouble

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. @Title本身:你不能没有“肮脏”的伎俩。 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. Double在Java中是不可变的。 The assignment a = 5.0 is not changing the Double value of a. 赋值a = 5.0不会改变a的Double值。 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. 它实际上是创建一个全新的Double对象并更改指针的值,该指针指向该新对象。 If you want to be able to change the value without changing the reference, you need to make a mutable Double. 如果您希望能够在不更改引用的情况下更改值,则需要创建一个可变的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); 然后你会调用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. 在java中,您无法选择将参数传递给方法的方式(按值/按ref)。

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

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