07 Java Tutorials - Type Conversion in Java - Study Viral
Type Conversion is changing of one data type variable to other data type variable.
It can be lower data type to higher data type which known as Implicit Casting OR Widening Casting
OR Automatic Type Conversion.
byte ----> short ----> int -----> long -----> float -----> double
Also higher data type to lower data type is known as Explicit Type Conversion OR Narrowing Type Conversion.
byte <---- short <---- int <----- long <----- float <----- double
==============================================
class TypeConversion{
public static void main(String []args){
byte a = 126;
System.out.println("byte = "+a);
short b = a;
System.out.println("short = "+b);
long c = b;
System.out.println("long = "+c);
float d = c;
System.out.println("float = "+d);
d = d+0.254f;
System.out.println("changed float = "+d);
c = (long)d;
System.out.println("long = "+c);
c = c+126;
System.out.println("long = "+c);
a = (byte)c;
System.out.println("byte = "+a);
}
}
Output :
public static void main(String []args){
byte a = 126;
System.out.println("byte = "+a);
short b = a;
System.out.println("short = "+b);
long c = b;
System.out.println("long = "+c);
float d = c;
System.out.println("float = "+d);
d = d+0.254f;
System.out.println("changed float = "+d);
c = (long)d;
System.out.println("long = "+c);
c = c+126;
System.out.println("long = "+c);
a = (byte)c;
System.out.println("byte = "+a);
}
}
Output :
0 comments:
Post a Comment