• 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

Why final static int like this?

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was reading along in a well known Java book, and saw the following:
public static final int X = 0x80000000;
"public" anyone can see it
"static" only one per class
"final" you can't change it
"int" - it's an int
"X" - they really called it something meaningful
why not just use a plain old decimal number here?
Pres
 
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For no reason , they just wanted to show you how to use Hexideciman number, also this number is pretty big it maybe easier to write it as hex.
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Pres.
0x80000000 is hex, and it looks like it should be a huge positive number. But as an int, it evaluates to -2147483648: a huge negative number. (As a long, 0x80000000 evaluates to +2147483648.) I think the code writer here may be trying to demonstrate the insidious nature of "rollover": That is, when you overflow a limit of a data type's range, you start over at the other end of the range, without warning. As to why the programmer used hex, maybe because it's clearer this way that you are at the upper bound of the range of an int. 32 bits in an int, with a range from (-2**31 - 1) to (+2**31), and that int is representable as eight hex digits.
Just a guess, tho.
Art
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Art is on the right track. Since an int is 32 bits, even though 0x80000000 is a positive number, stuffing it into an int will put a 1 in the sign bit (the leftmost bit) (0x8 == 1000 (binary)). If you try to System.out.println() the value, you'll see a large negative number instead. Writing the value as hex just makes it easier to see that the 1 bit will go into the sign bit.
Junilu
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic