• Post Reply Bookmark Topic Watch Topic
  • New Topic

pass by value/pass by referece  RSS feed

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,

can any one give clear description about passing an object to a function is
by value or by reference,


even though i read in some online documents that passin an object is always by value.

i want information when i pass an object in a single JVM and when i pass an object to multiple JVM ,if possible with an exampl.

if i pass an object by value when i ake changes to an object in fuction it is changing so it is reflecting toward ,so can any one clarify my doubt reg this when i pass an object in a single jvm and multiple jvm.

regards
anish
 
Ranch Hand
Posts: 445
Android Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want information when i pass an object in a single JVM and when i pass an object to multiple JVM ,if possible with an exampl.


SIngle JVM and multiple JVM........!!! I feel vague with your question.....!!! ...
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't "pass an object to a JVM" in the first place. A running object (or static method) creates objects (instances of classes) in each JVM. The nearest you can get to sending objects to JVMs is to serialise objects and pass the serialised form around.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer to the question about references/ values is: in Java you always pass a copy of the original variable to a method.

In case of primitves (boolean,char,byte,int,long,float,double...) this means just a copy of the original value, which in turn means that whatever you do with it inside the method won't be reflected in the original value of the passed variable after the method returns.

But in case of objects, you actually pass a copy of the reference variable (a variable, whose value points to an object on the heap), which means that the method is modifying the object itself (the copied reference still refers to the same 'address' on the heap). On the other hand, if you assign something else to the copied reference variable within the method, the original reference variable will still refer to the same object.

As already stated, there is no such thing as passing arguments to JVM, neither by value, nor by reference
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by anish reddy:
hello,

can any one give clear description about passing an object to a function is
by value or by reference,


even though i read in some online documents that passin an object is always by value.

i want information when i pass an object in a single JVM and when i pass an object to multiple JVM ,if possible with an exampl.

if i pass an object by value when i ake changes to an object in fuction it is changing so it is reflecting toward ,so can any one clarify my doubt reg this when i pass an object in a single jvm and multiple jvm.

regards
anish


Hello
The call by reference mechanism is useful in object-oriented programming,it
permits the manipulation of objects by reference and eliminates the copying of object parameters back-forth.It is also important to note that the references can be created not only for built in datatypes but also for user defined data types such as structure and classes.Refernces work wonderfully well with these user defined data types.
In traditioal C, a function call passes arguements by value.The called function creates a new set of variaables aand copies the values of arguements into them.The function does not have to acces th actual variables in the calling program and can only work on copies of values.This mechanism is fine if the function does not need to alter the values of the original variables in the calling program But there may arise situations where we would like to change the values of variables in the callling program
For Example in bubblesort, we compare two adjacent elements in the list and interchange their values if the first is greater than the second ,If a function is used for bubbleort then it should be able to alter the values of variables in the calilling function which is not possible, if call by value method is used.
Hope this clears the clarification
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C, C++ and Java all work on pass-by-value. Not sure about C#.
There was another recent thread on a similar subject (here). Note the reference to links in an older thread, which are well worth reading; one shows that (I think) Pascal is pass-by-reference and shows how you can alter the value of objects by passing the reference. You can try the same in Java, but it won't work.

For sorting algorithms, you pass the value of a reference to the whole array; although you cannot change the object, you can manipulate its state. You cannot alter an array by passing it to a method, but you alter the values of its members.
And if you want a simpler explanation, ask Rob to transfer this thread to beginners'.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This link would definitely help.. http://www.javaranch.com/campfire/StoryPassBy.jsp
 
C.R.Muthukumar
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by anish reddy:
hello,

can any one give clear description about passing an object to a function is
by value or by reference,


even though i read in some online documents that passin an object is always by value.

i want information when i pass an object in a single JVM and when i pass an object to multiple JVM ,if possible with an exampl.

if i pass an object by value when i ake changes to an object in fuction it is changing so it is reflecting toward ,so can any one clarify my doubt reg this when i pass an object in a single jvm and multiple jvm.

regards
anish

Hello:
Many JavaPersons speak about this topic that is clarified here

In java all arguments of the primitive datatype are passed by value,which means that the original value of the argumen cannot be altered by the called method .The called method gets only a copy of the variable.This means that any change made to object by the called method is not reflected in the calling method
call by value:Example

call By Refernce:Example
Arguments that are objects are passed by reference to the called method.This means that any change made to the object by the cacalled method is reflected


[edit]Corrected code tags. CR[/edit]
[ July 24, 2008: Message edited by: Campbell Ritchie ]
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Disagree. You are not passing the object by reference, but by value. You cannot change the object. The fact that the object takes a field with it which you can change is evidence of bad object-oriented design, not evidence of pass-by-value.
And I can't see your 2nd example compiling; you are trying to pass a String object to a Person reference.

Please go back to my post on 22nd July, and Raghavan Muthu's post, and read what those links say. Particularly go from this old thread to this older link.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!