Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Integer and Integer object

 
venkatesh badrinathan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.util.*;
class test
{
public static void main ( String [] args )
{
List < Integer > list = new ArrayList < Integer > ();
for ( int i = 1 ; i<10 ; i++ )
{
list.add(i);
}
list.remove( new Integer(4) ); // 1
list.remove( 1 ); // 2
}
}
ans:Line one removes Integer object 4 and Line 2 removes Integer at index one
my question: actually what do they mean by 'Integer' and 'Integer object'..???
 
Ankit Garg
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all let me describe you this program if this would clear your confusion-

import java.util.*;
class test
{
public static void main ( String [] args )
{
List < Integer > list = new ArrayList < Integer > ();
for ( int i = 1 ; i<10 ; i++ )
{
list.add(i); //autoboxing
}
list.remove( new Integer(4) ); //removes the Integer object with the value 4
list.remove( 1 ); //removes the Integer Object at the index 1 (i.e. the second value)
}
}

Now everywhere Integer objects are being added and removed. There is no difference between Integer and Integer Object here. don't get confused between int and Integer. Do you get it...If no please describe your question a bit more.... :roll:
 
venkatesh badrinathan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks AnkitJi Garg, i have understood what you have told..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic