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 Wrapper class 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 "Wrapper class" Watch "Wrapper class" New topic
Author

Wrapper class

Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
Hi,
i expected the output to be 20.0 10.0
public class Switch{
public static void main(String args[]){
Double d=new Double(10);
Double d2=new Double(20);
new Switch().swap(d,d2);
System.out.println(d+""+d2);
}
public void swap(Double d,Double d2){
Double temp;
temp=d;
d=d2;
d2=temp;
}
}
Since d and d2 are objects i thought their values will get interchanged.
But i am getting output 10.0 20.0

nss
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
both references d and d2 are passed by value which means that you cannot change the Double object that they are referencing inside a method. You can only mutate the referenced objects by invoking method on them.
This may be worth reading: http://www.javaranch.com/campfire/StoryPassBy.jsp
http://www.javaranch.com/ubb/Forum24/HTML/013290.html

http://www.javaranch.com/ubb/Forum24/HTML/013307.html
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
[This message has been edited by Valentin Crettaz (edited December 06, 2001).]


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Hussain
Ranch Hand

Joined: Nov 22, 2001
Posts: 110
hi neha,
I think u don't Know tht String and Wrapper classes R immutable
plz read the RHE page no 20 for tht
the program at the top is something like this :-
import java.awt.*;
public class G
{
public static void main(String [] args)
{
Button b=new Button("pink");
replacer(b);
System.out.println(b.getLabel());//return "pink"
}
static void replacer(Button b)
{
b=new Button("Hello");
}
}
i think the think to understand is tht when u pass refrence to a method the method variable points to tht refrence(simple)but if u create object on tht u simply change the refreance and at the end of the method its being garbage collected .
check the bottom program of RHE page no 20
it states tht:-
public class G
{
public static void main(String [] args)
{
Button b=new Button("pink");
replacer(b);
System.out.println(b.getLabel());//return "Hello"
}
static void replacer(Button b)
{
b.setLabel("Hello");
}
}
I think by vertu of above program its simply a piece of cake for u now to Understand tht same behaviour in Wrapper classes....
i changed ur program:--
public class Switch
{
public static void main(String args[])
{
Double d=new Double(10);
Double d2=new Double(20);
new Switch().swap(d,d2);
System.out.println(d+""+d2);
}
public void swap(Double d,Double d2)
{
Double temp;
temp=d;
d=d2;
System.out.println(d.doubleValue());/*as it points d2 but its actually the local variable d points to d2 and the ref pass to d is now change to d2(the orignal variable d is still refrencing 10.0)*/
d2=temp;
System.out.println(d2.doubleValue());
}
}
i think i have done best for u to understand and wht u can do for me is pray for me for SCJP on 13 of this month..

------------------
Muhammad Hussain

Muhammad Hussain<br />Sun Certified Java Programer (SCJP2)<br />Sun Certified Web Component Devloper SCWCD
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
The parameters to method are really alias pointing to the arguments passed. So d and d2 wihtin the method are not the same as in the outside. They are just another variables. Thus you can even setting them to null and the outside variables will be indifferent.
Beware that if you call modifing methods on the alias that point to mutable objects, you will be able to change the objects in a way observable from the outside world.


SCJP2. Please Indent your code using UBB Code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Wrapper class