Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strange behavior of long data type

Tags:

groovy

I have problem with the Long data type when I run this command:

Long nanos = 3 * 1000000000
println nanos

It prints out -1294967296, not 3000000000, and I dont know why.
I read on this page that Long is enough. So what is wrong ?

like image 814
hudi Avatar asked Dec 01 '22 00:12

hudi


2 Answers

You must mark your 2nd constant as long, otherwise it's considered an integer which overflows. Use:

Long nanos = 3 * 1000000000L
like image 99
xpapad Avatar answered Dec 05 '22 00:12

xpapad


You need to add a suffix to literals to imply that they are long values. Otherwise they are interpreted as int.

  Long nanos = 3 * 1000000000L;

More info about this here.

like image 30
Vincent Ramdhanie Avatar answered Dec 05 '22 00:12

Vincent Ramdhanie