Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why a char default value is not displayed on console?

Tags:

java

I wanted to know what will it get displayed on console when you try to print a char default value. But unfortunately, I can't see anything on the console. why?

I should be able to see 0 right. As the default value of a char variable is a null character i.e., '\u0000' (Its decimal equivalent is 0) ? Correct me if I'm wrong.

Code:

public class Index {
    static char c;
    public static void main(String[] args) {
        System.out.println("char default value is: "+Index.c);
    }
}

Output console:

enter image description here

like image 661
coderpc Avatar asked Dec 04 '22 22:12

coderpc


1 Answers

There's a big difference between the character for "the decimal digit 0" which is U+0030... and U+0000, the null character, which is a control character and so has no printed output. You're printing out the latter, so I wouldn't expect to see 0.

To think about it another way, consider:

System.out.println("a\u0009b");

Would you expect that to print "a9b"? You shouldn't because U+0008 is the tab character, so you'd see something like "a b" with some variable number of spaces between depending on exactly what your output device does.

like image 60
Jon Skeet Avatar answered Dec 06 '22 11:12

Jon Skeet