• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

char

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have question related to char value.

char c = -0; // why no compiler error.
System.out.println(c);//it prints nothing

Thanks.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
No thanks. We have all the government we need. This tiny ad would like you to leave now:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic