Suresh - Java Primitives Vs Objects

Rule #1 : Everything in java is passed by Value .

Rule #2 : The values of variables can either be primitives or references. Objects are never passed

#======================MyObject.java=====================
package obj;

public class MyObject extends Object
{
public String firstName ;
public String secondName ;

public MyObject()
{
}

public void printNames()
{
System.out.println("First Name :" + this.firstName );
System.out.println("Second Name :" + this.secondName);
}

public void printFirstName()
{
System.out.println("First Name :" + this.firstName );
}


public void printSecondName()
{
System.out.println("Second Name :" + this.secondName);
}


}

#====================Sub.java===============================

package obj;

public class Sub
{
private String str = "";
public Sub(String str)
{
this.str = str;
}

public void printString()
{
System.out.println("string value is :" + str);
}

}

#=============TestReference.java=====================
package obj;

/
*
* @author suresh
* 21 Dec 2005
*
/

public class TestReference
{

Object x = "suresh";
MyObject myObj = new MyObject();


/

Java passes all the parameters to methods strictly by pass-by-value.

Ref
:http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#37472

Java does not pass objects in parameters. Java does not pass object
reference in parameters. In case of Objects in parameters, Java
rather passes a copy of reference value to methods (bit confusing but
thats the fact:!)

http://java.sun.com/docs/books/tutorial/java/javaOO/arguments.html says

Arguments are passed by value. When invoked, a method or a constructor
receives the value of the variable passed in. When the argument is of
primitive type, "pass by value" means that the method cannot change
its value. When the argument is of reference type, "pass by value"
means that example 1 : the method cannot change the object reference
example 2 : but can invoke the object's methods and modify the
accessible variables within the object. /


/
@param args
*/
public static void main(String[] args)
{

// TODO Auto-generated method stub
TestReference t = new TestReference();

//example 1:
t.x = "suresh";
t.ChangeString (t.x);
System.out.println ("Original object value :" + t.x);

//example 2: t.myObj.firstName = "ar"; t.myObj.secondName = ".rahman";
System.out.println("===========original obj value before
experiment=============");

System.out.println("original obj value 1:" + t.myObj.firstName +
t.myObj.secondName );

t.changeObjValue(t.myObj);
System.out.println("original obj value 2:"
+ t.myObj.firstName + t.myObj.secondName );

//example 3:
t.changeObjValueNew(t.myObj);

System.out.println("original obj value 3:" + t.myObj.firstName +
t.myObj.secondName ); }

//example 1:
void ChangeString (Object y)
{
System.out.println("===========example 1=============");
System.out.println("x value passed to method:" + y);
y = "Hello";
System.out.println("new value inside method :" + y);
}

//example 2:
//here y is a copy of the "value of reference" to MyObject
void changeObjValue(MyObject y)
{
System.out.println("===========example 2==============");
System.out.println("y value before change1:" + y.firstName + y.secondName);
MyObject temp = y;

temp.firstName = "ilai" ;
temp.secondName = "araja";
temp.printNames();

}
//example 3: a new object is created
void changeObjValueNew(MyObject y)
{
System.out.println("===========example 3================");
System.out.println("y value before change2:" + y.firstName + y.secondName);
y = new MyObject();

y.firstName = "music";
y.secondName = "lover";
y.printNames();

}

}

#=================output===========================
===========example 1=============
x value passed to method:suresh
new value inside method :Hello
Original object value :suresh
===========original obj value before experiment=============
original obj value 1:ar.rahman
===========example 2==============
y value before change1:ar.rahman
First Name :ilai
Second Name :araja
original obj value 2:ilaiaraja
===========example 3================
y value before change2:ilaiaraja
First Name :music
Second Name :lover
original obj value 3:ilaiaraja