File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

A dumb question

Nagesh Vu
Greenhorn

Joined: Mar 18, 2010
Posts: 7
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
Sheriff

Joined: Sep 28, 2004
Posts: 18715
    
  40


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


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.

Try:


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

Joined: Oct 13, 2005
Posts: 38357
    
  23
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: http://aspose.com/file-tools
 
subject: A dumb question