• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help me with this simple code  RSS feed

 
shah rah
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
List list = new ArrayList();
list.add(0, 59);
//int total = ((Integer)(list.get(0))).intValue(); // this works
int total = list.get(0); //compile error
System.out.println(total);

I am using netbeans IDE and java 1.6. I thought it will not give compile error and autoboxing feature will handle it...

Why the error?


 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check your compiler settings. You might work with Java 6 but the compiler setting is set to 1.4.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're not declaring the list to be of any type, so the compiler has no way of knowing if the object in the list is castable. If you cast to Integer it'll compile.
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or if you use a List<Integer>.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!