java - passing a double value by reference

后端 未结 6 2257
后悔当初
后悔当初 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: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.

提交回复
热议问题