KEMBAR78
Type casting in java | PPTX
 Type Casting.
 Assigning a value of one type to a variable of
another type is known as Type Casting.
 Example
 int x = 10; byte y = (byte)x;
 There are TWO types of type casting.
1. Widening Casting(Implicit).
2. Narrowing Casting(Explicit).
 In widening casting we converted the data or
value into broad data
 Example
public class Test
{
public static void main(String[] args)
{ int i = 100;
long l = i; //no explicit type casting required
float f = l; //no explicit type casting required
System.out.println("Int value "+i);
System.out.println("Long value "+l);
System.out.println("Float value "+f);
}
}
 Out put
Int value 100
Long value 100
Float value 100.0
 In Narrowing casting we converted the data
or value into Narrow data
 Example.
 public class Test
{
public static void main(String[] args)
{ double d = 100.04;
long l = (long)d; //explicit type casting required
int i = (int)l; //explicit type casting required
System.out.println("Double value "+d);
System.out.println("Long value "+l);
System.out.println("Int value "+i);
}
}
 Output
Double value 100.04
Long value 100
Int value 100
Thank you.

Type casting in java

  • 3.
  • 4.
     Assigning avalue of one type to a variable of another type is known as Type Casting.  Example  int x = 10; byte y = (byte)x;
  • 5.
     There areTWO types of type casting. 1. Widening Casting(Implicit). 2. Narrowing Casting(Explicit).
  • 6.
     In wideningcasting we converted the data or value into broad data  Example
  • 7.
    public class Test { publicstatic void main(String[] args) { int i = 100; long l = i; //no explicit type casting required float f = l; //no explicit type casting required System.out.println("Int value "+i); System.out.println("Long value "+l); System.out.println("Float value "+f); } }  Out put Int value 100 Long value 100 Float value 100.0
  • 8.
     In Narrowingcasting we converted the data or value into Narrow data  Example.
  • 9.
     public classTest { public static void main(String[] args) { double d = 100.04; long l = (long)d; //explicit type casting required int i = (int)l; //explicit type casting required System.out.println("Double value "+d); System.out.println("Long value "+l); System.out.println("Int value "+i); } }  Output Double value 100.04 Long value 100 Int value 100
  • 10.