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は前の式のように一対の中かっこを必要としないのでしょうか?