It is the reference to the object that is passed by value.
In the setNewValue
method, you change the reference, so the changeMe
variable points to something else. The (reference to the) original value
object in the main
method is not changed.
Passing parametars by ref, double and Double in JAVA
-
01-09-2022 - |
Question
If someone could help me understand the following problem:
As I understand double
is primitive data type in Java and Double
is immutable class.
Every parameter is passed by value. If it is primitive type (int
, double
, byte
), then it is the actual value that is passed; and if it is an object type, then it is the address to that object that is copied.
If that is so, why this parameter that is of type Double
is not changed?
...
public static void main(String[] args) {
Double value = new Double(0);
SomeObj so = new SomeObj();
so.setNewValue(value);
System.out.println(value);
}
...
public class SomeObj {
public void setNewValue(Double changeMe)
{
changeMe = new Double(10.0);
}
}
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow