␡
- Data Types and Other Tokens
- Working with Variables
- The boolean Primitive
- The Flavors of Integer
- Operators
- Character Variables
- Floating-Point Variables
- Literals: Assigning Values
- Integer Literals
- Character Literals
- Floating-Point Literals
- String Literals
- Arrays
- Non-Token Input Elements
- Troubleshooting
< Back
Page 15 of 15
Troubleshooting
Floating-Point Literal Loss of Precision Error
When you assign a floating-point literal to a float variable, you must explicitly specify the type suffix. When you omit the suffix, the compiler assumes that the literal is of type double, which cannot be assigned to a float without an explicit cast. The following two statements illustrate a correct and an incorrect assignment statement:
float myFirstFloat = 1.0; // compiler error float mySecondFloat = 2.0F; // correct
ArrayIndexOutOfBoundsException
Remember that the first index in a Java array is 0 and the last valid index is one less than the length of the array. For example:
// create an array to hold two integer values int[ ] dataArray = new int[2]; // several correct data assignment statements dataArray[0] = 23; // valid assignment to first index dataArray[1] = 43; // valid assignment to last index dataArray[dataArray.length-1] = 50; // valid assignment to last index // runtime errors dataArray[2] = 55; // ArrayIndexOutOfBoundsException dataArray[dataArray.length] = 55; // ArrayIndexOutOfBoundsException
< Back
Page 15 of 15