Java Program to Implement Type Casting and Type Conversion
Last Updated :
28 Oct, 2021
There are many situations that come when we have to change the functionality of the output as well as the type of output according to the need of the requirements. For e.g. Entered PI value in decimal format should be converted in the decimal format so that value can be used efficiently without any kind of errors and wrong output.
There are two ways we can change the type from one to another.
- Type casting
- Type conversion
Type Casting means to change one state to another state and is done by the programmer using the cast operator. Type Casting is done during the program design time by the programmer. Typecasting also refers to Narrow Conversion. Because in many cases, We have to Cast large datatype values into smaller datatype values according to the requirement of the operations. We can also convert large datatype values into smaller datatype values that's why Type Casting called Narrow Casting.
Syntax: () is Cast Operator
RequiredDatatype=(TargetType)Variable
Example 1:
Java
// Java Program to Implement Type Casting of the Datatype
// Importing input output classes
import java.io.*;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring an Integer datatype
int a = 3;
// Casting to Large datatype
double db = (double)a;
// Print and display the casted value
System.out.println(db);
// Narrow Casting conversion
int db1 = (int)db;
// Print an display narrow casted value
System.out.println(db1);
}
}
Type Conversion is a type conversion that is done by compiler done by a compiler without the intention of the programmer during the compile time. This Conversion sometimes creates an error or incorrect answer if the proper resultant data type does not mention at the end of the multiple expression of the datatype that's why Type Conversion is less efficient than Type Casting.
The working mechanism of type conversion is as follows:
double > float > long > int > short > byte
Example 1: Error during type conversion
Java
// Java Program to illustrate Type Conversion
// Importing input output classes
import java.io.*;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring and initializing variables to values
// with different return type
long a = 3;
byte b = 2;
double c = 2.0;
// Type Conversion
int final_datatype = (a + b + c);
// Print statement
System.out.print(final_datatype);
}
}
Output:
prog.java:9: error: incompatible types: possible lossy conversion from double to int
int final_datatype = (a + b + c);
^
1 error
Output explanation:
When you assign the value of one data type to another, the two types might not be compatible with each other. If the data types are compatible, then Java will perform the conversion automatically known as Automatic Type Conversion, and if not then they need to be cast or converted explicitly. For example, assigning an int value to a long variable.
Example 2:
Java
// Java Program to illustrate Type Conversion
// Importing input output classes
import java.io.*;
// Main Class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring and initializing variables to values
// but to different data types
long a = 3;
byte b = 2;
double c = 2.0;
// Type Conversion
// As long and byte data types are converted to
// double return type
double final_datatype = (a + b + c);
// Printing the sum of all three initialized values
System.out.print(final_datatype);
}
}
Similar Reads
Java Program For Int to Char Conversion In this article, we will check how to convert an Int to a Char in Java. In Java, char takes 2 bytes (16-bit UTF encoding ), while int takes 4 bytes (32-bit). So, if we want the integer to get converted to a character then we need to typecast because data residing in 4 bytes cannot get into a single
3 min read
Java Program to Convert Char to Int Given a char value, and our task is to convert it into an int value in Java. We can convert a Character to its equivalent Integer in different ways, which are covered in this article.Examples of Conversion from Char to Int:Input : ch = '3'Output : 3Input : ch = '9'Output : 9The char data type is a s
4 min read
Java Program to Convert Char to Byte Given a char in Java, the task is to write a Java program that converts this char into Byte. Examples: Input: ch = 'A' Output: 65 Input: ch = 'B' Output 66 In Java, char is a primitive data type and it is used to declare characters. It has the capability to hold 16-bit unsigned Unicode characters. T
3 min read
Java Program to Convert Int to Double Java data types can be categorized as primitive and non-primitive. Primitive data types contain a single value, whereas non-primitive data types contain an address of the variable value. Java supports 7 primitive data types - boolean, byte, char, short, int, long, float, and double. These data types
4 min read
Java Program to Convert int to long Given a number of int datatype in Java, your task is to write a Java program to convert this given int datatype into a long datatype. Example: Input: intnum = 5 Output: longnum = 5 Input: intnum = 56 Output: longnum = 56 Int is a smaller data type than long. Int is a 32-bit integer while long is a 6
2 min read