Home > Articles

  • Print
  • + Share This

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
  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.