jQuery in Action, 2nd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes short s = 0x8000 gives compiler error Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "short s = 0x8000 gives compiler error" Watch "short s = 0x8000 gives compiler error" New topic
Author

short s = 0x8000 gives compiler error

Mo Bustany
Greenhorn

Joined: Feb 03, 2003
Posts: 11
Page 150 of Sun Cert. by Sierra and Bates states that we get the cast from int to short for free.
short s2 = 0x7fff;
works but not this one:
short s1 = 0x8000;
Padmavathi Narayana
Greenhorn

Joined: Jun 16, 2003
Posts: 12
Hi Mo Bustany,
I think the highest order bit is used as sign bit. So if the number is greater than 0x7fff and assigned to short if give give a loss of precision compilation error.
Regards,
Padma
Darren McLeod
Greenhorn

Joined: Jul 07, 2003
Posts: 19
0x8000 fails because it is larger than the maximum value allowed for a short (2^15 - 1): 0x8000 = 32768 which is larger than 32767.


SCJP, SCBCD
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Don't forget that this literal 0x8000 is type int.
Since it is type int, it has 32 bits. It looks like this,
0000 0000 0000 0000 1000 0000 0000 0000
Maybe this makes it more obvious that the value is decimal 32768.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: short s = 0x8000 gives compiler error
 
Similar Threads
NX: Flag help
NX: 0x8000?
magic cookie and 0x800 flag doubt
Magic Cookie checking and deleted flag
confusion on casting