wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes PBValue or PBRef Comprehensive Detail Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "PBValue or PBRef Comprehensive Detail" Watch "PBValue or PBRef Comprehensive Detail" New topic
Author

PBValue or PBRef Comprehensive Detail

Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
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) {
this.name = name;
}
public void setName(String name) {
this.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

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

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

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

static void changeState(Dog d) {
d.setName(�Tuxido�);
}
}

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.


Regards,
[ June 10, 2007: Message edited by: Chandra Bhatt ]

cmbhatt
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Or, to put it a little more lightly, see our Campfire story


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Gitesh Ramchandani
Ranch Hand

Joined: Feb 28, 2007
Posts: 274
nice explanation.
 
Consider Paul's rocket mass heater.
 
subject: PBValue or PBRef Comprehensive Detail
 
Similar Threads
how are the objects passed by value or by reference
Incrementor operator
Anyhting like super.super?
objects passed by reference
Question Mock