• Post Reply Bookmark Topic Watch Topic
  • New Topic

Pass by Value or Reference...  RSS feed

 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I was just wondering whether Java pass parameters by value or by reference??

If it is by reference ( as most of the people say) then how is it passing the
primitive values??

Im really confused here....

Any thoughts??

Vijay
 
Vijay Vaddem
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After goin through some online resources......what im understanding is

Java passes Object references by its value.....which means "Pass by value"
there is no such "Pass by reference" in Java.....

Correct me if im wrong....

Vijay :roll:
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, objects are passed by reference.
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll clarify:

1. All primitive types are passed by value. That means boolean, byte, short, char, int, long, float, double.

2. All objects (including arrays like 'int[]', and Strings) are passed by reference.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In java there is no pass by reference but only pass by value. so what happens is when u pass a argument to method it makes a copy of that which is then passed on.
(Now confusing bit)
when u pass a object you actually pass a reference which is pointing to that object. so similar to primitive thing java makes a copy of that reference and sends across to method. now actually speaking its a new copy of ref but since this ref is also pointing to the same object u get the object ref there. which u can think of as pass by ref but IT'S NOT.
So if u assign a new object to that ref u lose the original object.
i hope it helps clear ur doubt.
 
Vijay Vaddem
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Pinky's comments....

Thanks, Pinky. Its pretty clear for me now...

Vijay
 
Vijay Vaddem
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following link gives a good explanation for the above...


http://java.sun.com/docs/books/tutorial/java/javaOO/arguments.html
 
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, technically objects aren't passed at all - what gets passed is the reference variable. And the reference is passed by value. Therefore *it looks like* objects get passed by reference...
 
Vijay Vaddem
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

When you say "reference variable", does this mean the memory address of the reference ??
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vijay Vaddem:
When you say "reference variable", does this mean the memory address of the reference ??


As i understand it, when you say

TWO things get created in memory. the actual OBJECT gets created on the heap. all the instance variables are placed there. then, on the stack, you create the myFoo reference variable. what gets places in this variable is some means of getting to the object on the stack.

it's PROBABLY a pointer, but you don't know, nor do you care. it could be a magic spell, a little gremlin... but it doesn't matter. you have no control over it, nor can you do anything with it.

when you then pass myFoo into a method, you make a copy of whats in myFoo. so you now have two things in the stack referring to 1 thing on the heap. so it would not be the memory address of the reference, but the value stored IN the reference.
 
Fletcher Estes
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it is pointless to confuse the issue by saying "everything" is passed by value, as it ignores the key abstraction at play. Obviously, a reference is itself a value :roll:

Simple rule: objects passed by reference, primitives by value
 
Sheriff
Posts: 11495
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Fletcher Estes:
I think it is pointless to confuse the issue by saying "everything" is passed by value, as it ignores the key abstraction at play. Obviously, a reference is itself a value :roll:

Simple rule: objects passed by reference, primitives by value



:roll: IMO, the "simple" rule that you gave is what confuses the issue. Yes, it can be confusing but it is best to understand the true concept rather than adhere to one that is misguided. Objects are not passed by reference. All parameters in Java are passed by value. No ifs, ands, or buts about it.

Pass by reference means that whatever happens to the formal parameter also happens to the actual parameter. That is not how it works in Java. As someone pointed out earlier, if you have an object reference parameter, the object that is being referenced can be modified from within the called method, as long as the reference is not changed. If you make the reference variable refer to another object, the actual parameter remains unchanged.



If Java used pass by reference, then actual should reference the new StringBuffer created by method foo. This is not the case, as you will see if you run the program.
 
author
Ranch Hand
Posts: 799
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may also want to read the Campfire story "Pass By Value, Please"

-J-
 
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And they didn't put pointers in Java because they complicated things. :roll:
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pinky is spot on. No further arguments required.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gregg Bolinger:
And they didn't put pointers in Java because they complicated things. :roll:


yup, except the nullpointer of course
And we'll get C++ templates and other beloved code obfuscation techniques soon when 1.5, 5.0, or whatever it's called these days hits the streets
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeroen Wenting:
And we'll get C++ templates


Tiger's generics *are not* template based.


beloved code obfuscation techniques


I tend to disagree, but that's a topic for a different thread...

when 1.5, 5.0, or whatever it's called these days hits the streets


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!