long literal out if range error

S

Scott Steiner

Hi,

I read that the primitive datatype "long" has the following range:

long, 64-bit, from -2^63 to +2^63-1
i.e from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Why am I getting then a range error in the following code:

long x = 1120833262000;
Date date = new Date(x);
System.out.println(date.toLocaleString());

I'm getting the error: "The literal 1120833262000 of type int is out of
range"

Thanks for any help.
 
J

Jacques-Olivier Haenni

Hi,

Try with the following:

long x = 1120833262000L;

Note the 'L' after the literal; it indicates to the compiler that the
literal must be considered as a long as not as an int (default).

Jacques-Olivier
 
S

Scott Steiner

Jacques-Olivier Haenni said:
Hi,

Try with the following:

long x = 1120833262000L;

Note the 'L' after the literal; it indicates to the compiler that the
literal must be considered as a long as not as an int (default).

thanks, it worked! that also explains why the error message was talking
about an int instead of long.
 
T

Thomas G. Marshall

Scott Steiner coughed up:
thanks, it worked! that also explains why the error message was
talking about an int instead of long.

You see a lot of people making similar mistakes. I believe that much of it
comes from C's notion that int and long are often the same size---int being
some datasize "natural" to the cpu, or words to that effect. In java, int
and long are specifically different sizes by fiat. Seems a little goofy to
me that they kept the primitive names the same.
 

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,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top