• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Wrapper class

 
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
You firghten me terribly. I would like to go home now. Here, take this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic