• Post Reply Bookmark Topic Watch Topic
  • New Topic

date type "Long" range too small?  RSS feed

 
Shawn Ma
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone, I have a very basic question, about the range of type "long". It seems on my machine, the type "long" is the same range as type "int", so when I try to give a very large number as expected by the method "setTime", it overflow and give wrong answer.

what should I do?
Thanks a lot.



+toSring(): Sat Feb 18 20:50:48 EST 2012
+getTime() 1329616248384
+toSring(): Wed Dec 31 19:00:00 EST 1969
+getTime() 0
+toSring(): Thu Jan 01 19:00:00 EST 1970
+getTime() 86400000
+toSring(): Fri Dec 12 01:57:12 EST 1969
+getTime() -1702967296
t= -1702967296
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like you assumed you were using long values here:

But you aren't. You're using int values, and that is where the overflow occurs. Not in the Date object. To tell the compiler you want to use long values in that calculation, specify one (or more) in the calculation to be long. Like this:
 
Shawn Ma
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:It looks like you assumed you were using long values here:

But you aren't. You're using int values, and that is where the overflow occurs. Not in the Date object. To tell the compiler you want to use long values in that calculation, specify one (or more) in the calculation to be long. Like this:


Thank you very much! It works as you said.
So is it that every time when I need to assign value to a long variable, I need to have a "L" attached to the end?
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.10.1
 
Stephan van Hulst
Saloon Keeper
Posts: 7991
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. If you want the result of an arithmetic operator to be a long, one of the operands has to be a long. So technically you only have to add the L suffix to the first operand.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shawn Ma wrote:
So is it that every time when I need to assign value to a long variable, I need to have a "L" attached to the end?


No.



In this case, as in your original code, the value on the RHS is an int expression. And in both cases, the value of that int expression gets automatically promoted to a long when it's used where a long was expected.

Your problem was that when you were doing 24*3600*1000*30, since all of the operands were ints, all intermediate results and the final result were also ints, so when the calculation values exceeded int's limits, the high-order bits were lost. After the calculation was done, the result was automatically promoted to long, but by that point, it was too late, as the high-order bits were already gone.

So it's not that you need to explicitly make something a long with L or by casting when you want to assign to a long variable. Rather, when you need an intermediate or final result of a calculation to be a long because it won't fit into an int, then you need to make at least one operand a long.

A similar issue applies with doubles
 
Shawn Ma
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the link! which is very helpful. now I understand where the problem is: I didn't use correct form of literals to evaluate.

Darryl Burke wrote:http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.10.1
 
Shawn Ma
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for your input!
But I am not sure about the bold lines in your reply.

for example, if I change it to:

I still got:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The literal 2592000000 of type int is out of range

at dateclasstest.main(dateclasstest.java:20)

It seems " the value of that int expression gets automatically promoted to a long when it's used where a long was expected. " is not working?


Jeff Verdegan wrote:
Shawn Ma wrote:
So is it that every time when I need to assign value to a long variable, I need to have a "L" attached to the end?


No.



In this case, as in your original code, the value on the RHS is an int expression. And in both cases, the value of that int expression gets automatically promoted to a long when it's used where a long was expected.



Your problem was that when you were doing 24*3600*1000*30, since all of the operands were ints, all intermediate results and the final result were also ints, so when the calculation values exceeded int's limits, the high-order bits were lost. After the calculation was done, the result was automatically promoted to long, but by that point, it was too late, as the high-order bits were already gone.

So it's not that you need to explicitly make something a long with L or by casting when you want to assign to a long variable. Rather, when you need an intermediate or final result of a calculation to be a long because it won't fit into an int, then you need to make at least one operand a long.

A similar issue applies with doubles
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shawn Ma wrote:Thanks a lot for your input!
But I am not sure about the bold lines in your reply.

for example, if I change it to:

I still got:
[color=red]Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The literal 2592000000 of type int is out of range


As already stated: Without an L, that literal is an int. That number is larger than Integer.MAX_VALUE, so it's not a valid int.

It seems " the value of that int expression gets automatically promoted to a long when it's used where a long was expected. " is not working?


No, that statement is accurate. However "where a long is expected" does not mean a literal that's too big for an int. It means where you're assigning to a long variable or passing a long parameter to a method.

Here, you have an int literal, but the number is too big for an int, so it's an error. It can't get promoted to a long until it's a valid int first.
 
Shawn Ma
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much! Now I totally understand.

Jeff Verdegan wrote:
Shawn Ma wrote:Thanks a lot for your input!
But I am not sure about the bold lines in your reply.

for example, if I change it to:

I still got:
[color=red]Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The literal 2592000000 of type int is out of range


As already stated: Without an L, that literal is an int. That number is larger than Integer.MAX_VALUE, so it's not a valid int.

It seems " the value of that int expression gets automatically promoted to a long when it's used where a long was expected. " is not working?


No, that statement is accurate. However "where a long is expected" does not mean a literal that's too big for an int. It means where you're assigning to a long variable or passing a long parameter to a method.

Here, you have an int literal, but the number is too big for an int, so it's an error. It can't get promoted to a long until it's a valid int first.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shawn Ma wrote:Thank you so much! Now I totally understand.


You're very welcome!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!