Win a copy of Head First Android this week in the Android 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • Al Hobbs
  • salvin francis

PBValue or PBRef Comprehensive Detail

Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A little effort in understanding the concept of pass by reference and passing array:

All C and C++ programmers should keep their PBR and PBV concepts aside on the table and then jump to Java terminology of the same.

It is a big confusion among folks regarding whether the array is passed by value of passed by reference but if we need to understand the whold concept in just two lines I would write:

1-If the called method modifies the element of the array, it would affect the called method array.
2-If the called method dereference of say assigns it to refer to another array object, its effect would be local and there wont we any any change in the calling method array.

Finally I say, the object itself can be modified but if the called method tries to change the object the reference valriable points to, its affect would be local to the called method.

In one line to refresh, copy of reference is passed and object is never passed. And when copy of reference is passed, and you the called method changes the state of the object, it would reflect the calling method too because it points to the same object where the called method has done the modification.

One more confusion related to this is that folks get confused with immutable objects, like String, and all the Wrapper class objects. Once you assign an object to the reference variable of String of wrapper, because the object is immutable so it can�t be changed although you can reassign another object to the reference variable because the reference variable is not immutable.

What we talked happens just opposite in the Java constants, in that case when you have assigned an object to the reference variable, you can�t do reassignment althougn you can change the state of the object, but remember you can�t change the state of the immutable object because they can�t be modified.

Now because we are programmer nothing completes without codes: Let me give you some fantastic treats in the form of code snippets:

class Dog {

Dog(String name) { = name;
public void setName(String name) { = name;
public String getName(){
return name;
private String name;

public static void main(String [] args) {
Dog d = new Dog(�Fido�);
System.out.println(�My dog�s name is � + d.getName());//prints Fido

System.out.println(�My dog�s name is still � + d.getName()); //prints Fido

System.out.println(�My dog�s name changed to � + d.getName());
//prints Tuxido

static void changeRef(Dog d) {
Dog d1 = new Dog(�Sheroo�);

static void changeState(Dog d) {

Wasn;t that fabulous example to understand what is passed exactly.

Ok! We know that StringBuffer and StringBuilder are mutable, means you can modify the object without creating new object as happens in case of String. One more example with standard classes will help you to ice cool your mind

OK! Let�s play with arrays a bit

A bittest with Immutable objects :

The same theory applies with all immutable class object. Remember every time reference to the variable is passed never or nothing like passing Object itself; Change is only seen by the calling method when the called method changes the state of the object that is internal to the object as we saw in Dog example when we changed the name of the Dog.
This is only true when objects are mutable or Objects that can be modified never with immutalbe which are rigid in their behaviour.

And the bottom line �Don�t confused with the reference variable and objects�.
Reference: variables are always modifiable except in the case of final where a reference variable once assigned can�t be changed or say can�t refer to another object. Remember the point that changing the reference in the called method has its local effect and its not going to affect the calling method.

[ June 10, 2007: Message edited by: Chandra Bhatt ]
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or, to put it a little more lightly, see our Campfire story
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
nice explanation.
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic