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 ...
abc ccf adksjfhak
andabc ccf\n adksjfhak
for me. I'm not sure what the problem is. – Disembowel"abcccf adksjfhak"
. – Shelves