I am running this code and
char[] str = { 'a', 'b', 'c', 0, 'c', 'c', 'f' };
System.out.print(str);
System.out.println(" adksjfhak");
This prints just "abc". while,
char[] str = { 'a', 'b', 'c', 0, 'c', 'c', 'f' };
System.out.print(str);
System.out.println("\n adksjfhak");
prints
abc
adksjfhak
Why does print buffer stop at null (0) character? Does this mean Java just keeps appending character to buffer and prints that buffer? And of course, since that buffer has 0 in between, it discards rest of the string.
Probably I have answered part of my own question. But I would like to know more details about this. Hows JVM handles this? Where is this output buffer? And Any reason to stop at 0? ALso why adding \n stops this behaviour?
Edit 1: Using JDK 1.7, Eclipse 3.8.1 and Ubuntu 13.10
Edit 2: Strangely, this one does not have that problem. https://ideone.com/VwFbRr
Edit 3: I ran the same on command line
[bin]$ java com.sakura.C
abcccf adksjfhak
The behaviour you are seeing cannot be explained by looking just at the Java code. Rather, I suspect that it is something to do with what you are using to look at the output.
First this:
char[] str = { 'a', 'b', 'c', 0, 'c', 'c', 'f' };
System.out.print(str);
According to the PrintWriter javadoc,
System.out.print(str);
would be equivalent to calling
System.out.print(str[i]);
for each character. (Yes each one, including the zero character!). And the behaviour of write(char)
to to just encode the character according to the default platform encoding, and write it. For a Zero character (codepoint zero) and a typical 7 or 8 bit charset, that is going to write the NUL
character.
There is no funky "zero means of end of string" stuff on the Java side with standard Java strings of standard Java I/O classes. Period.
I can think of 3 possible explanations:
You are mistaken. Your actual code is writing something different. (For example, you might have forgotten to recompile ... if you are doing your testing from the command line.)
The strange behaviour you are seeing is happening because your console and/or the utility you are using to display the output is doing something "special" with NUL characters. (However, I don't recall hearing of a console, etc program that handled NUL in this strange way ...)
Your application has created a custom subtype of PrintWriter
that implements some special handling for codepoint zero, and it has used System.setOut(...)
to redirect the output via that class.
Having said that, it is probably a bad idea to try to print strings or character arrays that contain zero / NUL characters. The NUL character is a "control code" and is generally classed as not printable. What you will see when you try to print it is ... unpredictable.
If you want to pursue this further, I suggest that you redirect the suspect output to a file, and then use some (OS specific) utility to view the bytes of the file; e.g. od
on a Unix / Linux system. I would expect to see the zero bytes in the file ...