Java Variable Narrowing Example

Java Programming Java8Java Technologies Object Oriented Programming

Narrowing refers to passing a higher size data type like int to a lower size data type like short. It may lead to data loss. Casting is required for narrowing conversion. Following program output will be 44.

public class MyFirstJavaProgram {
   public static void main(String []args) {
      int a = 300;
      byte b = (byte)a; // narrowing
      System.out.println(b);
   }
}
raja
Published on 22-Jan-2018 05:53:45
Advertisements