Objectives
- Discuss what variables are
- Determine validity of variable names in Java
- Discuss and enumerate the primitive data types in Java
- Create variables and store data in variables
- Cast variables to different data types
- Declare constants
You can make a variable or value temporarily look like a different data type by "casting" it. You "cast" a variable by putting another data type name within parentheses in front of the value or variable. For example, the following code examples will display 5 instead of 5.9:
double x = 5.9; System.out.println((int) x); System.out.println((int) 5.9);
Notice that casting a floating point number to an integer truncates instead of rounding.