Wednesday, 8 August 2018

TYPE CASTING



The process of converting one data type to another is called type casting
Examples:
int m=50;
byte n=(byte)m;
long count=(long) m;
class Typecasting
{
public static void main(String args[])
{
System.out.println("variales created");
char c='x';
byte b=50;
short s=1996;
int i=123456789;
long l=1234567654321L;
float f1=3.142F;
float f2=1.2e-5F;
double d2=0.000000987;
System.out.println(" c="+c);
System.out.println(" b ="+b);
System.out.println(" s= "+s);
System.out.println(" i="+i);
System.out.println(" l="+l);
System.out.println(" f1= "+f1);
System.out.println(" f2="+f2);
System.out.println("d2 ="+d2);
System.out.println(" ");
System.out.println(" Types converted");
short s1=(short)b;
short s2=(short)i;
float n1=(float)l;
int m1=(int)f1;
System.out.println(" (short)b ="+s1);
System.out.println(" (short)i ="+s2);
System.out.println(" (float)l= "+n1);
System.out.println(" (int)f1= - "+m1);
}
}
Note : floating point constants have a default type of double
For example when we want to declare a float variable and initializing it to constant
float x=7.56;
This will give “incompatible type for declaration. Explicit cast needed to convert double to
float.”
This should be written as

float x=7.56F

No comments: