aspose file tools*
The moose likes Beginning Java and the fly likes Need clarification Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need clarification" Watch "Need clarification" New topic
Author

Need clarification

Alvin York
Greenhorn

Joined: Jan 07, 2002
Posts: 7
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

Joined: Nov 29, 2001
Posts: 121
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 ]

Stefan Zoerner
Author of German LDAP-Book
Committer at Apache Directory Project
Alvin York
Greenhorn

Joined: Jan 07, 2002
Posts: 7
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

Joined: Sep 29, 2000
Posts: 8521
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.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Michael Fitzmaurice
Ranch Hand

Joined: Aug 22, 2001
Posts: 168
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


"One good thing about music - when it hits, you feel no pain" <P>Bob Marley
Alvin York
Greenhorn

Joined: Jan 07, 2002
Posts: 7
Thanks Michael,
That's exactly the answer I was looking for...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need clarification
 
Similar Threads
Integer class again
Question of Arrays.asList()
== comparison
problem about constructing Integer objects
Wrapper Classes for Arrays