Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Autoboxing / unboxing

 
Lalitha Gottumukkula
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is compiled with jdk1.5.0_03


When compiled,

F:\practice\newjava>javac TestBoxing.java
Note: TestBoxing.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

F:\practice\newjava>javac -Xlint TestBoxing.java
TestBoxing.java:14: warning: [unchecked] unchecked call to add(int,E) as a member of the raw type java.util.ArrayList
l.add(0, new Integer(59));
^
TestBoxing.java:15: warning: [unchecked] unchecked call to add(int,E) as a member of the raw type java.util.ArrayList
l.add(1, num); // auto boxing
^
2 warnings

What are these warnings? What is an unchecked or unsafe operation?

Thanks,
Lalitha.
 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is auto boxing ???
 
Marcus Green
arch rival
Rancher
Posts: 2813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is where an object that corresponds to a primitive is automatically cast by the system. This means you can assign an Instance of the Integer wrapper to an int value.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From http://java.sun.com/docs : New Java Language Features in JDK 5.0
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In order to avoid the warning, you can:



Nick
 
Lalitha Gottumukkula
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it compulsory to use generics in 1.5? Because, if I am commenting the autoboxing statement , then even it is showing warning with the other statement.


This code is working properly without any warnings in versions less than 1.5

Thanks,
Lalitha.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is "compulsory" to use 1.5 source level features when you are compiling with the 1.5 source level (logical isn't it?). There's no good reason not to (the "I don't understand it" reasoning is a bit silly).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic