简体   繁体   English

为什么System.out.println()以ASCII码零终止

[英]Why does System.out.println() terminate at ASCII Code zero

Try this piece of code - 试试这段代码-

public class WhitespaceTest
{
    public static void main(String[] args)
    {
        int x = 0;
        char c = (char) x;
        System.out.println("c-->"+c+"<---this doesn't print?");
    }
}

The output is - 输出是-

c-->

Why does System.out.println() terminate at ASCII code zero? 为什么System.out.println()以ASCII码零终止?

I tried this in JCreator LE under Windows 7. 我在Windows 7下的JCreator LE中尝试了此操作。

It depends on what your console does (or whatever else is handling System.out ). 这取决于您的控制台执行的操作(或其他正在处理System.out )。 System.out will propagate all the information just fine, and if your console attaches no particular meaning to U+0000 then all will be well. System.out会很好地传播所有信息,如果您的控制台对U + 0000没有特殊含义,那么一切都会很好。 However, many UI controls will treat that as a terminating character. 但是,许多UI控件会将其视为终止字符。 This isn't Java's "fault" - it's the UI control itself. 这不是Java的“故障”,而是UI控件本身。

(Just for reference, running that code within a Windows command prompt on Windows 7 is fine for me.) (仅供参考,对我而言,在Windows 7上的Windows命令提示符下运行该代码就可以了。)

In C/C++ style strings the end of the string is determined by the location of the terminating null-character (the character with value 0, like in your case). 在C / C ++样式字符串中,字符串的结尾由终止空字符(如您所用的情况,值为0的字符)的位置确定。 Most likely the System.out.println() call passes the string to the OS verbatim, which thinks the string ended at the null-character, and only prints up to that point 最有可能System.out.println()调用将字符串逐字传递给操作系统,操作系统认为字符串以空字符结尾,并且仅打印到该点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM