Note that there are some explanatory texts on larger screens.

plurals
  1. POJava: int array initializes with nonzero elements
    primarykey
    data
    text
    <p>According to the JLS, an <code>int</code> array should be filled by zeros just after initialization. However, I am faced with a situation where it is not. Such a behavior occurs first in JDK 7u4 and also occurs in all later updates (I use 64-bit implementation). The following code throws exception:</p> <pre><code>public static void main(String[] args) { int[] a; int n = 0; for (int i = 0; i &lt; 100000000; ++i) { a = new int[10]; for (int f : a) if (f != 0) throw new RuntimeException("Array just after allocation: "+ Arrays.toString(a)); Arrays.fill(a, 0); for (int j = 0; j &lt; a.length; ++j) a[j] = (n - j)*i; for (int f : a) n += f; } System.out.println(n); } </code></pre> <p>The exception occurs after the JVM performs compilation of the code block and does not arise with <code>-Xint</code> flag. Additionally, the <code>Arrays.fill(...)</code> statement (as all other statements in this code) is necessary, and the exception does not occurs if it is absent. It is clear that this possible bug is bounded with some JVM optimization. Any ideas for the reason of such a behavior?</p> <p><em>Update:</em><br> I see this behavior on HotSpot 64-bit server VM, Java version from 1.7.0_04 to 1.7.0_10 on Gentoo Linux, Debian Linux (both kernel 3.0 version) and MacOS Lion. This error can always be reproduced with the code above. I did not test this problem with a 32-bit JDK or on Windows. I already sent a bug report to the Oracle (bug id 7196857) and it will appear in public Oracle bug database in few days.</p> <p><em>Update:</em><br> Oracle published this bug at their public bug database: <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7196857" rel="noreferrer">http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7196857</a> </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload