Program to show Type Casting in JAVA

Type Casting: Type Casting refers to changing an entity of one datatype into another. means treating a variable of one type as though it is another type.

Type Casting are of two types:
  1. Widining/Unboxing/Implicit Typecasting:Converting From smaller data types into Larger data types.
  2. Narrowing / Boxing / Explicit Typecasting:Converting From Larger data types into smaller data types.
byte -> short -> int -> long -> float -> double

Casting in Java is safer than in C or other languages that allow arbitrary casting. Java only lets casts occur when they make sense, such as a cast between a float and an int. However you can’t cast between an int and a String (is an object in Java).

When up casting primitives as shown below from left to right, automatic conversion occurs. But if you go from right to left, down casting or explicit casting is required.

Example1:
public class Conversion
{
public static void main(String[] args)
{
boolean t = true;
byte b = 2;
short s = 100;
char c = 'C';
int i = 200;
long l = 24000;
float f = 3.14f;
double d = 0.000000000000053;
String g = "string";
System.out.println("Value of all the variables like");
System.out.println("t = " + t );
System.out.println("b = " + b );
System.out.println("s = " + s );
System.out.println("c = " + c );
System.out.println("i = " + i );
System.out.println("l = " + l );
System.out.println("f = " + f );
System.out.println("d = " + d );
System.out.println("g = " + g );
System.out.println();
//Convert from boolean to byte.
b = (byte)(t?1:0);
System.out.println("Value of b after conversion : " + b);
//Convert from boolean to short.
s = (short)(t?1:0);
System.out.println("Value of s after conversion : " + s);
//Convert from boolean to int.
i = (int)(t?1:0);
System.out.println("Value of i after conversion : " + i);
//Convert from boolean to char.
c = (char)(t?'1':'0');
System.out.println("Value of c after conversion : " + c);
c = (char)(t?1:0);
System.out.println("Value of c after conversion in unicode : " + c);
//Convert from boolean to long.
l = (long)(t?1:0);
System.out.println("Value of l after conversion : " + l);
//Convert from boolean to float.
f = (float)(t?1:0);
System.out.println("Value of f after conversion : " + f);
//Convert from boolean to double.
d = (double)(t?1:0);
System.out.println("Value of d after conversion : " + d);
//Convert from boolean to String.
g = String.valueOf(t);
System.out.println("Value of g after conversion : " + g);
g = (String)(t?"1":"0");
System.out.println("Value of g after conversion : " + g);
int sum = (int)(b + i + l + d + f);
System.out.println("Value of sum after conversion : " + sum);
} // end of main function
} // end of Conversion.java

Example2:
public class TypeCasting
{
public static void main(String[] args)
{
double answer;
int integer1=3;
int integer2=2;
answer=1.5+ integer1/integer2;
System.out.println("Answer when Typecasting is not done: " + answer);

answer=1.5+(double) integer1/integer2;
System.out.println("Answer After using typecasting technique " + answer);
} // end of main function
} // end of TypeCasting.java

1 comment:

Unknown said...

http://java.meritcampus.com/core-java-topics/type-conversion-in-java

Google Search