Java Type Casting Programs
In this tutorial, we will learn how to perform typecasting programs in java. But before moving forward if you are not familiar with the concept of typecasting in java, then do check this article on TypeCasting in Java.
Program 1: Java Type Casting Program
In this program, we will see how to perform widening or automatic type conversion in java.
Algorithm:
- Start
- Create an instance of the Scanner class.
- Declare a variable.
- Ask the user to initialize it.
- Perform the widening or automatic type conversion.
- Print the value for each data type.
- Stop.
Below is the code for the same.
//Type Casting Program in Java
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
//Take input from the user
// create an object of Scanner class
Scanner sc = new Scanner(System.in);
// ask users to enter the number
System.out.println("Enter the number: ");
int i=sc.nextInt();
// widening or automatic type conversion
long l = i;
float f = l;
double d= f;
System.out.println("After widening or automatic type conversion values are: ");
System.out.println("Int value "+i);
System.out.println("Long value "+l);
System.out.println("Float value "+f);
System.out.println("Double value "+d);
}
}
Enter the number: 50
After widening or automatic type conversion values are:
Int value 50
Long value 50
Float value 50.0
Double value 50.0
Program 2: Java Type Casting Program
In this program, we will see how to perform narrowing or explicit type conversion in java.
Algorithm:
- Start
- Create an instance of the Scanner class.
- Declare a variable.
- Ask the user to initialize it.
- Perform the narrowing or explicit type conversion.
- Print the value for each data type.
- Stop.
Below is the code for the same.
//Type Casting Program in Java
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
//Take input from the user
// create an object of Scanner class
Scanner sc = new Scanner(System.in);
// ask users to enter the number
System.out.println("Enter the number: ");
double d=sc.nextDouble();
// narrowing or explicit type conversion
float f=(float)d;
//explicit type casting
long l = (long)d;
//explicit type casting
int i = (int)l;
System.out.println("After narrowing or explicit type conversion values are: ");
System.out.println("Double value: "+d);
//fractional part lost
System.out.println("Float value: "+f);
System.out.println("Long value: "+l);
//fractional part lost
System.out.println("Int value: "+i);
}
}
Enter the number: 123.541346731783
After narrowing or explicit type conversion values are:
Double value: 123.541346731783
Float value: 123.54134
Long value: 123
Int value: 123
Program 3: Java Type Conversion Program
In this program, we will see how to perform type conversion in java. Here, we will convert an integer value to a string value in java.
Algorithm:
- Start
- Declare a variable.
- Ask the user to initialize the variable.
- Convert the integer to string value.
- Print the string value.
- Stop
Below is the code for the same.
//Type Conversion Program in Java
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
//Take input from the user
// create an object of Scanner class
Scanner sc = new Scanner(System.in);
// ask users to enter the number
System.out.println("Enter the integer value: ");
int num=sc.nextInt();
String data = String.valueOf(num);
System.out.println("The string value is: " + data);
}
}
Enter the number: 1234
The string value is: 1234
Program 4: Java type conversion Program
In this program, we will see how to perform type conversion in java. Here, we will convert a string value to an integer value in java.
Algorithm:
- Start
- Declare a variable.
- Ask the user to initialize the variable.
- Convert the string to the integer value.
- Print the integer value.
- Stop
Below is the code for the same.
//Type Conversion Program in Java
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
//Take input from the user
// create an object of Scanner class
Scanner sc = new Scanner(System.in);
// ask users to enter the string
System.out.println("Enter the string value");
String str=sc.nextLine();
int num = Integer.parseInt(str);
System.out.println("The integer value is: " + num);
}
}
Enter the string value 245
The integer value is: 245