• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method parameters passed by reference/value  RSS feed

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I went to a Java interview today, and got asked whether parameters in Java methods were passed by reference or value. I said reference, but the guy asking the question said value. He wasn't talking about primitives, but about classes. How would you have answered this question?
 
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everything is passed by value.

http://faq.javaranch.com/view?CallByReferenceVsCallByValue
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it is a primitive, you pass the value.
If it is a reference value, you pass a copy of the value of the reference.

So people say "pass-by-reference," even when it is really "value."
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed - everything is passed by value in Java. So primitives as well as references are passed by value.

Note the difference between "pass by reference" and "references are passed by value" - that confuses many people.
 
Steve Sinai
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm actually surprised at all the pass-by-value answers. I'm wondering if the definition of pass-by-value and pass-by-reference have changed over time. I first learned this over 20 years ago, but haven't had anyone ask me this in years.

My idea of pass-by-value is that you pass something into a void method, change it within the method, but when you come back out of the method, it keeps it's old value. You CANNOT make changes to a passsed-by-value object within a method.

Pass-by-reference is when you pass an object (not a primitive) into a method, change something in the object, and when you come back out of the method, it remains changed. You CAN make changes to a passed-by-reference object wthin a method.

My explanation is a bit simplified, but is this what other people think? Or is my way of thinking about this obsolete?
 
Steve Sinai
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just went back to my old "The Java Programming Language" Second Edition by Gosling and Arnold, and it says parameters are passed by value. I see what everyone's talking about, in that the actual reference to the object is copied and passed into the method. This seems like a very literal interpretation of the term "pass-by-value," but who am I to argue with the people who wrote the language.
 
Sheriff
Posts: 22845
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, well when you write a document that describes a computer language you should expect people to interpret it literally. My favourite example is this:In a pass-by-reference language, the output of this fragment is "null". In a pass-by-value language, the toString() value of the original object is output.

Notice that confusion arises in statements like this:
you pass something into a void method, change it within the method...
for at least two reasons: first, it's unclear whether it's a variable or an object reference that is being passed, and second it's unclear what constitutes changing it.
 
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steve Sinai:
My idea of pass-by-value is that you pass something into a void method, change it within the method, but when you come back out of the method, it keeps it's old value.


Yes. That is exactly what happens with object references.

You CANNOT make changes to a passsed-by-value object within a method.


In Java, you cannot pass an object as a method parameter at all. You can only pass references. And those are passed by value.

And no, passing an object reference by value is not exactly the same as passing an object by reference.

Pass-by-reference is when you pass an object (not a primitive) into a method


In the languages I know that support pass-by-reference, such as C or C++, you can also pass primitive values by reference.
 
Steve Sinai
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java, you cannot pass an object as a method parameter at all. You can only pass references. And those are passed by value.

And no, passing an object reference by value is not exactly the same as passing an object by reference.


I do understand what everyone's saying. Language can be pretty loose and fuzzy sometimes, and when I talk about passing an object as a method parameter, I actually do implicitly mean that a reference to the object gets passed. I'm too lazy to explicitly say "reference to an object" instead of just "object." (Maybe not a good thing in a field like computer science, where preciseness matters.) Kind of like the difference between a republic and a democracy. Even though lots of people know that the United States is technically a republic, they still refer to it as a democracy.
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steve Sinai:


I do understand what everyone's saying. Language can be pretty loose and fuzzy sometimes, and when I talk about passing an object as a method parameter, I actually do implicitly mean that a reference to the object gets passed. I'm too lazy to explicitly say "reference to an object" instead of just "object." (Maybe not a good thing in a field like computer science, where preciseness matters.) Kind of like the difference between a republic and a democracy. Even though lots of people know that the United States is technically a republic, they still refer to it as a democracy.


Unfortunately that "loose" use of very well defined formal terms results in your words communicating something quite different from what you intend. Pass-by-value and pass-by-reference are both very well defined. In both it is how the parameter is passed that is being referred to. In Java, that parameter is either a primitive, a reference or null (which can be thought of as a reference). In ALL cases that parameter is passed by value.

An object is never passed. Not by value and not by reference. What you can or cannot do to the object and who can and cannot see mutations in the object is irrelevent because the object is not the parameter. Furthermore, passing a reference by value and passing an object by reference are not equivalent. They're not equivalent technically nor semantically.
 
Steve Sinai
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I should just let this drop...but if I said I was passing an object as a parameter into a method, would people really be confused as to whether I was passing a whole object into the method, or would they just know that I was talking about a reference to an object?
 
Paul Clapham
Sheriff
Posts: 22845
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steve Sinai:
I should just let this drop...but if I said I was passing an object as a parameter into a method, would people really be confused as to whether I was passing a whole object into the method, or would they just know that I was talking about a reference to an object?
Ken and Ilja and I would know. Beginners would not know; many of the questions you'll see in these forums make it clear that beginners don't understand the difference between variables and objects, let alone that variables actually contain references to objects and not the objects themselves.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steve Sinai:
I should just let this drop...but if I said I was passing an object as a parameter into a method, would people really be confused as to whether I was passing a whole object into the method, or would they just know that I was talking about a reference to an object?


There are few contexts where blurring the distinction between an object and an object reference makes sense. What is the difference between an object and an object reference?

C is exactly the same - all types are passed by value, including pointers. You wouldn't say that passing an int* through which one can observe a change to the int being pointed to after invocation is "passing an int by reference".

The language has been around for over a decade and still people have trouble determining the difference between a frying pan and a frying pan handle. It's frustrabating at times.
Java passes everything by value
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steve Sinai:
I should just let this drop...but if I said I was passing an object as a parameter into a method, would people really be confused as to whether I was passing a whole object into the method, or would they just know that I was talking about a reference to an object?


I would know. But you seemed to suggest that it therefore would be ok to say that the object is passed-by-reference. Which is at least misleading, if not plain wrong.
 
Steve Sinai
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ken and Ilja and I would know. Beginners would not know; many of the questions you'll see in these forums make it clear that beginners don't understand the difference between variables and objects, let alone that variables actually contain references to objects and not the objects themselves.


This is actually a good point.


C is exactly the same - all types are passed by value, including pointers. You wouldn't say that passing an int* through which one can observe a change to the int being pointed to after invocation is "passing an int by reference".


When I used to do C/C++ programming (1983-1997), a very common interview question was "are arrays passed by value or reference?" The answer was reference. OK...the actual pointer to the start of the array was passed by value, but the point of the question was about knowing whether one could change the contents of an array within a method. This is why I took it for granted that Java objects were passed by reference.

The language has been around for over a decade and still people have trouble determining the difference between a frying pan and a frying pan handle. It's frustrating at times.


It's not that people don't know the difference. It's more like...if I was holding a frying pan by the handle, and you asked me what I was doing, I would say "I'm holding a frying pan." People would know what I'm talking about. Except for programmers - they would say "No! You're holding the handle to the frying pan!"
 
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
If it is a primitive, you pass the value.
If it is a reference value, you pass a copy of the value of the reference.

So people say "pass-by-reference," even when it is really "value."


Hate to be pedantic but...

If the variable holds a primitive, the method will receive a copy of that primitive held by a method local variable. If the variable holds a reference, the method will receive a copy of that reference held by a method local variable. No differences here.

I think if Java would have called their references, pointers, nobody would have been confused. But Java wanted to strongly show there was no pointer arithmetic so they just did away with the whole pointer term.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!