File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why final static int like this?" Watch "Why final static int like this?" New topic
Author

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?
Pres
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.
Art
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

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


Junilu - [How to Ask Questions] [How to Answer Questions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why final static int like this?
 
Similar Threads
about constructor
Global final v/s Local final
final variable
final modifier in variable
operator presedence