• 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 size and usage

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am able to use char c=(char)2147483647;
where 2147483647 is the max size of an int.
anything more than that throws an compile time error.
however the size of a char is 2 bytes and its unsigned.so the maximum int value is around 65535.
how is char variables able to hold values from 65535 to 2147483647 which exceeds its size?
the same is not applicable to int which is of size 32 bits and cant hold a value greater than
2147483647 inspite of casting int x=(int)2147483647.

Simon
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
char can't hold a value greater than 65535. If you cast a value greater than that to a char, the high-order bits are truncated -- i.e.,

c is assigned the value 0 (65536 & 0xFFFF is 0.)
The reason you get a compiler error with numbers larger than Integer.MAX_VALUE is that to write a larger integral literal in Java, it must be a long -- you have to write 2147483648L. But you can write
and as it turns out, c will be 0 here as well (all the low-order bits of this number are 0.)
[ July 18, 2003: Message edited by: Ernest Friedman-Hill ]
 
Eric Lidell
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ernest,
Thank for the answer.
reply
    Bookmark Topic Watch Topic
  • New Topic