Java - short and casting
Asked Answered
A

3

11

I have the following code snippet.

public static void main(String[] args) {
 short a = 4;
 short b = 5;
 short c = 5 + 4;
 short d = a;
 short e = a + b; // does not compile (expression treated as int)


 short z = 32767;
 short z_ = 32768; // does not compile (out of range)

 test(a);
 test(7); // does not compile (not applicable for arg int)
}

public static void test(short x) { }

Is the following summary correct (with regard to only the example above using short)?

  • direct initializations without casting is only possible using literals or single variables (as long as the value is in the range of the declared type)
  • if the rhs of an assignment deals with expressions using variables, casting is necessary

But why exactly do I need to cast the argument of the second method call taking into account the previous summary?

Andesine answered 27/4, 2010 at 11:34 Comment(1)
See also #478250Jester
S
19

These are the relevant JLS sections:

JLS 5.1.1 Identity Conversion

A conversion from a type to that same type is permitted for any type.

JLS 5.2 Assignment Conversion

Assignment conversion occurs when the value of an expression is assigned to a variable: the type of the expression must be converted to the type of the variable. Assignment contexts allow the use of one of the following:

  • Identity conversion
  • [...]

In addition, if the expression is a constant expression 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.

The above rules explain all of the following:

short a = 4;     // representable constant
short b = 5;     // representable constant
short c = 5 + 4; // representable constant
short d = a;     // identity conversion
short e = a + b; // DOES NOT COMPILE! Result of addition is int

short z  = 32767; // representable constant
short z_ = 32768; // DOES NOT COMPILE! Unrepresentable constant

As to why this doesn't compile:

test(7); // DOES NOT COMPILE! There's no test(int) method!

It's because the narrowing conversion with constant is only defined for assignments; not for method invocation, which has entirely different rules.

JLS 5.3. Method Invocation Conversion

Method invocation conversions specifically do not include the implicit narrowing of integer constants which is part of assignment conversion. The designers of the Java programming language felt that including these implicit narrowing conversions would add additional complexity to the overloaded method matching resolution process.

Instead of explaining how method resolution works precisely, I will just quote Effective Java 2nd Edition, Item 41: Use overloading judiciously:

The rules that determine which overloading is selected are extremely complex. They take up thirty-three pages in the language specification, and few programmers understand all of their subtleties.


See also

Stalk answered 27/4, 2010 at 11:47 Comment(1)
Thanks for all your answers! That helped me a lot!Andesine
R
1

The result of an arithmetic operation on short values is always int. test(7) doesn't work, since you haven't said that 7 is of type short. The compiler should be a bit smarter here.

Radloff answered 27/4, 2010 at 11:38 Comment(2)
so you are saying some arith operation is applied to the literal-argument 7 when the function is called?Andesine
No, there is no arithmetic in test(7). a+b is arithemetic with an int result and test(7) is a method invocation with a int literal as parameter.Radloff
A
1

The '7' in the call test(7); is an int and will not be automatically converted to a short.

It works when you declare and initialize short values, but that's a special case for the compiler. This special case doesn't exist for method calls.

Ailina answered 27/4, 2010 at 11:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.