• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

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
 
What a stench! Central nervous system shutting down. Save yourself tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic