File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes A dumb question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A dumb question" Watch "A dumb question" New topic

A dumb question

Nagesh Vu

Joined: Mar 18, 2010
Posts: 7

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

Joined: Sep 28, 2004
Posts: 20517

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.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Matt Cartwright
Ranch Hand

Joined: Aug 25, 2008
Posts: 149

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.


For more details about integers, have a look here and here.
Hope that helps
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46337
Welcome to the Ranch

I see you have got an answer; it would be better if your thread title had a proper question in
I agree. Here's the link:
subject: A dumb question
It's not a secret anymore!