java printing char array
Asked Answered
Y

2

6

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
Yesseniayester answered 6/4, 2014 at 2:26 Comment(8)
This works fine for me (JDK 1.7)Shelves
@Christian you mean in the first case, do you get both abc and adksjfhak?Yesseniayester
Respectively, it's printing out abc ccf adksjfhak and abc ccf\n adksjfhak for me. I'm not sure what the problem is.Disembowel
@Yesseniayester I get "abcccf adksjfhak".Shelves
@Christian I'm also using JDK1.7 + eclipse. Anything to do with eclipse's console settings?Yesseniayester
@Yesseniayester I've not touched Eclipse configurations, at least not to configure anything about output/console. Maybe it depends on OS?Shelves
Cannot replicate your results bit.ly/1idejXOEulogistic
Did anyone try using Linux?Yesseniayester
K
3

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:

  1. 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.)

  2. 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 ...)

  3. 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 ...

Kraken answered 6/4, 2014 at 2:58 Comment(1)
I think it should be 2 here. It is just a stand alone, single class. But I'm using JDK 1.7, Eclipse 3.8.1 and Ubuntu 13.10, which is pretty common setup. Also I have not done anything to JVM settings, or any changes to Linux.Yesseniayester
Z
0

Most likely, all of it is getting "printed" to the operating system in both cases, but in the first case, your operating system or shell takes the '0' character as a signal not to display the rest of the line. In the first case, both strings appear on the same line so the second string gets suppressed too. In the second case, the second string is on a new line and so gets displayed.

Zeller answered 6/4, 2014 at 2:47 Comment(3)
Have you tried running the code? If anything, the terminal emulator could be at issue, but since the OP is running in Eclipse, that's not likely. Just like the others including me who've tried, and were not able to reproduce the issue - it's just printing 'abcccf' in both cases, not displaying any output for the 0 character.Secondclass
Yes, looks like linux is doing something here. Any references, anyone?Yesseniayester
@ErwinBolwidt The 0 character is not a printable character so it would not be expected to be displayed.Zeller

© 2022 - 2024 — McMap. All rights reserved.