Wrapper Classes
posted 23 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
{
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.
posted 23 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
"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
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
posted 23 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 ]
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 ]
posted 23 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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.
Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"
posted 23 years ago
Blake, there is no need to correct you...
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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...

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
| He was expelled for perverse baking experiments. This tiny ad is a model student: Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |










