Java Type casting

AmirthaVembu

 TYPE CASTING:

The process of convening the value of one data type (int, float, double etc..) to another type data type is known as type casting. In java there are 13 types of type conversion.

The major 2 types they are:

1. Widening Type Casting
2. Narrowing Type Casting

1.WIDENING TYPE CASTING:

It is automatically converts one data type to another data type is called widening type casting.

Example for widening type casting:

Program:

import java.util.*;

class main

{

public static void main(String args[])

{

//create int type variable.

int num=10;

System.out.println("The integer value:"+num);

//convert into double type

double data =num;

System.out.println("The double value:"+data);

}

}

Output:

The integer value: 10

The double value: 10.99


2.NARROWING TYPE CASTING:

 To convert one data type into another using the parenthesis is called narrowing type casting.


Example for narrowing type casting:

Program:

import java.util.*;

class main

{

public static void main(String args[])

{

//create double type variable.

double num =10.99;

System.out.println(" The double value:"+num);

//convert into int type

int data =(int)num;

System.out.println("The integer value:"+data);

}

}

Output:

The double value: 10.99

The integer value: 10



Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send