• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Pass by value vs reference

 
Ranch Hand
Posts: 73
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

It is said that java is always pass be value.

1. Pass by value means the changes made in the called method does not reflect in the caller method? Right?

2. In the below example, when I modify an int primitive or String object in the called method it does not reflect in the calling method. But when I modify a list in the called method it reflects in the calling method. Why?

Output:
5
abc
[1, 2]



 
Marshal
Posts: 75836
361
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are not modifying an int or a String anywhere. Strings are immutable and primitives behave as if they were immutable. You cannot modify either. What you are doing is replacing the value the variable points to.Those changes are not reflected back to a calling method. Remember the addresses are simply guesses, and it isn't possible to find out what their real‑life counterparts are. What's more, what is passed is probably not an address, but a “handle”, i.e. an address where the real address of the object is to be found.I haven't changed the information passed. What is passed is a copy of the address information. What you have in line 8 is two references a second reference to the exact same object. There is no change in the reference to reflect back or not reflect back to the calling method.
What you are seeing is the normal behaviour of a mutable object. You have two references to the same object, in lines 1 and 38, and whatever happens to the object in one place is visible in the other place. Imagine you have a sheet of paper in front of a webcam connected to some program. As soon as you write something on that paper, that writing will become visible on screen. Because you have two references to the same object, whether you pass the reference by reference or by value (or at all) doesn't make any difference. The code with the List behaves the same as the following code:-
 
pie. 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
reply
    Bookmark Topic Watch Topic
  • New Topic