Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

OCA Java 7 Question (Pass by reference / value)

 
Alexandru Gifei
Ranch Hand
Posts: 42
Java Linux Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I am currently studying for the OCA Java 7 Exam. In OCA Java SE 7 Programmer I Study Guide (Exam 1Z0-803) (Oracle Press), by Robert Liguori and Edward Finegan, at the "Understanding Methods and Variable Scope" chapter, I found the statement that Objects are passed to methods by reference and not by value. As far as i know, in Java, the objects are always passed by value. Can someone please explain this?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34672
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider calling this method. l changes which shows you that it is passed by reference.



If it was passed by value, you'd have a copy of l and it wouldn't change in the caller.
 
milan chovatiya
Greenhorn
Posts: 24
Java Oracle Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
objects are passed by reference...
 
Lucas Moraes
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hum, I have a problem with saying objects are passed by reference. Although in practice it is true, accordingly to Kathy Sierra and Bert Bates in the SCJP 6 Study Guide, everything is passed by value in Java, it's just that the "value" of a reference variable would be the reference to the object.
 
Sresh Rangi
Ranch Hand
Posts: 52
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Objects aren't passed at all, only references are. So is the reference passed by reference? Books say different things but recently there's more consensus that Java only supports "pass by value".

Compare with a language that supports "pass by reference" like C# (C# ref):

The ref keyword causes an argument to be passed by reference, not by value. The effect of passing by reference is that any change to the parameter in the method is reflected in the underlying argument variable in the calling method. The value of a reference parameter is always the same as the value of the underlying argument variable.

Do not confuse the concept of passing by reference with the concept of reference types. The two concepts are not the same. A method parameter can be modified by ref regardless of whether it is a value type or a reference type. There is no boxing of a value type when it is passed by reference.


Here changing a parameter doesn't mean mutating the object it refers to, but changing its own value by reassigning it. So something like this would be possible:



See also: Campfire Story
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is Java "pass-by-reference"? says -

I try to consistently use the terminology found at the Evaluation Strategy article. It should be noted that, even though the article points out the terms vary greatly by community, it stresses that the semantics for call-by-value and call-by-reference differ in a very crucial way. (Personally I prefer to use call-by-object-sharing these days over call-by-value[-of-the-reference], as this describes the semantics at a high-level and does not create a conflict with call-by-value, which is the underlying implementation.)



Regards,
Dan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic