Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

regarding pass by reference  RSS feed

 
Kaush Kane
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class A{
public static void main(String args[]){
Object x = null;
A.giveMeAString (x);
System.out.println ("In main" + x);
}
static void giveMeAString (Object y)
{
y = new Integer(2);
System.out.println ("In giveMeAString" + y);
}
}


i m getting the o/p:
In giveMeAString 2
In main null

As in java objects are passed by reference, the value of object pointed by x must change when i change the value of object pointed by y.
Then why doen't this hold true in above case.
Please clear my doubt.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Object references are not passed by reference. They are passed by value. This means that the formal parameter in the method becomes a copy of the actual parameter. So what you are doing inside the method is changing the copy. You aren't changing the original reference.
[ June 07, 2006: Message edited by: Keith Lynn ]
 
Kaush Kane
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Object references are not passed by reference

When u say Object references r u talking about the references of Object class or in general object references of any class??
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A reference to any object will be passed by value.

I'm sorry for the confusion. I only made the letter o uppercase because it was at the beginning of the sentence.
[ June 07, 2006: Message edited by: Keith Lynn ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in your example, you are not changing the object that y is pointing to, your changing which object y points to.

so, x refers to your original null.

at the start of your method, y also points to null. but then you say "hey, i want y to refer to a brand new Integer type object, which i will create right now.

this does not in any way change what x is referring to. it still refers to the same thing it always did, in this case, still null.
 
Kaush Kane
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. Yaa I got the point.

But is there any way to perform pass by reference??
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java only has one means of parameter passing, and that is by value.
[ June 07, 2006: Message edited by: Keith Lynn ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kaush Kane:
Ok. Yaa I got the point.

But is there any way to perform pass by reference??


No, there is no pass by reference in Java. There is only pass by value.
 
Kaush Kane
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for so many replies. I really love this group.
plz bare with, I have one more doubt.

is call by value and pass by value one and the same thing?
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
call by value is an incorrect phrase usually used when meaning pass by value, yes.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kaush Kane:

is call by value and pass by value one and the same thing?


Yep.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pendantically speaking, there's no way to get pass-by-reference because, as formally define, Java only have pass-by-value. Practically speaking, it's trivial to achieve the equivalent effect in code:
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Practically speaking, it's trivial to achieve the equivalent effect in code

This is incorrect, it is not achievable. You might be referring to the restriction of scope such that if you ignore certain aspects, then you can observe the same effect. The context that is typically referred to in the usual "pass by reference/value confusion" cannot have "pass by reference practically (or any other magic adverb) achieved".

The JLS makes this quite clear. http://java.sun.com/docs/books/vmspec/2nd-edition/html/Concepts.doc.html#26454

Also of interest might be:
What is the difference between an object and an object reference?
http://jqa.tmorris.net/GetQAndA.action?qids=75&showAnswers=true

Does Java pass method parameters by reference, by value, both or neither?
http://jqa.tmorris.net/GetQAndA.action?qids=37&showAnswers=true
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everything in Java is pass by value, there is nothing you can do about it. However, you can get the behavior of pass by reference with a mutable objects, as long as the method you passed the value to, doesn't create a new object, but change the existing one. Despite that, it is important to remember that it pass by value.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

you can get the behavior of pass by reference with a mutable objects

I'll repeat since this is a beginner's forum - with the potential to mislead those who do not know any better. The above statement is incorrect and is a very common misconception. You cannot even get "kind of" behaviour or "practically the behaviour of" or "literally the behaviour of" or any magical adverb or adjective that will legitimise the statement. It is blatantly false - this is easily demonstrated and I encourage it as an exercise.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tony Morris:

This is incorrect, it is not achievable. You might be referring to the restriction of scope such that if you ignore certain aspects, then you can observe the same effect. The context that is typically referred to in the usual "pass by reference/value confusion" cannot have "pass by reference practically (or any other magic adverb) achieved".


I'll be the first to admit you're so much smarter than I am that I seldom understand what you're trying to say.

All I was trying to answer was this practical question: how can one pass a String and have the method cause the parameter to refer to a different String. One answer is to use an object with a mutable string property. Nothing tricky, but I know beginners get stuck on this point. (Another answer is to leave the parameters alone and work with the return value, but that's not always an option.)
 
vijaya bharath
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey

u told that object is not pass by reference, but when we swap the variables in the passed object it reflects in passing object also
how can its happening?

and one more thing
when u say like this String s = "abc";
s1 = s;
s=null;
o/p s=null,s1="abc"
how can this happening
total confusing
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vijaya bharath:
hey

u told that object is not pass by reference, but when we swap the variables in the passed object it reflects in passing object also
how can its happening?


That is the usefulness of being able to send an object reference to a method. The formal parameter becomes an alias of the actual parameter, and we can make changes to the original object using the formal parameter just like we could by using the actual parameter, as long as we don't point the formal parameter to another object.

Originally posted by vijaya bharath:
and one more thing
when u say like this String s = "abc";
s1 = s;
s=null;
o/p s=null,s1="abc"
how can this happening
total confusing


Here you are just rearranging references. You create the object "abc" and refer to it with s. Then you make s1 an alias of s, so s1 now points to the object "abc". Then you set s to null.
 
vijaya bharath
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx for u r reply

you told that as long we point to new object both s,s1 are same

when we say s = null

it means its creating one more object with null and pointing s to that?
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!