Answer the question
In order to leave comments, you need to log in
Strange behavior with array initialization in Java?
I have come across some very strange behavior in Java .
According to the Java specification, when an array is created, its elements must be initialized with default values. For elements of type byte , the default value is 0 .
Consider the code on which I was able to reproduce the strange behavior:
package javatest;
import java.io.*;
public class JavaTest
{
public static void main(String[] args) throws IOException
{
byte[] buf = new byte[5];
System.in.read(buf);
for (int i = 0; i < buf.length; ++i)
{
System.out.println((char)buf[i]);
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question