Program to Demonstrate Type Casting

Program to Demonstrate Type Casting:-

19 specific conversions on primitive types are called the widening primitive conversions:
  • byte to short, int, long, float, or double
  • short to int, long, float, or double
  • char to int, long, float, or double
  • int to long, float, or double
  • long to float or double
  • float to double

class typecasting
public static void main(String args[])
byte h=127;
int a=300;
float a1=12.222f;
float g;
short b=200;
long c=999999;
float e=345.89F;
double f=45645.78222222222222;
g= (float)f;
System.out.println("short b ="+g);
System.out.println("short b ="+b);
System.out.println("long c ="+c);
System.out.println("float e="+e);
System.out.println("double f="+f);
System.out.println("short b="+b);
System.out.println("short to byte "+(byte)b);
System.out.println("int to byte "+(byte)a);
System.out.println("int to float"+(float)a);
System.out.println("long to byte "+(byte)c);
System.out.println("double to long "+(long)f);
System.out.println("double to int "+(int)f);
System.out.println("double to byte "+(byte)f);
System.out.println("double to short "+(short)f);
System.out.println("double to float "+(float)f);
System.out.println("float to int "+(int)e);
System.out.println("float to byte "+(byte)e);
System.out.println("float to short "+(short)e);
System.out.println("float to long "+(long)e);
System.out.println("float to double ="+(double)e);
System.out.println("long to int"+(int)c);
System.out.println("byte to int ="+(int)h);
Powered by Blogger.