File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes question about class object arguments Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "question about class object arguments" Watch "question about class object arguments" New topic
Author

question about class object arguments

Jay Simon
Greenhorn

Joined: Jul 05, 2011
Posts: 6

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

Joined: Jun 06, 2011
Posts: 12
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

Joined: Jul 05, 2011
Posts: 6

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

Joined: Mar 10, 2008
Posts: 326

That actually goes for any object.

Jay Simon
Greenhorn

Joined: Jul 05, 2011
Posts: 6

Ove, thanks for your input. I am studying your example.
Jay Simon
Greenhorn

Joined: Jul 05, 2011
Posts: 6

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!
Karim Hamamouch
Greenhorn

Joined: Jun 07, 2011
Posts: 5

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

Joined: Jul 05, 2011
Posts: 6

That clarifies Ove's remarks. Thank you Karim.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question about class object arguments
 
Similar Threads
char
EJB3 Client Not Running
0.1 + 0.2 is not equal 0.3
Why does x=x++ cause infinite loop?
Scope coding question