Win a copy of Spark in Action this week in the Open Source Projects 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Pass by value explanation

 
Greenhorn
Posts: 27
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is pass by value explanation from Boyarsky and Selikoff OCA book:

"To review, Java uses pass-by-value to get data into a method. Assigning a new primitive or reference to a parameter doesn’t change the caller. Calling methods on a reference to an object does affect the caller".

It seems to me that the statement "Calling methods on a reference to an object does affect the caller" is not clear enough. For example let's try this code based on the example from the same book:



The output is Webby despite we "called a method on a reference to an object". Of course, String is immutable, but anyway our action match the rule provided by the book, but has a different result. And even in this case the result will be the same:



And at this case as well:



So, I think the rule should be modified. Am I wrong?
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Vitaliy Ovcharenko wrote:So, I think the rule should be modified. Am I wrong?


Yes, you are wrong! The rule mentioned in the book is spot-on but you must use of course an appropriate example. As you know the String class is immutable, so using a String object as an example is not the right thing to do because it's immutable (you can never change the value of a String object, regardless if the object is passed to a method). Illustrated in this code snippetSo although you called a method on a reference, it doesn't affect the object because String is immutable.

Now let's see if we can explain the pass-by-value "rule" with some good (if not excellent) examples.

Let's start with the first part: "Assigning a new primitive or reference to a parameter doesn’t change the caller."Now let's focus on the second part of the pass-by-value "rule": "Calling methods on a reference to an object does affect the caller"So you clearly see from the output that the array list to which reference variable list refers to was affected by calling the add method in the addElement method.

Hope it helps!
Kind regards,
Roel
 
Vitaliy Ovcharenko
Greenhorn
Posts: 27
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Roel De Nijs wrote:
As you know the String class is immutable, so using a String object as an example is not the right thing


Yes, I know that (as I mentioned in my previous post) and that's why I think that the rule should be modified. Because as you said

Roel De Nijs wrote:
although you called a method on a reference, it doesn't affect the object because String is immutable.


And I don't agree that to use the rule

Roel De Nijs wrote:
you must use of course an appropriate example


because a rule is something applicable for any case. And if the rule has an exception, the exception must be mentioned in it. And that's the main point of my post. As you see I said "should be modified" but not refuted.
So to avoid any misunderstanding and to be absolutely right (as the rule must be) it should consider any possible case. In our case something like this: "Calling methods on a reference to an object does affect the caller except the reference pointing on String object due to its immutability". And by the way is there any other exceptions that could be mentioned in the rule?
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Vitaliy Ovcharenko wrote:So to avoid any misunderstanding and to be absolutely right (as the rule must be) it should consider any possible case. In our case something like this: "Calling methods on a reference to an object does affect the caller except the reference pointing on String object due to its immutability". And by the way is there any other exceptions that could be mentioned in the rule?


You only mention String objects, but what with other immutable objects like primitive wrapper class objects? Or what about your custom created immutable class? And what if I simply invoked a read-only method? Because it's read-only, it will also not affect the caller. Will you incorporate that as well in the "improved" version of this "rule"?

In my opinion this rule is simple, concise, and completely spot-on! No need to change anything, because it's obvious that the caller will only be affected if you don't have an immutable object and you are not invoking a read-only method.
 
Vitaliy Ovcharenko
Greenhorn
Posts: 27
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Roel De Nijs wrote:

Vitaliy Ovcharenko wrote:So to avoid any misunderstanding and to be absolutely right (as the rule must be) it should consider any possible case. In our case something like this: "Calling methods on a reference to an object does affect the caller except the reference pointing on String object due to its immutability". And by the way is there any other exceptions that could be mentioned in the rule?


You only mention String objects, but what with other immutable objects like primitive wrapper class objects? Or what about your custom created immutable class? And what if I simply invoked a read-only method? Because it's read-only, it will also not affect the caller. Will you incorporate that as well in the "improved" version of this "rule"?


Yes, you have got exactly what I meant saying: "And by the way is there any other exceptions that could be mentioned in the rule?" And in my opinion, the exam guide must be precise in this case.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Vitaliy Ovcharenko wrote:Yes, you have got exactly what I meant saying: "And by the way is there any other exceptions that could be mentioned in the rule?" And in my opinion, the exam guide must be precise in this case.


There's simply no way the exam guide can be precise! There are waaaaaaaaaaay too many possibilities. And if all these possibilities must be handled in this rule, you'll probably have a rule of more than 50 lines. And readers will complain it's impossible to memorize those 50 lines. Now you have a very concise and easy-to-remember rule. And if you use a bit of common sense and think logical (quite an important requirement for a developer) you know that the rule only makes sense if you don't pass immutable objects to the method and the called method is effectively making a change to the object. There's no need to add this to the rule.
 
Vitaliy Ovcharenko
Greenhorn
Posts: 27
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be honest I was confused by the explanation. From one hand the author uses immutable class String to demonstrate passing by value:



which is not quite clear because of immutability of String class that could be the main reason of the resulting output. But from another hand the same immutable  type could not be used for demonstrating that "Calling methods on a reference to an object does affect the caller". So, the thing which is obvious for you as for experienced person could be confused for the beginners. And that's why it needs to be explained as clear as possible especially since the subject is quite complicated.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Vitaliy Ovcharenko wrote:From one hand the author uses immutable class String to demonstrate passing by value:


There's nothing wrong with that example provided in the exam guide. And just for the record: it only demonstrates the first part of the rule ("Assigning a new primitive or reference to a parameter doesn’t change the caller"). And the immutability of String objects doesn't play any role in this example. Even if String objects would have been mutable, the outcome of that code snippet would still be the same.
 
Vitaliy Ovcharenko
Greenhorn
Posts: 27
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Roel De Nijs wrote:

Vitaliy Ovcharenko wrote:From one hand the author uses immutable class String to demonstrate passing by value:


And the immutability of String objects doesn't play any role in this example. Even if String objects would have been mutable, the outcome of that code snippet would still be the same.


You are right again, but I don't want to call into question the correctess of the examples. I am just talking about the best way to understand the subject and to avoid any missunderstanding for the readers. Anyway thank you for your comments.
 
Hang a left on main. Then read this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic