When your passing 1 to method, it will convert 1 to Integer using auto boxing,but it can not find method taking Integer argument,So it will give compile time error,If you change method parameter from Short to Integer then code will compile.
Originally posted by Adam Tkaczyk: I have found the solution. Short x=1 doesn't work in Sun compiler but works fine in IBM compiler which is in Eclipse IDE which I use.
Sun compiler can't make two implicit conversion, but the IBM one can in variable initialization.
This shouldn't happen. Are you sure you're using a 1.5 version of the Sun compiler? The Java Language Specification explicitly describes the mechanism that allows statements like "Short x = 1":
5.2. Assignment Conversion
Assignment conversion occurs when the value of an expression is assigned (�15.26) to a variable: the type of the expression must be converted to the type of the variable.
In addition, if the expression is a constant expression (�15.28) of type byte, short, char or int :
* A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable. * A narrowing primitive conversion followed by a boxing conversion may be used if the type of the variable is : --- Byte and the value of the constant expression is representable in the type byte. --- Short and the value of the constant expression is representable in the type short. --- Character and the value of the constant expression is representable in the type char.
Note the part I bolded above. [ November 30, 2007: Message edited by: Kelvin Lim ]
Joined: Nov 29, 2007
I thought so, but I discovered that the JDeveloper, which I used for test, has Oracle implementation of java compiler (that is implicit setup). In JDeveloper (10.1.3.3) and ocj compiler code Short x=1; does not compile. I don't know why. Today I know that is good to use Sun IDE for SCJP preparation. Otherwise it is posible to make even simple aspects of language hard to understand because of standard violations.
Thanks for help to everybody.
Kelvin Chenhao Lim
Joined: Oct 20, 2007
Perhaps you're using an older compiler that doesn't support Java 1.5 at all? Check whether it accepts other new Java 1.5 language features, like var-args and generics.
Otherwise, if other Java 1.5 constructs compile fine but the line you stated doesn't, then it's most likely a compiler bug. You probably should see if there's an update or patch that fixes this.