Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

Question about Java "Pass-by-Value" behavior

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

It is not clear for me, that if Java always passes parameter variables by value, how is that you do something with the passed parameter in a method and that changes the real object? As i understand you always pass a copy in the method...this shouldn't affect anything outside the method (on the heap). I already read the Cup Size explanation from here: https://javaranch.com/campfire/StoryPassBy.jsp. Still not getting it.
Are there some exceptions to this pass-by-value rule? Is this something to do with the object mutability/immutability?

Problematic examples from the study book Boyarsky and Selikoff Oracle Certified Associate Java SE8 Programmer I
Chapter 3 Review Question  6
Chapter 4 Review Question 17

P.S. I am completely new to programming, sorry if this is an obvious question!
Thanks in advance,
Andrea

 
Rancher
Posts: 3599
38
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Andrea,

You probably confused by this situation.

Assume this code:


and


After the code run the original StringBuilder created at line n1 will contain "abcxyz".

 
Saloon Keeper
Posts: 11923
253
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pass-by-value doesn't copy the object. It copies the reference to the object, meaning if you change the reference that the method parameter holds, it won't affect what object the variable in the calling method points to. You can still edit the object itself through the reference that was passed into the method call.

You are probably confused because the alternative to 'pass-by-value' is named 'pass-by-reference', but the word 'reference' in this context has nothing to do with Java references.

'Reference' is used in the C++ language to indicate an alias for a variable. When you pass a variable by reference, the method parameter is actually an alias for the variable in the calling method, and you can change its value from inside the method call.
 
Stephan van Hulst
Saloon Keeper
Posts: 11923
253
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to CodeRanch!
 
Andrea Sandor
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh,
so I am passing the copy of the reference. Any reference to that object can modify the object itself (in case of mutable objects).

I think slowly I am getting it:)

I thank both of you!
 
Mikalai Zaikin
Rancher
Posts: 3599
38
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And to summarize implication of the behavior described above are (three cases possible)

1) we declare a primitive and pass in



method 'doSomething' cannot affect outer value assigned to 'i', even if it tries to re-assign parameter inside (because passed in is copy of primitive)

2) we declare an immutable object pass in




method 'doSomething' cannot affect outer value assigned to 's', even if it tries to re-assign parameter inside (because passed in is copy of the reference), and also cannot modify content of the String object, as it's immutable by design

List of immutable objects in Java: String, Long, Char, Integer, Byte, etc.. you can also create own immutable object (no setters, value defined in constructor)

3) we declare a mutable, regular object and pass in



method 'doSomething' cannot affect outer value assigned to 'sb' (i.e. technically it will refer to the same object on the heap), even if it tries to re-assign parameter inside (because passed in is a copy of the reference), BUT code inside can modify content of the StringBuilder object, as inner code has reference to the object and can call "setters" (object is mutable by design)

I think these 3 cases describe all possible situations you can have.

 
Andrea Sandor
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much!
Now I understand. Good to have these three examples next to each other.
 
Marshal
Posts: 69035
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Andrea Sandor wrote:Thank you very much!

On everybody else's behalf, that's a pleasure/you're welcome.

Now I understand. . . .

Well done.

Maybe you will find the two links in this old post of mine useful. The JavaPapers link explains pass by value and quotes the Java® Language Specification (=JLS) which specifies newly created variables, which means pass by value. I have quoted the same JLS section myself, but in a different version.


and welcome to the Ranch (again)
 
Ranch Foreman
Posts: 103
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mikalai Zaikin wrote:I think these 3 cases describe all possible situations you can have.


You missed a very common and important 4th one: Arrays

When doing low-level I/O one often has this construct:

Arrays are a bit special in terms of what they are and how they're handled. Even arrays of primitives (boolean, byte, short, int, long, char, float, double) are in fact objects, mutable object. This means: the reference contains the address to some continues block of memory. It's this starting address that gets copied. As the read() method gets a "pointer" to this very block of memory it is able to manipulate its content. Hence the data read from the input end up in the array. Although this is still pass-by-value it's somewhat near how pass-by-reference in C works.

About the topic overall: Most of those asking this question often struggle with what's stored in a reference and what gets actually copied when a method is called.
 
Marshal
Posts: 15565
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bob Winter wrote:
You missed a very common and important 4th one: Arrays
...
Arrays are a bit special in terms of what they are and how they're handled. Even arrays of primitives (boolean, byte, short, int, long, char, float, double) are in fact objects, mutable object.


This isn't really different from the 3rd case. Arrays are always objects and variables that reference arrays are always reference variables, the same as any other reference variable. The fact that an array can hold primitives doesn't make it some kind of special reference type that behaves differently in terms of the pass-by-value semantics in question; it behaves just like any other mutable object that's passed by value to a method.
 
Mikalai Zaikin
Rancher
Posts: 3599
38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bob Winter wrote:

Mikalai Zaikin wrote:I think these 3 cases describe all possible situations you can have.


You missed a very common and important 4th one: Arrays



I believe arrays (in general -- no matter if they contain primitives or object references) fall under 3rd category -- passing to a method a reference of mutable object. (this was noted by Junilu)
 
They worship nothing. They say it's because nothing is worth fighting for. Like this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic