# Integers and out of range

Jeroen de Wolf
Greenhorn
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
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 ]

Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
In other words:

Barkat Mardhani
Ranch Hand
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: