Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A dumb question

 
Nagesh Vu
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am a beginner and I typed my first code. This the first piece:

public class ShortEg {

public static void main (String args[]){

short value = 35600;
System.out.println("The value is " + value);
}
}

I get the following error:
possible loss of precision
found: int
required: short
short value: 35600

I changed the value to 35000000000.
short value = 35000000000;

I get the following error.
integer number too large: 35000000000
short value = 35000000000

Can anyone help me understand why the difference in the error messages?

Thank you. Have a nice day.
 
Henry Wong
author
Marshal
Pie
Posts: 20892
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In Java, a short has a maximum value of 32767. This is smaller than the integer literal that you are trying to assign to it. Hence, in the first case, it is complaining about a lost of precision, as the value will overflow, when converted to a short.

In Java, a literal value (ie. a number) is considered to be an integer. And an integer has a maximum value of 2147483647. Hence, in the second case, it is complaining about the integer literal as not being valid.

Henry
 
Matt Cartwright
Ranch Hand
Posts: 152
Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nagesh, welcome to the Ranch.

pleaseuse code tags when posting code.

as Java does not support the unsigned type specifier for integer data,
your value for short is out of range.

Try:


For more details about integers, have a look here and here.
Hope that helps
Matt
 
Campbell Ritchie
Sheriff
Posts: 48402
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I see you have got an answer; it would be better if your thread title had a proper question in
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic