mavix
mavix

Reputation: 2572

How is it possible that a project compiles in eclipse, but javac throws compiler errors?

Working on a group project for school, and the following line throws an error when I run javac in the command line.

Object result = engine.eval(equation); //evaluate the arithmetic expression
cellValue = (double) result; // <-- This throws a compiler error (obviously)

But for some reason, this compiles and works(!) in eclipse, which my groupmates are using. I tried it for myself to confirm because I couldn't believe it.

ScriptEngine engine = manager.getEngineByName("JavaScript");

engine is a ScriptEngine, if that's relevant at all. I can't for the life of me figure out how eclipse's compiler is allowing a line to be compiled that casts an Object directly to a double.

Upvotes: 3

Views: 405

Answers (3)

meriton
meriton

Reputation: 70564

It appears the permitted conversions have been extended between Java 5 and 7. In the Java Language Specification, 3rd edition (for Java 5 and 6):

A value of a reference type can be cast to a primitive type by unboxing conversion (§5.1.8).

Unboxing conversion converts values of reference type to corresponding values of primitive type. Specifically, the following 8 conversion are called the unboxing conversions:

  • From type Boolean to type boolean
  • From type Byte to type byte
  • From type Character to type char
  • From type Short to type short
  • From type Integer to type int
  • From type Long to type long
  • From type Float to type float
  • From type Double to type double

So in Java 5 and 6, casting Object to double is not legal.

The Java Language Specification, Java SE 7 Edition writes:

The following tables enumerate which conversions are used in certain casting conversions. Each conversion is signified by a symbol:

⇓ signifies narrowing reference conversion (§5.1.6)

⊔ signifies unboxing conversion (§5.1.8)

and the following table says a cast from Object to double to be

⇓,⊔

i.e. a cast from Object to double is a cast to Double followed by an unboxing conversion to double.

It is therefore very likely that your team mates are compiling for Java 7, while you are compiling for Java 6.

Upvotes: 5

Cyrille Ka
Cyrille Ka

Reputation: 15533

Probably your friends are using another version of the Java language. The cast of Object to double (the primitive type) seems to be legal in Java 7, but not in Java 6. You can have your friends change their project settings in Eclipse or update your compiler to the version 7.

Note that casting Object to Double (the class) works in both versions.

Upvotes: 6

assylias
assylias

Reputation: 328659

Allowed casting conversions are detailed in the JLS #5.5. In particular, this conversion is allowed:

  • a narrowing reference conversion (§5.1.6) optionally followed by either an unboxing conversion (§5.1.8) or an unchecked conversion (§5.1.9)

In your case, if cellValue is a double, the cast from Object is allowed and will try to cast Object to Double then to unbox the Double to double.

Upvotes: 2

Related Questions