• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java using pass-by-value  RSS feed

 
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I just learned that java is a pass-by-value language. I understand the concept but I do not understand the 'inconsistency'. Let me explain with an example:



First things first the program prints out: 1 abc 2.

I understand that the method number only touches its method parameter (x) and therefore does not increment the instance variable originalNumber. However, the arraylist method parameter(x) adds another String ("Paul") which is also passed to arraylist. Following the example of the int, I would expect it NOT to update because it only touches its method parameter and not the actual arraylist. The fact that the String letters is not update is understandable since a String is immutable.

Questions:
1) originalNumber = number(originalNumber) fixes the problem as well as originalLetters = letters(originalLetters). Does this mean this is just the way to do it for primitives?
2) Why doesn't the arraylist follow the same rules as the int?

Cheers.
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bart Boersma wrote:
2) Why doesn't the arraylist follow the same rules as the int?


It is following the same rules. The arraylist reference did not change.

See these links for more details...

https://coderanch.com/wiki/660293/Call-Reference-Call
http://javaranch.com/campfire/StoryPassBy.jsp

Henry
 
Bart Boersma
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is clearing the sky!

So from what I understand, passing the arraylist into the method creates a new reference to the object (so now there are 2 references to the object: the original reference and the method parameter reference). Therefore updating the method parameter reference will affect the object.
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bart Boersma wrote:. . . Therefore updating the method parameter reference will affect the object.
No. You might have understood the concept, but that terminology is all wrong.
You have an original location with a reference to the object.
You pass the (reference to the) object to the method and the method has a second reference to it.
Changing the second reference cannot alter the first reference nor the object.
Adding an element to the List changes the state of the object. If you have a reference to a mutable object you can change its state. You haven't changed it to a different object, but you have told it to change something inside itself.
Remember you have two references to the object (during the lifetime of the method called), as you said. Both those references can be used to change the state of the object, rather like having two remote controls to the same television (as shown in the campfire story). You can however change the reference or the state of the object, but not both at the same time. You cannot swap the remote controls and push their buttons at the same time. One or the other but not both.

Changing the state is like changing channel or volume on a television. Changing the reference is like buying a new television.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!