This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!See this thread for details.
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of Refactoring for Software Design Smells: Managing Technical Debt this week in the OO, Patterns, UML and Refactoring forum!

# 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:

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: