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 ]
Author of German LDAP-Book
Committer at Apache Directory Project
Joined: Jan 07, 2002
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?
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
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
Joined: Jan 07, 2002
Thanks Michael, That's exactly the answer I was looking for...