Consider the following code:
byte b[] = new byte[34];
for (int x = 0; x < b.length; x++)
{
System.out.println((int) b[x] );
}
Java compiles it and runs it. This prints a bunch of "0".
So my question is this correct code? Or should/must I have a loop to
assign it to 0. Is it a rule that a newly created byte is 0 or is this
merly a coincidence?
-
Fred
Jim Korman - 20 Sep 2006 02:11 GMT
>Consider the following code:
>byte b[] = new byte[34];
[quoted text clipped - 11 lines]
>-
>Fred
<quote>
Each class variable, instance variable, or array component is
initialized with a default value when it is created
</quote>
http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.12.5
Jim
Chris Uppal - 20 Sep 2006 09:46 GMT
> So my question is this correct code? Or should/must I have a loop to
> assign it to 0. Is it a rule that a newly created byte is 0 or is this
> merly a coincidence?
It is a rule, part of the JLS (Java Language Specification). In Java it is
/never/ possible to "see" uninitialised memory.
-- chris