Hi Parvathi,
Java works always using pass by value semantics. An object is a phyical location in memory and is represented thru a reference variable.
Class X{
public static void main(String []args){
X referenceVar = new X(); //Here referenceVar is a reference variable.
int primitiveVar = 10;
}
public static void someMethod( <args>
{
//please ignore the syntax.
}
}
When u pass a primitive type as an argument to a method call i.e., in the above scenario
if you pass "primitiveVar" to a method call it passes 10 to the method.
in above case a method call will be like:
someMethod(primitiveVar);
if you pass "referenceVar" to a method call it also passes the value in it i.e., the address/reference of the physical memory.
In the above case a method call will be like:
someMethod(referenceVar);
if you see the second scenario we are passing the address of an object so, if you manipulate/change the values of an object using the argument it affects the original object.
take another case of the method definition:
someMethod(X ref){
X newRef = new X();
ref = newRef;
}
In this case any modifications done thru ref after the last insturction will not change the original object as the value in ref (address) is changed.
Any confusions?