This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Wrapper class  RSS feed

 
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • 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
 
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!