aspose file tools*
The moose likes Beginning Java and the fly likes range of values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "range of values " Watch "range of values " New topic
Author

range of values

joel garner
Greenhorn

Joined: Dec 28, 2008
Posts: 9
The sun documentation says that short data type has a range from
It has a minimum value of -32,768 and a maximum value of 32,767

The self test question in K&B book had this code


this code throws an error saying that 7 should be converted to short.
my question is when short has a value range from -32,768 to 32,767, then why is 7 not considered to be a short value?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11475
    
  16

because a literal value is considered to be a long - and a long may not fit into a short


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

fred rosenberger wrote:because a literal value is considered to be a long - and a long may not fit into a short


I guess you meant literals are considered int (unless appended with 'L' or 'l').

JLS: Integer Constants


Steve
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19759
    
  20

Steve is right, and Fred is right about the not-fitting. There are two possible solutions:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
joel garner
Greenhorn

Joined: Dec 28, 2008
Posts: 9
Thank you all for your valuable replies. My doubt is clarified.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: range of values