• 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

Conversion of Primitives

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there...
I have a silly question. Hope someone can help me out.
I know that a literal int value can be assigned to a narrower primitive type provided that the literal value falls within the legal range.
byte b = 1; //legal
short s =2; //legal
char = 3; //legal
What I want to know is what the legal range for byte,short and char? Does it have anything to do with the effective representation of bits for each type?
Thanks a lot.
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The legal ranges for a byte, int and char are as follows:
byte: [-128, 127] (8 bits)
int: [-32768, 32767] (32 bits)
char: [OxO, Oxffff] (16 bits)
For example, a byte has the range [-128, 127] because -2^7 is -128 and 2^7 -1 is 127. Count from zero until 7 and you have 8 bits.
If you look at an 8 bit number in binary you can also see the same thing:
1000 0000
There you see the largest 8 bit negative number. It's value is -2^7 + 0^6 + 0^5 + 0^4 + 0^3 + 0^2 + 0^1 = -128.
I hope that helps...

Originally posted by Karen Leoh:
Hi there...
I have a silly question. Hope someone can help me out.
I know that a literal int value can be assigned to a narrower primitive type provided that the literal value falls within the legal range.
byte b = 1; //legal
short s =2; //legal
char = 3; //legal
What I want to know is what the legal range for byte,short and char? Does it have anything to do with the effective representation of bits for each type?
Thanks a lot.


[ May 15, 2002: Message edited by: Ricardo Cortes ]
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Karen Leoh:
Does it have anything to do with the effective representation of bits for each type?


yes, it has everything to do with it.


So you see, in all cases except for char, the range is +/- 2 to the power of the Effective # Bits (less one for the sign bit). Also note that there is one more negative value in the range than the positive value.
Btw, all the wrapper classes like java.lang.Byte, have members named MAX_VALUE and MIN_VALUE, so if you want to know the range of say a short, you can say it is:
Short.MIN_VALUE - Short.MAX_VALUE
[ May 15, 2002: Message edited by: Rob Ross ]
 
Karen Leoh
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Ricardo and Rob,
Thanks so much. Your explanation helps me a lot.
 
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The char value should be as the above range, I believe.
Regards,
Clement
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you are correct.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic