• 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 Classes

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Wrap
{
public void add(Integer o)
{
String str = o.toString();
int i = o.intValue() + 5;
str = Integer.toString(i);
o = Integer.valueOf(str);
System.out.println(o.intValue());
}
public static void main(String args[])
{
Wrap ob = new Wrap();
Integer a = new Integer(5);
ob.add(a);
System.out.println(a.intValue());
}
}
The output of the prog is 10 and 5 whereas it should be 10 and 10 as original object 'a' gets modified int the add method. Kindly explain.
 
author
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"kirty",
Thanks for participating here at the Ranch. However, the name you are using does not comply with our naming convention described at http://www.javaranch.com/name.jsp . Please log in with a new name, which meets these requirements.
You can change your name here.
Thanks.
Sean
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is true that the add function computes the value 10. However, the main function can't "see" this result because parameters to functions are call-by-value.
Try making the add function an Integer function instead of a void function. Then return the value you want.

[ September 25, 2002: Message edited by: Norm Miller ]
[ September 25, 2002: Message edited by: Norm Miller ]
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is how I see it, please anyone correct me if I'm wrong...
When you pass an Object to a method, you are passing a reference to the object. So, anything you do to the Object in your method add(), you are doing to the actual object that it's referencing. However, when you use Integer.valueOf(), you are reassigning the reference of your Integer object to a new Integer object. You're just reassigning what object you're referencing, so the orginal Integer object defined in main() remains the same.
Here's a couple of smaller examples that may help.

 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Blake Minghelli:
This is how I see it, please anyone correct me if I'm wrong...


Blake, there is no need to correct you...
reply
    Bookmark Topic Watch Topic
  • New Topic