Dominic Cleal's Blog

Sat, 24 May 2008 15:25:04 GMT

permalink Don't doubt the JRE

Deep inside a Java-based application, a class exists that when distilled down looks similar to:

public class ExampleEntry
  private Integer num_ = new Integer(375);
  public void add(double x, int max)
    double[] prob = new double[num_];
    for (int i = 0; i < num_ && i < max; i++)
      prob[i] = func(x);
Using a debugger, i was observed to reach 375, i.e. attempting to access the 376th element of the array! This then threw an ArrayIndexOutOfBoundsException on line 10. The variable max was set high enough so the condition didn't meet, however the first part should have stopped the loop.

Upgrading the JRE from 1.5 to 1.6, or changing the input parameters for the class "fixed" it, though it can still be reproduced in the full test harness for the class. Recompiling in Eclipse instead of using ant caused the compiled instructions to change considerably and the newly compiled class worked fine.

Bizarre and slightly scary - you shouldn't have to doubt the JRE!

Comments for this entry are now closed.