Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login

# conversion from Integer.MAX_VALUE to short

Charu Murali
Ranch Hand

Joined: Jul 12, 2002
Posts: 37
The following qn comes in Dan's mock exam.

Integer.MIN_VALUE is -2 to the power of 31. How it can be converted to short. I don't understand the explanation given with the answer.

Thanks
Charu
Khurram Shahood(SCJP2)
Greenhorn

Joined: Jun 08, 2002
Posts: 18
hi
wrapper classes have immutable objects u cant cast them.

khurram shahood(Java Farmer)
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by Charu Murali:
The following qn comes in Dan's mock exam.

Integer.MIN_VALUE is -2 to the power of 31. How it can be converted to short. I don't understand the explanation given with the answer.

Thanks
Charu

Integer.MIN_VALUE is represented as a binary two's compliment value as follows.
10000000 00000000 00000000 00000000
When the above is cast to a short the 16 most significant bits are thrown away. The result is the following 16 bit value.
00000000 00000000
Integer.MAX_VALUE is represented in binary as follows.
01111111 11111111 11111111 11111111
When the above is cast to a short the 16 most significant bits are thrown away. The result is the following.
11111111 11111111
If the above is interpreted as a 16 bit two's compliment value, then it is -1. However, it is interpreted as a 16 bit unsigned value then it is 65535.

Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by Khurram Shahood(SCJP2):
hi
wrapper classes have immutable objects u cant cast them.

Khurram,
The wrapper classes are indeed immutable, but Integer.MAX_VALUE is not a wrapper object. Instead, it is a public field within an Integer object. In fact, it is declared as follows.
public static final int MAX_VALUE
There is no problem casting MAX_VALUE to a short.
A wrapper object such as Integer may be cast to type java.lang.Object or java.lang.Number.
Charu Murali
Ranch Hand

Joined: Jul 12, 2002
Posts: 37

11111111 11111111
If the above is interpreted as a 16 bit two's compliment value, then it is -1. However, it is interpreted as a 16 bit unsigned value then it is 65535.

Dan, if 11111111 11111111 is converted to 2's complement value we get 00000000 00000001. Can you explain me how we get 65535 here? I think these questions are very very basic. Please bear with me.
Charu
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
if 11111111 11111111 is converted to 2's complement value we get 00000000 00000001

Dan did not say "converted" he said "interpreted".
So 11111111 11111111 in a signed int variable is the equivalent to our concept of minus one.
11111111 11111111 in an (implicitly unsigned) char variable is equivalent to 2^16-1 = 65535.
This is basic computer science stuff and common to many programming languages, so
take a look at this (PDF) it may help
-Barry
[ August 10, 2002: Message edited by: Barry Gaunt ]