java打印字符数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22889298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java printing char array
提问by sakura
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,
这仅打印“abc”。尽管,
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.
为什么打印缓冲区在空 (0) 字符处停止?这是否意味着 Java 只是不断地将字符附加到缓冲区并打印该缓冲区?当然,由于该缓冲区之间有 0,因此它会丢弃字符串的其余部分。
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?
可能我已经回答了我自己的问题的一部分。但我想知道更多关于这方面的细节。JVM 如何处理这个问题?这个输出缓冲区在哪里?有什么理由停在0?还为什么添加 \n 会阻止这种行为?
Edit 1: Using JDK 1.7, Eclipse 3.8.1 and Ubuntu 13.10
编辑 1:使用 JDK 1.7、Eclipse 3.8.1 和 Ubuntu 13.10
Edit 2: Strangely, this one does not have that problem. https://ideone.com/VwFbRr
编辑 2:奇怪的是,这个没有那个问题。https://ideone.com/VwFbRr
Edit 3: I ran the same on command line
编辑 3:我在命令行上运行相同的
[bin]$ java com.sakura.C
abcccf adksjfhak
回答by Warren Dew
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.
最有可能的是,在这两种情况下,所有这些都被“打印”到操作系统中,但在第一种情况下,您的操作系统或 shell 将“0”字符作为不显示该行其余部分的信号。在第一种情况下,两个字符串都出现在同一行上,因此第二个字符串也被抑制了。在第二种情况下,第二个字符串在一个新行上,因此被显示。
回答by Stephen C
The behaviour you are seeing cannotbe 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.
仅查看 Java 代码无法解释您所看到的行为。相反,我怀疑这与您用来查看输出的内容有关。
First this:
首先这个:
char[] str = { 'a', 'b', 'c', 0, 'c', 'c', 'f' };
System.out.print(str);
According to the PrintWriter javadoc,
根据 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.
对于每个字符。(是的,每一个,包括零字符!)。而write(char)
to的行为只是根据默认的平台编码对字符进行编码,并编写它。对于零字符(代码点零)和典型的 7 或 8 位字符集,这将写入NUL
字符。
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.
在带有标准 Java I/O 类的标准 Java 字符串的 Java 端没有时髦的“字符串结尾的零均值”。时期。
I can think of 3 possible explanations:
我能想到 3 种可能的解释:
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 usedSystem.setOut(...)
to redirect the output via that class.
你误会了。您的实际代码正在编写不同的东西。(例如,您可能忘记重新编译...如果您是从命令行进行测试。)
您看到的奇怪行为正在发生,因为您的控制台和/或用于显示输出的实用程序正在使用 NUL 字符执行“特殊”操作。(但是,我不记得听说过以这种奇怪的方式处理 NUL 的控制台等程序......)
您的应用程序创建了一个自定义子类型,
PrintWriter
该子类型实现了对代码点零的一些特殊处理,并且它已用于System.setOut(...)
通过该类重定向输出。
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.
话虽如此,尝试打印包含零/NUL 字符的字符串或字符数组可能是一个坏主意。NUL 字符是一个“控制代码”,通常被归类为不可打印。当您尝试打印它时,您会看到......不可预测。
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 ...
如果你想更进一步,我建议你将可疑的输出重定向到一个文件,然后使用一些(特定于操作系统的)实用程序来查看文件的字节;例如od
在 Unix / Linux 系统上。我希望看到文件中的零字节......