• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

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
 
It's hard to fight evil. The little things, like a nice sandwich, really helps. Right tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic