Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question about class object arguments

 
Jay Simon
Greenhorn
Posts: 6
Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since class object arguments are passed by reference, I do not understand why the method in the program below does not modify the Integer argument, i.e, why does the variable Y come back as 2 and not 6? Thanks in advance.

public class Test
{
public static void changeNumber(Double number)
{
number =6.0;
System.out.println("After call: number = " + number);
}

public static void main(String[] args)
{
double x = 2.0;
Double Y= x;
changeNumber(Y);
System.out.println("After call: Y = " + Y);

}

}
 
Dan Din
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Jay, welcome!

I guess you are talking about the Double value for number. (btw, you could use code tag for your example).
The response it's the immutability of the wrappers (Double) : actually, in the method changeNumber, when you are doing

a new Object is created and the reference it's associated with number.
But after the method exit, that reference is lost and in the main you got the old one (that points to the value 2.0).

Hope that helps,
D.
 
Jay Simon
Greenhorn
Posts: 6
Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Dan. I forgot about the immutability thing. Same would be true for Strings I assume. I appreciate your help.
 
Ove Lindström
Ranch Hand
Posts: 326
Android Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That actually goes for any object.

 
Jay Simon
Greenhorn
Posts: 6
Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ove, thanks for your input. I am studying your example.
 
Jay Simon
Greenhorn
Posts: 6
Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By way of an introduction: I have spent the past two months intensively trying to get up to speed in C++, Java, and Ruby. (I am not a programming pro - I use Fortran and Matlab mostly for one-of scientific modeling). Ruby is kind of outrageous (and the most fun for me). It is sometimes difficult for me to keep C++ and Java untangled from each other. However studying the two simultaneously also has its advantages. But even now, if I am not alert, I occasionally find myself starting to put together a Java struct!! Anyhow, this is a great resource for those working with Java - you are all very generous to help out! For my part I promise to search previous posts before posting re-hashed questions. Thanks again folks!
 
Hakim Benhamadouche
Greenhorn
Posts: 5
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java primitives or Object references are passed by value meaning you have a copy of the argument in your method.

If it is a primitive (i.e an int), even if you modify it's value inside the method, it won't affect it where the method has been called (try a swap method with two ints, that won't swap them outside the method).
A reference to an object is also no going to be modified (if my parameter ref is a reference to a mutable class, if I do ref = new MutableClass() in the method, the caller won't see that the reference to it's initial Object is pointing to the new MutableInstance().

The only thing you can modify inside a method is the content of a reference to an Object
* if I have a setValue() method in MutableClass, I can change the value member and that will be seen outside the method
* same thing for arrays, you can modify its content but not the memory location it refered to.

If you like the JVM, maybe you should look at the Groovy Language, the grails framework is pretty much like Rails as I heard (you can also check Scala language ;) )

 
Jay Simon
Greenhorn
Posts: 6
Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That clarifies Ove's remarks. Thank you Karim.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic