Reputation: 3015
I have recently switched from Netbeans to Eclipse, and Eclipse is finding syntax errors in my Project lot's of places that Netbeans doesn't and I cannot figure out why. As far as can tell both IDE's are set to use java 1.6. An example of this problem would be as follows (which is actually horrible code but I am working with legacy stuff):
Map map;
map = new Hashtable();
... add some stuff to map
int number = 5;
int status = 7;
assertTrue(number == map.get(status));
The above comes back with "Incompatable operand types int and Object" whereas Netbeans does not complain at all. I do not actually following why this doesn't work (does the int object not get autoboxed to an Integer?) as it works at run time from Netbeans. I am presuming there is a configuration setting somewhere in eclipse?
Upvotes: 0
Views: 589
Reputation: 5813
Change declaration to
Map<Integer,Integer> map;
map = new Hashtable<Integer,Integer>();
and this will solve you problem.
Alternatively, you can change this line
assertTrue(Integer.valueOf(number) == map.get(status));
Comparing Integer with == is not good practice. It is working just occasionally. You really should use equals() instead.
I don't why autoboxing in your case doesn't occurs automatically, maybe somebody that know spec better could provide answer.
P.S. Even better change this to
assertEquals(number, map.get(status));
and this will work as expected.
After clarifications that it is legacy code, my advice is the following. Change your code to:
Map map;
map = new Hashtable();
... add some stuff to map
Integer number = Integer.valueOf(5);
Integer status = Integer.valueOf(7);
assertEquals(number, map.get(status));
(I would even define temporary variable of type Integer where I put result of map.get(status)
, but it is question of the style whether to do this; This will help compiler though). Here, no new features is used.
Why you don't have problem with the Netbeans? My guess is because your version of JRE (or vendor) or your project settings.
Upvotes: 0
Reputation: 5813
It looks like autoboxing is disabled. Check that Window->Preferences->Java->Compiler->Errors/Warnings Boxing and unboxing conversion is not set to Error. Also check that Window->Preferences->Java->Installed JRE use JDK\JRE that is at least 1.5.
Upvotes: 3
Reputation: 9535
You can setup compiler-warnings under Window->Preferences->Java->Compiler->Errors/Warnings.
Make also sure you're compiling against the correct Java Version (check if your 1.6 Java is in the build-path and check the JDK Compilance level, see Preferences->Java->Compiler)
Upvotes: 1