aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A doubt in passing references Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "A doubt in passing references" Watch "A doubt in passing references" New topic
Author

A doubt in passing references

siva prabhu
Greenhorn

Joined: Sep 27, 2005
Posts: 4
Dear friends,
i am new to this site.please go through the following code and clarify my doubt.


class Astr
{

String name="Java";
int i=120;

public static void main(String args[])
{

Astr obj=new Astr();

pass(obj);

System.out.println(obj.name);
System.out.println(obj.i);
}

static void pass(Astr x)
{
x.name="Ranch";
x.i=220;
}

}

The o/p is: Ranch
220

Here i have passed the reference 'obj' to pass method.Now the reference variable 'x' and 'obj' point to the same object in the memory.Using the reference 'x', I have modified the value of the variables 'name' and 'i'.
There is no doubt in the value of 'i' being changed in the memory.but how could the value of 'name' change, as it is a String variable.we know that String objects are immutable, so when 'x' reference tries to change the value of the String,will that not end up in creation of new String and refrence 'x' pointing it as in the following program?



class Astr1
{

String s="david";

public static void main(String args[])
{

Astr1 obj=new Astr1();

pass(obj.s);

System.out.println(obj.s);
}

static void pass(String s)
{
s="victoria";
System.out.println(s);
}

}

The o/p here is: Ranch
Java



please clarify my doubt.
regards,
Siva
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Hi,
I think there is some difference in both the programs.
In the first program when you do this:

static void pass(Astr x)
{
x.name="Ranch";
x.i=220;
}
you are actually changing the reference of the object x.name. ie. now the variable 'name' of the object 'x' is refering to the newly created string "Ranch", hence you are getting the output "Ranch".

While in the second code:

static void pass(String s)
{
s="victoria";
System.out.println(s);
}

You are actually changing the reference of the local variable 's' and there is no change in variable 'obj.s' as it still refers to the same string containing "david". hence you are getting output as
victoria
david

P.S. The output of the second code should be "david" and not "Ranch" as you have assigned : String s="david";

Hope this helps.
[ September 27, 2005: Message edited by: Sandeep Chhabra ]

Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
agrah upadhyay
Ranch Hand

Joined: Sep 01, 2005
Posts: 579


Here i have passed the reference 'obj' to pass method.Now the reference variable 'x' and 'obj' point to the same object in the memory.Using the reference 'x', I have modified the value of the variables 'name' and 'i'.
There is no doubt in the value of 'i' being changed in the memory.but how could the value of 'name' change, as it is a String variable.we know that String objects are immutable, so when 'x' reference tries to change the value of the String,will that not end up in creation of new String and refrence 'x' pointing it as in the following program?

In above example,name is an instance variable of class Astr.When the class is instantiated ,it gets a default value of "Java"i.e. name is referring to a String object which contains "Java" as literal.Now when static method pass() is called,name is reinitialised to such an String object which contains String literal as "Ranch".Mind you that original String is not modified at all but it is now pointing to an entirely different String.
Check answer of your second problem.it should be Victoria David.
[ September 27, 2005: Message edited by: Barry Gaunt ]
siva prabhu
Greenhorn

Joined: Sep 27, 2005
Posts: 4
hi Sandy and Agrah,
thanks for the quick replies which made the concept clear for me.
regards,
Raja
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
And I still wonder at your second programme and the output you have posted there.... they are far way apart.


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: A doubt in passing references