Language
Version: SE 8
Categories
-
User Program Communication
-
Variables
-
Control Flow
-
Object Oriented Programming
-
String Class
Variables
Type Casting in Java
Type casting is used to convert an object or variable of one type into another.
Syntax
dataType variableName = (dataType) variableToConvert;
Notes
There are two casting directions: narrowing (larger to smaller type) and widening (smaller to larger type). Widening can be done automatically (for example, int to double), but narrowing must be done explicitly (like double to int).
Example
double calculatedMark = 87.6;
int finalGrade = (int)calculatedMark; //will return 87