Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

range of values

 
joel garner
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12098
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because a literal value is considered to be a long - and a long may not fit into a short
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve is right, and Fred is right about the not-fitting. There are two possible solutions:
 
joel garner
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for your valuable replies. My doubt is clarified.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic