programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Why final static int like this?

Pres Brawner
Ranch Hand
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?
Pres

Val Dra
Ranch Hand
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.

Art Metzer
Ranch Hand
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.
Art

Junilu Lacar
Sheriff
Posts: 11165
160
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