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

See Also
Related

Primitive Data Types

Add to Slack

< Primitive Data Types   |   Variable Declaration >

© 2019 SyntaxDB. All Rights Reserved.