Why does System.out.println () end in ASCII code with a null value

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?"); } } 

Exit -

 c--> 

Why does System.out.println () exit with zero ASCII code?

I tried this on JCreator LE under Windows 7.

+8
java
source share
2 answers

It depends on what your console is doing (or something else to handle System.out ). System.out will distribute all the information just fine, and if your console does not attach much importance to U + 0000, then everything will be fine. However, many user interface controls will see this as a trailing character. This is not a Java “bug” - it is user interface control itself.

(Just for reference, running this code on the Windows command line in Windows 7 is right for me.)

+18
source share

In C / C ++ style strings, the end of the string is determined by the location of the terminating null character (character with a value of 0, as in your case). Most likely, a call to System.out.println() passes a string to OS verbatim, which considers that the string ends with a null character and only prints to this point

+2
source share

All Articles