Thursday 15 August 2013

java - JVM bug? Cached Object field value cause ArrayIndexOutOfBoundsException -


This is strange, but the code speaks more words, so check to see what I'm doing to see the exam. In my current setup (Java 7 Update 21 on Windows 64 bit) this test fails with the ArrayIndexOutOfBoundsException, but replaces the testing method code with the comment method, it works and I wonder why any part of the Java specification Can explain.

It seems I have been suggested "Michael Nesterno" because the value of the array area is cached in the stack called method, and not updated on the return from the phone. I can not tell whether it is a JVM bug or documented "customization" does not include any multi-threading or "magic".

  public class testib {personal string} array = new string [0]; Private Ent Grows (Last String Texture) {Final Int Index = Array.line; Array = Arrays.copyOf (array, index + 1); Array [index] = txt; Return index; } @Test Public Zero TestGrow () {// Final Integer Index = Increase ("Test"); //System.out.println(array[index]); Println (increasing array ("test")]); }}    

It is well defined: x to evaluate [Y] , before x is evaluated, and then the y is evaluated. In your case, x evaluates a string [] with zero elements, then, y modifies a member variable, and Assesses for 0 . Trying to access the 0th element of the array already returned. The fact is that member array changes have no effect on array lookup, because we are referenced on the string [] that array We evaluated it.

No comments:

Post a Comment