package com.datatypes;
public class TypeCastingExample {
public static void main(String args[]){
byte b=1;
short s = 2;
int i= 300;
long l=4l;
char c = 'e';
boolean bn = false;
//long myLong = 123123123123123l; usage of long literal. without l prefix it's out of range
b = (byte) c;
System.out.println("Boolean value is-----"+bn);//cannnot cast boolean to either datatype AND otherway around
System.out.println("after conversion from character+++"+b);//char to byte
float f = 10.00f;
double d = 20d;
d = f;
System.out.println("value of d is --------"+d);//narrowing downcasting
System.out.println("Byte is........"+b);
System.out.println("short is........"+s);
System.out.println("Integer is......"+i);
System.out.println("Long is......"+l);
b = (byte)i;//LOSS OF DATA AS INTEGER TO BYTE CAUSES DATA LOSS//CONVERT THE INTEGER TO DECIMAL TRUNCATE HIGH ORDER BYTES THAN USING 2S COMPLIMENT GENERATES BYTE VALUE
i = s;
System.out.println("Byte value after narrowing is........."+b);
System.out.println("Integer value after widening is........."+i);
}
}
No comments:
Post a Comment