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 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark ""Long"s too short?" Watch ""Long"s too short?" New topic
Author

"Long"s too short?

Justin Porter
Ranch Hand

Joined: Nov 20, 2004
Posts: 34
Hey, I'm trying to use a number like this:

long n = 109275027171421;

and I believe the max value allowed in a long is 9223372036854775807. Can someone confirm this? I keep getting this error when trying to compile:

integer number too large: 109275027171421

Any help would be appreciated!


Want a free iPod, digital camera, or $300 cash? Completely legit offer at <a href="http://www.geocities.com/pricklymustard/freestuff.html" target="_blank" rel="nofollow">http://www.geocities.com/pricklymustard/freestuff.html</a> . I am looking for referrals which is why I need YOU!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Yes, the max value for a 32-bit int is (2^31)-1 = 2147483647, and the max value for a 64-bit long is (2^63)-1 = 9223372036854775807.

However, a literal integral (whole number) value is implicitly of type int. So, you'll need to explicitly cast this value to type long...

long n = (long)109275027171421;
[ December 01, 2004: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Justin Porter
Ranch Hand

Joined: Nov 20, 2004
Posts: 34
Thanks for the suggestion, but it didn't fix the problem. Even when casting it like this:

long n = (long)109275027171421;

it still came up with the same error....
Mark Patrick
Ranch Hand

Joined: Feb 22, 2004
Posts: 51
Justin,

As Marc stated, by default literal numbers are of type int, however, the literal you have in your code '109275027171421' is too big to fit into an int. It has to be able to be able to fit into an int before it can be casted to long. However, you can append 'L' to the end of the literal to tell the compiler that this number should be treated as a long instead of an int. Then it will compile.



Mark Patrick<br />SCJP 1.4
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Mark Patrick:
...you can append 'L' to the end of the literal to tell the compiler that this number should be treated as a long instead of an int...

Of course. The literal itself must be designated as a long. Casting won't work because you can't even get the int to cast from. (I must be getting tired. Sorry...)
[ December 01, 2004: Message edited by: marc weber ]
Justin Porter
Ranch Hand

Joined: Nov 20, 2004
Posts: 34
Many thanks! The second suggestion (appending L) worked!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: "Long"s too short?
 
Similar Threads
Strange Error messgae in tomcat server
Literal values doubt
is this a bug with Long.parseLong() code?
Fractions
Conversion from String to Byte