This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need clarification

 
Alvin York
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public static void Add(Integer b)
{
int I = b.intValue();
I += 3;
b = new Integer(I);
}
Can someone explain to me exactly what 'new Integer(I)' is doing?
Thanks in advance...
 
Stefan Zoerner
Author
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!
new Integer(I) creates a new object of the class java.lang.Integer. This is one of the so calles wrapper classes. They are used to handle primitive values (like those of type "int") as Objects. Instances of the class Integer are "immutable", that is there is no way to change its state (same with other wrapper classes and String). The only way to set the int-value, that the object is wrapping, is to do it via the constructor. That is exactly what "new Integer(I)" is doing. There is a constructor Integer(int i) in class java.lang.Integer (see API for details). "I" is of type int (remark: better use a name starting with lowercase) and is declared/calculated in your method.
Hope that helped. Greetings from Hamburg,
Stefan
[ January 11, 2002: Message edited by: Stefan Zoerner ]
 
Alvin York
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can 'new Integer(I)' be creating an object named I when I already exists as a primitive? Are you able to create different types with the same name in the same method?
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
b is a variable that holds a copy of a reference to an object that is an Integer.
b is passed into the method.
The int value of b is sucked out of it and placed in the int variable I. Then that is bumped up by 3.
The the reference in b is thrown away and a new Integer object is created using the larger int value, and a reference to it is placed in b.
 
Michael Fitzmaurice
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Alvin York:
How can 'new Integer(I)' be creating an object named I when I already exists as a primitive? Are you able to create different types with the same name in the same method?

The short answer is that there is only one variable called I. The line you refer to uses I to construct another object. This new object is then assigned to the existing variable b. And no, you cannot have 2 variables with the same name, irrespective of their types, if they are defined within the same scope.
Hope this helps
Michael
 
Alvin York
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Michael,
That's exactly the answer I was looking for...
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic