my dog learned polymorphism
The moose likes Beginning Java and the fly likes Why final static int like this? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why final static int like this?" Watch "Why final static int like this?" New topic

Why final static int like this?

Pres Brawner
Ranch Hand

Joined: Jan 18, 2001
Posts: 92
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?
Val Dra
Ranch Hand

Joined: Jan 26, 2001
Posts: 439
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.

Val SCJP <BR>going for SCJD
Art Metzer
Ranch Hand

Joined: Oct 31, 2000
Posts: 241
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.
Junilu Lacar

Joined: Feb 26, 2001
Posts: 6529

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 - [How to Ask Questions] [How to Answer Questions]
I agree. Here's the link:
subject: Why final static int like this?
It's not a secret anymore!