简体   繁体   中英

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?

I tried this in JCreator LE under Windows 7.

It depends on what your console does (or whatever else is handling 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. However, many UI controls will treat that as a terminating character. This isn't Java's "fault" - it's the UI control itself.

(Just for reference, running that code within a Windows command prompt on Windows 7 is fine for me.)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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