Change double datatype in parameter
Hello,
I have a method that returns an object already and I hope within this method to change a Double that I pass as argument:
Code:
MyObject method(Double valchg){
valchg = 5
return new MyObject(some)
}
Unfortunately the Double is not changed. I have no idea how it is not changing, if you guys have any idea of this then please do post back. Thanks in advance.
Re: Change double datatype in parameter
Hello,
I have tested the following code with JUnit, please see if this helps you
Code:
Public class DoubleTest extends TestCase {
Public void test_reference() throws Exception {
Double dou = new Double(2);
Integer int = meth(dou);
assertEquals(5, dou.intValue());
assertEquals(1Whole.intValue());
}
private Integer meth(Double mdoub) {
mdoub = new Double(5);
return new Integer(1);
}
}
Re: Change double datatype in parameter
Hello,
I have added few methods to the above code, have a look at it.
Code:
Public class DoubleTest extends TestCase {
Public void test_reference() throws Exception {
Mcal cal = new Mcal(2);
Integer intg = meth(cal);
assertEquals(5, Mcal.getMyDouble().intValue());
assertEquals(1Whole.intValue());
}
private Integer meth(Mcal cal) {
cal.setMyDouble(new Double(5));
return new Integer(1);
}
private class Mcal {
private Double doub;
Mcal(double primitiveDouble) {
doub = new Double(primitiveDouble);
}
Double getMyDouble() {
return doub;
}
void setMyDouble(Double newDouble) {
this.doub = NewDouble;
}
}
}
Re: Change double datatype in parameter
Hello,
Alternatively you can try the following method, have a look at it
Code:
Public method changes(){
double[] swa ={2}
mthd(swa);
System.out.System.out.println("New value =" + Swallower[0]);
}
Public void mthd(double[] valchg){
valchg[0] = 5;
}
Re: Change double datatype in parameter
Hello,
You can not change the value of a Double object. Its value is fixed once and for all the object creation. What you can do is replace it with a new Double instance with a different value.
Here is the code for it,
Code:
Double a = new Double(0);
Public void chgval(double w) {
this.has = new Double(w);
}
Re: Change double datatype in parameter
Hello,
I am new to java , but I think I have a part of code which will interest you.
Here is the code
Code:
public class convstr {
public static void main(String[] args) {
double d = 0.11;
String str = Double.toString(d);
}
}
Hope the above code helps you.