Casting: Turning on data type into another
Syntax:
Just put the desired type in parentheses in front of the value
Ex.
Double x = 10.3;
Int y = (int)x;
The y becomes 10, otherwise, you would get an error
You can also just make one number a double, by just adding a .0 to the number
Implicit casting: Java changes type automatically
Ex.
Java will turn integers into doubles automatically
Integer Storage Limitations:
- An integer is stored in 32 bit, which means that there are “only” 2^32 ~ 4 billion combinations, so that is why the integer range is from -2,147,483,648 to 2,147,483,647
- Also written as Integer.MIN_VALUE AND Integer.MAX_VALUE
Casting a double to an int:
It truncates to the lowest whole number, it won’t round normally.
No Responses