wood burning stoves*
The moose likes Beginning Java and the fly likes 'long' with more than 10 digits won't compile Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark " Watch " New topic

'long' with more than 10 digits won't compile

Nick Stevens

Joined: Nov 13, 2004
Posts: 15
I'm working on an anagram solver and I've found that

int 1234567890;

compiles okay (as does long 1234567890);

long 12345678901;

generates the error ': integer number too large 12345678901'. I thought 'long' variables could consist of more digits than 'int' variables? Any suggestions would be much appreciated!
Joanne Neal

Joined: Aug 05, 2005
Posts: 3432
Put an 'L' on the end to indicate it is a long

long l = 12345678901L;

bart zagers
Ranch Hand

Joined: Feb 05, 2003
Posts: 234
When you write

The compiler handles the number as an int and therefore this gives a compiler error. (All literals like that are considered int's by default)

To make the compiler handle this as a long, you have to append a 'l' or a 'L' to the number.

Now the compiler will know you really meant a long and not complain.
Nick Stevens

Joined: Nov 13, 2004
Posts: 15
Thanks both of you for your help, I've added an l and it compiles okay - hurray! I wonder why the compiler assumes a long 'literal' is an int? I'll be unscrambling anagrams again now :-)
I agree. Here's the link: http://aspose.com/file-tools
subject: 'long' with more than 10 digits won't compile
Similar Threads
JavaRanch style: Avoiding use of break /return in the middle of a method
float behavior
converting float hex to int
why are the answers different?
Changes in the out put