由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - 请教:怎样函数里改变一个Double变量的值?
相关主题
问个primitive type的问题新手问一个mutable的问题
多线程的一个基础问题一个Java程序员的话(3)
NullPointerException 问题List, LinkedList and Vector
Java 问题BufferedWriter里的write()
functional programming why? (转载)immutable list
Re: for help请问有没有generic的array
这个闭包怎么写?能否让函数返回一个用于赋值的引用
ArrayList vs Array, StringBuffer vs String, 大侠们给讲讲有modify parameter, or return?
相关话题的讨论汇总
话题: double话题: dobj话题: public话题: java话题: class
进入Java版参与讨论
1 (共1页)
h*********y
发帖数: 235
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;
}
}
m******t
发帖数: 2416
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;
}
h*********y
发帖数: 235
3
多谢魔法大师!
这么说来,如果我想要通过调用函数改变2个Number变量,我就要:
1 定义一个myNumber 类用来传给函数
或者 2 放在数组或者structure里面传
A**o
发帖数: 1550
4
or just make the function return another Double...
t*****6
发帖数: 12
5
use global variable!

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

m******t
发帖数: 2416
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里面传

g*****g
发帖数: 34805
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.

m******t
发帖数: 2416
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
: }

h*********y
发帖数: 235
9
多谢各位!学到了很多哦!
y****i
发帖数: 156
10
what is your major? not from CS ba?
相关主题
Re: for help新手问一个mutable的问题
这个闭包怎么写?一个Java程序员的话(3)
ArrayList vs Array, StringBuffer vs String, 大侠们给讲讲有List, LinkedList and Vector
进入Java版参与讨论
r*****l
发帖数: 2859
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.
h*********y
发帖数: 235
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?
r*****l
发帖数: 2859
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.

g*****g
发帖数: 34805
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.

m******t
发帖数: 2416
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.

t********k
发帖数: 808
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);
: }

e****e
发帖数: 418
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());
}

}
e****e
发帖数: 418
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){
e****e
发帖数: 418
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
1 (共1页)
进入Java版参与讨论
相关主题
modify parameter, or return?functional programming why? (转载)
java 的函数 xxx(a, b,c)能够向a 写入数据吗?Re: for help
一个简单的关于java Map的问题这个闭包怎么写?
这周抽空研究了一下Nathan的那篇CAP的blogArrayList vs Array, StringBuffer vs String, 大侠们给讲讲有
问个primitive type的问题新手问一个mutable的问题
多线程的一个基础问题一个Java程序员的话(3)
NullPointerException 问题List, LinkedList and Vector
Java 问题BufferedWriter里的write()
相关话题的讨论汇总
话题: double话题: dobj话题: public话题: java话题: class