Redian新闻
>
请教:怎样函数里改变一个Double变量的值?
avatar
请教:怎样函数里改变一个Double变量的值?# Java - 爪哇娇娃
h*y
1
我想在函数里改变一个Double变量的值,可是这下面的函数始终打印2.0而不是0.3。好
像java是新创建了一个对象,而不是去改变原来对象的值。请问各位大牛,怎样修改一
个Double 变量的值啊?多谢拉!!!
public class test {
public static void main(String [] args){
Double dObj = new Double(2.0);
test tt=new test();
tt.change(dObj);
System.out.println("Double " + dObj);
}

public void change(Double dObj) {
dObj = 0.3;
}
}
avatar
m*t
2
Short answer: you can't.
Long answer: java parameters are passed by value. when you call change(dObj)
, the reference to dObj is passed by value. So when you assign a different
reference to it from within change(), it gets lost when the method returns.
On top of that, Double is an immutable type, so you can't change its value.
You can return a new Double object from change(), or you can (warning: bad
practice):
change(Double[] pseudoPointer) {
pseudoPointer[0] = 0.3;
}
avatar
h*y
3
多谢魔法大师!
这么说来,如果我想要通过调用函数改变2个Number变量,我就要:
1 定义一个myNumber 类用来传给函数
或者 2 放在数组或者structure里面传
avatar
A*o
4
or just make the function return another Double...
avatar
t*6
5
use global variable!

【在 h*********y 的大作中提到】
: 多谢魔法大师!
: 这么说来,如果我想要通过调用函数改变2个Number变量,我就要:
: 1 定义一个myNumber 类用来传给函数
: 或者 2 放在数组或者structure里面传

avatar
m*t
6

That's what I would do.
You could use an array, but I for one think it's bad practice, because it
pretty much ruins the API.

【在 h*********y 的大作中提到】
: 多谢魔法大师!
: 这么说来,如果我想要通过调用函数改变2个Number变量,我就要:
: 1 定义一个myNumber 类用来传给函数
: 或者 2 放在数组或者structure里面传

avatar
g*g
7
I don't see anything wrong doing
int[] modify(int[]) {
//process your int[] and return it,
//if original should not be touched, make a copy first
}

【在 m******t 的大作中提到】
:
: That's what I would do.
: You could use an array, but I for one think it's bad practice, because it
: pretty much ruins the API.

avatar
m*t
8

Nothing wrong with that, if what you are processing is a _real_ array. OP
only meantions "2 Number variables". I just wanted to make the point that
if those two are independent scalar variables, it'd be bad practice in an OO
language to pass them using an array rather than a proper bean type.

【在 g*****g 的大作中提到】
: I don't see anything wrong doing
: int[] modify(int[]) {
: //process your int[] and return it,
: //if original should not be touched, make a copy first
: }

avatar
h*y
9
多谢各位!学到了很多哦!
avatar
y*i
10
what is your major? not from CS ba?
avatar
r*l
11
To make it better, you can use interface, like
the following pseudo code:
public Interface ModifiableDouble {
public setValue(Double value);
pulibc Double getValue();
}
and have your wrapper class implement this
interface. The interface will be in your
method signature.
It seems redundant to use interface here, while
you will find the beauty of using it later.
avatar
h*y
12
Actually I am from CS ;)
I used C++ before. Everybody says Java is much easier than C++, but it looks
like Java is not powerful enough, even for such a easy function, I need a
wrapper class like reaBull mentioned.

【在 y****i 的大作中提到】
: what is your major? not from CS ba?
avatar
r*l
13
Well, Java and C++ are not comparable. They focus on
different markets.
Immutable objects have advantages, like:
- thread safe, hence better performance
- sample cache
One example, immutable objects can be used as the keys
of a map w/o worrying too much. If you use mutable
objects as key, you will have huge headache.
A good Java practice: if a class can be made immutable,
then make it immutable. For all classes, it's good to
make them less mutable.

looks

【在 h*********y 的大作中提到】
: Actually I am from CS ;)
: I used C++ before. Everybody says Java is much easier than C++, but it looks
: like Java is not powerful enough, even for such a easy function, I need a
: wrapper class like reaBull mentioned.

avatar
g*g
14
You are wrong, Double is designed to be an immutable class for
efficiency. If you want a mutable Double class, create a MutableDouble class
should be trivial.
Java is not powerful enough for writing an OS. But enough for
most application programming.

looks

【在 h*********y 的大作中提到】
: Actually I am from CS ;)
: I used C++ before. Everybody says Java is much easier than C++, but it looks
: like Java is not powerful enough, even for such a easy function, I need a
: wrapper class like reaBull mentioned.

avatar
m*t
15

looks
You must unlearn what you have learned. (I think Yoda said that...)
You feel the need of a MutableDouble because you are thinking in C/C++'s "
here's a pointer and feel free to mess with what it points to" mentality.

【在 h*********y 的大作中提到】
: Actually I am from CS ;)
: I used C++ before. Everybody says Java is much easier than C++, but it looks
: like Java is not powerful enough, even for such a easy function, I need a
: wrapper class like reaBull mentioned.

avatar
t*k
16
这个贴子上看一点不象是CS的啊
而且还是接触过C++的
接触过C的人都应该知道变量作用域变量覆盖吧

【在 h*********y 的大作中提到】
: 我想在函数里改变一个Double变量的值,可是这下面的函数始终打印2.0而不是0.3。好
: 像java是新创建了一个对象,而不是去改变原来对象的值。请问各位大牛,怎样修改一
: 个Double 变量的值啊?多谢拉!!!
: public class test {
: public static void main(String [] args){
: Double dObj = new Double(2.0);
: test tt=new test();
: tt.change(dObj);
: System.out.println("Double " + dObj);
: }

avatar
e*e
17
The above code messed up.
Put in this way:
public class test {
private Double dObj;

protected test() {
dObj = new Double(2.0);
}

public void change() {
this.dObj = new Double(0.3);
}

Double getObj() {
return dObj;
}

public static void main(String [] args){

test tt=new test();
tt.change();
System.out.println("Double " + tt.getObj());
}

}
avatar
e*e
18
The reason the original code isn't working is when change() routine is being
invoked, the parameter dObj is passed by a copy of the dObj object, so the
dObj in the main routine isn't changed. But the dObj (actually a new dObj
object)in change() routine is. You can print this dObj insdie change() to
shwo it does being changed to 0.3
You can also do this way. But I don't recommend. It's still messed up, not a good way Java does.
public class test {
public static void main(String [] args){
avatar
e*e
19
This is the Java way to do.
public class test {
public class MyDouble {
double d;

protected MyDouble() {
this(0.0);
}

protected MyDouble(double value) {
d = value;
}

public void setValue(double newValue) {
d = newValue;
}

public double getValue() {
return d;
}

public String toString() {
return new Double(d).toStrin
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。