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