4.2.17.Casts are required when you want to perform a narrowing conversion.
public class MainClass { public static void main(String[] argv) { short s = 259; byte b = s; // Compiler error
b = (byte) s; // Explicit cast
System.out.println("b = " + b);
System.out.println("s = " + s + ", b = " + b);
}
}
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from short to byte
at MainClass.main(MainClass.java:4)