'long' integer of 19 digits will not compile

K

Keith Valentine

I am learning about integer types and I have found that when I try to
compile code that assigns the highest or lowest 'long' integers to a 'long'
type variable I receive the errors:

"integer number too large: -9223372036854775808"

Or

"integer number too large: 9223372036854775807"

These values contain 19 digits, however values containing a maximum of 9
digits will compile without a problem.

Does anyone know why this is?

Keith
 
A

Andrew Thompson

I am learning about integer types

The best group for those learning JAva is described here..
..and I have found that when I try to
compile code that assigns the highest or lowest 'long' integers to a 'long'
type variable I receive the errors:

"integer number too large: -9223372036854775808"

long l = 9223372036854775807l; // compiles just fine, try it
These values contain 19 digits, however values containing a maximum of 9
digits will compile without a problem.

Does anyone know why this is?

System.out.println(Long.MAX_VALUE);
System.out.println(Integer.MAX_VALUE);

HTH

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.lensescapes.com/ Images that escape the mundane
 
V

VisionSet

Keith Valentine said:
I am learning about integer types and I have found that when I try to
compile code that assigns the highest or lowest 'long' integers to a 'long'
type variable I receive the errors:

"integer number too large: -9223372036854775808"

Or

"integer number too large: 9223372036854775807"

These values contain 19 digits, however values containing a maximum of 9
digits will compile without a problem.

You are probably doing this:

long lng = 9223372036854775807;

which you can't do because the literal value 9223372036854775807 is being
taken as an integer which it obviously isn't.
You must do this:

long lng = 9223372036854775807L;

which tells the compiler you wish to have the literal treated as a long.
All literals are taken as an int unless you specify otherwise.

The error message gives you a clue:

"integer number too large" it has failed to recognise that you want it to be
a long.
 
T

Thomas G. Marshall

VisionSet coughed up:
You are probably doing this:

long lng = 9223372036854775807;

which you can't do because the literal value 9223372036854775807 is
being taken as an integer which it obviously isn't.
You must do this:

long lng = 9223372036854775807L;

which tells the compiler you wish to have the literal treated as a
long. All literals are taken as an int unless you specify otherwise.


Yes, always been that way.

But in the OP's defense, IMHO the jls really /should/ have allowed for
implicit type conversion when the immediate value is very large.
 
K

Keith Valentine

Thanks Andrew for your help.

Keith

VisionSet said:
You are probably doing this:

long lng = 9223372036854775807;

which you can't do because the literal value 9223372036854775807 is being
taken as an integer which it obviously isn't.
You must do this:

long lng = 9223372036854775807L;

which tells the compiler you wish to have the literal treated as a long.
All literals are taken as an int unless you specify otherwise.

The error message gives you a clue:

"integer number too large" it has failed to recognise that you want it to
be
a long.
 
K

Keith Valentine

Thanks for your help Mike.

Keith

VisionSet said:
You are probably doing this:

long lng = 9223372036854775807;

which you can't do because the literal value 9223372036854775807 is being
taken as an integer which it obviously isn't.
You must do this:

long lng = 9223372036854775807L;

which tells the compiler you wish to have the literal treated as a long.
All literals are taken as an int unless you specify otherwise.

The error message gives you a clue:

"integer number too large" it has failed to recognise that you want it to
be
a long.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top