File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Pass by reference 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 "Pass by reference" Watch "Pass by reference" New topic
Author

Pass by reference

Atul Sawant
Ranch Hand

Joined: Jul 06, 2006
Posts: 304
Hi,

Please read the following question...

What will the following program print?


public class TestClass
{
static String str = "Hello World";
public static void changeIt(String s)
{
s = "Good bye world";
}
public static void main(String[] args)
{
changeIt(str);
System.out.println(str);
}
}

Select 1 correct option.
a "Hello World"

b "Good bye world"


c It will not compile.


d It will throw an exception at runtime.


e None of the above.

The answer is "a". I thought it would be "b" since String is an object and it is passed by reference in method changeIt and when it changes its value the original object's value is also changed.

Kindly let me know your inputs on this.

Thanks!


Mission SCWCD. Mission SCJP Complete: SCJP 1.4 - 91%
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
All parameter passing in Java is by value. When the String reference is used as a parameter to the method, a copy of the reference is sent to the method. So changing where the formal parameter in the method points has no effect on where the original reference points.
Paul Casal
Greenhorn

Joined: Dec 05, 2006
Posts: 2
Hi there,

Java is not really using pass by reference, it is using pass a copy of the reference. Thus, what the called code was really doing is changing where the copy of that reference is pointing, leaving the original reference (held by the caller) intact.


Paul Casal<br />Sr Developer<br /><a href="http://www.jbilling.com" target="_blank" rel="nofollow">jBilling</a> - The Open Source Enterprise Billing System
ashni Prakash
Ranch Hand

Joined: Dec 05, 2006
Posts: 50
You are passing class level static variable(str=hello world)to the changeIt method as a parameter in main method.The passed parameter value(hello world) is overwriting the declared value of changeIt methods variable s thats the output is option A .Even i got option A as output when i ran your program.To get the output you are expecting the code should be as below:

public class TestClass
{
public static String str = "Hello World";
public static void changeIt()
{

String str1="God bless you:";
System.out.println(str1);

}
public static void main(String[] args)
{


changeIt();
System.out.println(str);
}
}

the output for above code is:

God bless you
hello world

[ December 05, 2006: Message edited by: ashu prakash ]
[ December 06, 2006: Message edited by: ashu prakash ]
hardikjava shah
Greenhorn

Joined: Dec 12, 2005
Posts: 13
The only reason

"Sting object is immutable"

After changing, you have to reassign it to reference to see the change.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pass by reference