Originally posted by Kalyani Marathe:
Hi,
I have question related to char value.
char c = -0; // why no compiler error.
System.out.println(c);//it prints nothing
Thanks.
When you assign -0 (an int) to a char variable, the compiler casts it to the char represented by the
ASCII value of the int. (Well, the UNICODE value actually, but it's the same in this case.)
Really, all a char is is an unsigned, 16-bit integer anyway. "Behind the scenes", it's just a number being stored. In this case -0 is the same as 0, which is the ASCII value for "NULL". (Not a null reference--that's something different.)
So your println()
is printing exactly what you're telling it to. It's just that you're essentially telling it to print nothing.
- Jeff