Java Type Casting
Java Tutorial » Java Type Casting
Converting the value of one data type as int, long, float, double, etc. to another data type named typecasting.
There are 13 types of type conversion in Java
In this tutorial, we target on the major 2 types.
1. Widening Type Casting - (automatically) - converting a smaller type to a larger type size
2. Narrowing Type Casting - (manually) - converting a larger type to a smaller size type
Widening Type Casting
Example: Converting int to double
The integer value of my number is: 24
The double value of my number: 24.0
public class Main {
public static void main(String[] args) {
// create int type variable
int my_number = 24;
System.out.println("The integer value of my number is: " + my_number);
// convert into double type
double data = my_number;
System.out.println("The double value of my number: " + data);
}
}
Output:public static void main(String[] args) {
// create int type variable
int my_number = 24;
System.out.println("The integer value of my number is: " + my_number);
// convert into double type
double data = my_number;
System.out.println("The double value of my number: " + data);
}
}
The integer value of my number is: 24
The double value of my number: 24.0
Narrowing Type Casting
Example: Converting double into an int
The double value of my number: 24.98
The integer value of my number: 24
public class Main {
public static void main(String[] args) {
// create double type variable
double my_number = 24.98;
System.out.println("The double value of my number: " + my_number);
// convert into int type
int data = (int)my_number;
System.out.println("The integer value of my number: " + data);
}
}
Output:public static void main(String[] args) {
// create double type variable
double my_number = 24.98;
System.out.println("The double value of my number: " + my_number);
// convert into int type
int data = (int)my_number;
System.out.println("The integer value of my number: " + data);
}
}
The double value of my number: 24.98
The integer value of my number: 24
Converting the value of one data type (short, char, int , float , double , etc.) to another data type is known as typecasting.
Java Type Casting objects, string to int, example, rules, double to int, parent to child, syntax, object, error
Java Type Casting - java
Online Editor
This tool makes it easy to create, adjust, and experiment with custom colors for the web.
HTML Templates

Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.
CSS HTML Layout

Find here examples of creative and unique website layouts.
Free CSS HTML Menu

Find here examples of creative and unique website CSS HTML menu.