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 java pass by value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java pass by value" Watch "java pass by value" New topic
Author

java pass by value

thejwal pavithran
Ranch Hand

Joined: Feb 11, 2012
Posts: 118
Hey guys,

I am having difficulty understanding How parameters are passed in java.

heres my code:



so i am passing the address of Hippo h2 to copyHippo through method copy. So when i modify the value of size of copyHippo in the copy method, it gets reflected in the main method, which can be seen by the output of the sop.

I just cant understand why the string variables value is not getting modified. Because isnt it completely similar to the copyHippo reference variable?

but why is the value of s2(null here)passed instead of the address.
I mean copyHippo is a reference variable which points to the same object to which h2 and h1 created in the main method are pointing to.so when i modify the value of instance variables of copyHippo in the copy function, the values of instance variables in the main function gets modified.

Same should be the case for the string variable too right? But what am i missing here?

I need to understand this.please help


on job hunt
jatan bhavsar
Ranch Hand

Joined: Jul 23, 2008
Posts: 297

Hi ,

Refer this . It will solve your doubts.

Regards
jatan
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1031
    
    5

This question is more related to the behaviour of Strings as opposed to passing by value. See here about Strings: http://docs.oracle.com/javase/tutorial/java/data/strings.html. The important fact is that Strings are immutable.
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
James Boswell wrote:This question is more related to the behaviour of Strings as opposed to passing by value. See here about Strings: http://docs.oracle.com/javase/tutorial/java/data/strings.html. The important fact is that Strings are immutable.

Not really. The OP would have got the same result if s1 and s2 had been references to a mutable object.
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Deleted - meant to edit previous post rather than quote it.
 
Don't get me started about those stupid light bulbs.
 
subject: java pass by value