aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Integers and out of range Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Integers and out of range" Watch "Integers and out of range" New topic
Author

Integers and out of range

Jeroen de Wolf
Greenhorn

Joined: Jul 31, 2003
Posts: 13
The book SCJP Study Guide from Kathy Sierra which is by the way an excelent book explains the principle of integer overflow by assigning a hexadecimal nr. 0XDEADCAFE to an int variable. The compiler accepts this and because its out of range, it rolls over to negative.
Subsequently I tried to assign the decimal representation of 0XDEADCAFE to the int variable (3735931646) but then the compiler complains about the variable being out of range.
In my humble opinion the value 0XDEADCAFE and 3735931646 are equal and differ only in representation. Why does the compiler treats them differently?
Kind regards,
Jeroen de Wolf
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
Hi, Jeroen -
An int is 32 bits, always signed, which means it can handle the range -2^31 to (2^31)-1. In decimal, the largest value is 2147483648. The number you selected, 0XDEADCAFE, is a negative number because the most significant bit is 1. Now, in C++, which supports and unsigned int, you could assign that value as a positive value 3735931646, but not in Java. Instead, you'd have to assign it to a long, which is 64 bits.
To assign OXDEADCAFE as a decimal number, you'd have to find its negative value through 2's complement conversion.
Hope that helps.
[ October 10, 2003: Message edited by: Jeff Bosch ]

Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
In other words:
0XDEADCAFE and 3735931646 are not equal. 0XDEADCAFE is a negative number.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787

The book SCJP Study Guide from Kathy Sierra which is by the way an excelent book explains the principle of integer overflow by assigning a hexadecimal nr. 0XDEADCAFE to an int variable. The compiler accepts this and because its out of range, it rolls over to negative.
Subsequently I tried to assign the decimal representation of 0XDEADCAFE to the int variable (3735931646) but then the compiler complains about the variable being out of range.
In my humble opinion the value 0XDEADCAFE and 3735931646 are equal and differ only in representation. Why does the compiler treats them differently?
Kind regards,

When you are assigning a hex number to an int, you are actually telling the compiler the bit values. That is:
0XDEADCAFE
is equal to 32 bits. They will be copied to int's 32 bits. And because MSB (32nd bit) is 1, the number is treated as negative: -559035650.
However, when you try to assign a decimal number (e.g. 3735931646) to an int, the compiler first has find it's 2's complement. It uses the 32nd bit (MSB) as sign bit which will be 0 in this case. The remaining 31 bits can not accomodate the give number.
Hope this makes sense.
Barkat
[ October 10, 2003: Message edited by: Barkat Mardhani ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Integers and out of range