Response to a good question by Mr.Brophy
by Mike
In the post below my friend Mark posted this comment/question:
# Mark B. Says:
March 27th, 2007 at 7:22 pm eThat article was good.
But is Java always pass by value? Am I retarted? Has it really been over a year since I coded in Java? So many questionsÂ…
Now the answer to that question “But is Java always pass by value?” is more complicated than it seems. The first part of the answer is easy, in Java primitives are passed by value. Example
public void primTest(int x){ x = 11; } int y = 1; primTest(y); System.out.println(y);
this does not print 11, it will print 1, because y was passed by value(thanks for the correction here Brian) as are all primitives in java.
Now here’s where the confusion comes, Objects appear to be passed by reference in Java but they are not, Objects are not passed at all. Let me explain. In Java we pass references to objects, basically a reference to an Object Instance. All variables in Java are either primitives or object references so its impossible to pass an object by reference.
Now thats kind of confusing because it still doesn’t answer the question. So the real answer here is that we pass everything by value but seeing that an Object reference is just a reference(The memory address of the Object) to an object when you pass an object reference by value to a method.
//note: I got this example(or at least the string buffer idea from an //example of this on another web site, I honestly cant remember the //url so thats why theres no reference here) StringBuffer sb = new StringBuffer("bleh"); passObject(sb); public void passObject(Object o){ o.append("bleh"); }
So the Object you modify in passobject is actually your String Buffer object and if you print your string buffer, you get “BlehBleh”.
(Finally after 2 years this is clear)
Anyway, I hope this answers your question Mark.
If it is unclear, I apologize, its not like me to get technical like this
-
Mike
Comments
Crazy… I always thought it would edit the original a la C . Oops!
Thanks dude.
np! where were you today, still sick?
Wasn’t sick… just got up a little late and worked a bit at home before a meeting. I went out to lunch yesterday, so that’s why I wasn’t around. Werd.
I think you meant “because y was passed by value as are all primitives in java.”
Haha, Sure did! Thanks Brian! woops! thats sure is a goof! At least you got the idea, especially since I say that we pass everything by Value! Also someone who isn’t a friend of mine reading my blog, who would have thought!
Yeah… well your very smart professor Simon led me here. Although he denies it, I was the one who got him started with C#.
Hah, well that explains it then! I guess its a good thing you got him started because if you hadn’t I probably wouldn’t have had the motivation to start either
If you like C#, check out the C# 3.0 features, especially LINQ. Cool stuff.
Yeah, Ive been actually checking out the LINQ stuff, Simon put me onto that after I was telling him how much I was liking lambda functions in Python. The job I’m (hopefully) starting in the fall Is all .net so I’m kind of boning up on all that stuff so I am ready to impress come the fall.
[...] passing by reference and passing by value, and we talked about Java just a little bit, and it seems I answered this question differently today then I did over 2 years ago. This is kind of funny, because I said today that you pass primitives by value and Objects by [...]