5

In the following line, can buffer be assumed to be filled with zeroes?

byte buffer[] = new byte[120];
Andreas
  • 7,470
  • 10
  • 51
  • 73
  • 5
    This only takes 10 sec to test yourself... – AppX Sep 30 '13 at 12:32
  • 7
    @AppX For instance in C, this would work _sometimes_ depending on compiler and lucky memory allocation. I didn't know whether it was the same for Java. – Andreas Sep 30 '13 at 12:33
  • 3
    @AppX But then you don't know if it only applies in your environment or is consistent over all the different ones and won't change over time. – Angelo Fuchs Sep 30 '13 at 12:34
  • 1
    Java is much more system independant than C, unless you're doing something crazy it will be the same on all systems. cc @AngeloNeuschitzer – Richard Tingle Sep 30 '13 at 12:50

4 Answers4

12

Language-lawyer-answer completely based on Java Language Specification:

10.3. Array Creation

An array is created by an array creation expression (§15.10) or an array initializer (§10.6).

10.6. Array initializers

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.

Community
  • 1
  • 1
default locale
  • 13,035
  • 13
  • 56
  • 62
11

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

The default value of a byte is 0.

enter image description here

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
8

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.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Local object references are not initialized with null on construction. {Byte b; System.out.print(b);} Will not compile since b is a local variable and not a field – Ron Sep 30 '13 at 12:38
  • @RonE Could you expand, please? Would using `byte b` solve that? – Andreas Sep 30 '13 at 12:42
  • 1
    IMHO, Local variables are not constructed. There is no `new` for a local variable. ;) – Peter Lawrey Sep 30 '13 at 12:45
  • @Andreas No, the compiler checks all local variables to ensure they have an initial value before being used. – Peter Lawrey Sep 30 '13 at 12:45
  • @PeterLawrey Consider `byte buffer[] = new byte[120]; int num_bytes = myInputStream.read(buffer, 0, 120);` . What about the bytes in `buffer` after `num_bytes`? How have they been initialised? – Andreas Sep 30 '13 at 12:48
  • 1
    @Andreas They have been initialised before the read, even if they didn't need to be. See the quote from the JLS I have added. ;) – Peter Lawrey Sep 30 '13 at 12:50
-2

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

Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64
  • 12
    Because there's no way of testing whether it's _guaranteed_. What if I were just lucky with memory allocation? – Andreas Sep 30 '13 at 12:34
  • 3
    This is not an answer. There is nothing here or in the link that answers the question. It should have been posted as a comment. – user207421 Oct 01 '13 at 02:17