Classes/Objects  «Prev 


The NumericConvert Program

The NumericConvert program shows some of the potential problems that you may encounter as the result of numeric promotion. As is, the program compiles, runs, and displays a value of 11 as its output. However, if you remove the parentheses from around (b + 1) or remove the (byte) cast operator, the program will not compile. That is because in the expression (b + 1), b is promoted to an int and added to 1.
The result of the expression is an int value. This value must be explicitly cast to a byte in order to avoid the compilation error.

NumericConvert

public class NumericConvert {
  public static void main(String[] args) {
    byte b = 10;
    b = (byte)(b + 1);
    System.out.println(b);
  }
}