package typecastingpkg;
public class Main
{
public static void main(String[] args)
{
byte a=10;
Integer b=(int)-a;
System.out.println(b);
int x=25;
Integer c=(Integer)(-x); // If the pair of brackets around -x are dropped, a compile-time error is issued - illegal start of type.
System.out.println(c);
Integer d=(int)-a; //Compiles fine. Why does this not require a pair of braces around -a?
System.out.println(d);
}
}
このコードでは-x
、 のプリミティブ型int
をラッパー型にキャストしているInteger
ときに、コンパイル時エラーが発生します: illegal start of type
.
Integer c=(Integer)-x;
-x
のように中かっこのペアが必要ですInteger c=(Integer)(-x);
ただし、次の式は正常にコンパイルされます。
Integer d=(int)-a;
なぜこれ-a
は前の式のように一対の中かっこを必要としないのでしょうか?