Java type conversion and casting Question 2

Question

What is the output of the following code?

public class Main {
  public static void main(String[] args) {
    int i = 1; //from ww w.j a  v a2  s .  com
    byte b = i;  


    System.out.println(i); 
    System.out.println(b);

  }
}


int i = 1; 
byte b = i; // Error because explicit casting is required

Note

To assign a variable of the int type to a variable of the short or byte type, explicit casting must be used.

However, as long as the integer literal is within the permissible range of the target variable, explicit casting is not needed to assign an integer literal to a variable of the short or byte type.




PreviousNext

Related