In the following line, can buffer
be assumed to be filled with zeroes?
byte buffer[] = new byte[120];
In the following line, can buffer
be assumed to be filled with zeroes?
byte buffer[] = new byte[120];
Language-lawyer-answer completely based on Java Language Specification:
An array is created by an array creation expression (§15.10) or an array initializer (§10.6).
ArrayInitializer:
{ VariableInitializersopt ,opt }
So, we can conclude that your expression is not an array initializer (it isn't wrapped in braces). So we move to array creation:
15.10.1 Run-Time Evaluation of Array Creation Expressions
...Then, if a single DimExpr appears, a one-dimensional array is created of the specified length, and each component of the array is initialized to its default value (§4.12.5).
Otherwise, if n DimExpr expressions appear, then array creation effectively executes a set of nested loops of depth n-1 to create the implied arrays of arrays.
And finally:
4.12.5. Initial Values of Variables
...For type byte, the default value is zero, that is, the value of (byte)0.
So the answer is Yes any implementation of java is expected to initialize byte array with zeroes.
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
The default value of a byte
is 0.
All objects and arrays are initialised with zeros or the equivalent (null, false) on construction.
The default values are listed in JLS 4.12.5 and thus are guaranteed.
FROM 4.12.5. Initial Values of Variables
Each class variable, instance variable, or array component is initialized with a default value when it is created (§15.9, §15.10):
For type byte, the default value is zero, that is, the value of (byte)0.
For type short, the default value is zero, that is, the value of (short)0.
For type int, the default value is zero, that is, 0.
For type long, the default value is zero, that is, 0L.
For type float, the default value is positive zero, that is, 0.0f.
For type double, the default value is positive zero, that is, 0.0d.
For type char, the default value is the null character, that is, '\u0000'.
For type boolean, the default value is false.
For all reference types (§4.3), the default value is null.
Why you didn't try this one.
byte buffer[] = new byte[120];
for (int i = 0; i < buffer.length; i++)
{
System.out.println(buffer[i]);
}
Please find the reference
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html