Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning a value to long data type in java - defaulting to int

long val = 5000000000;

The error during this assignment is:

The literal 5000000000 of type int is out of range

Why does the compiler by default assume the literal to be type int when it is declared with type long?

like image 667
Aarish Ramesh Avatar asked Sep 12 '13 06:09

Aarish Ramesh


2 Answers

You can use:

long val = 5000000000L;

Check it here

like image 146
BobTheBuilder Avatar answered Oct 25 '22 12:10

BobTheBuilder


There is aspecific suffixes for long i.e L. If there is no suffix, then 5000000000 assumed to be an int type. And 5000000000 is out of int range, causing the erro. So you need to add L at the end of 5000000000 for it to be treated as a long value. Change your declaration from

long val = 5000000000;

to

long val = 5000000000L;
like image 37
Juned Ahsan Avatar answered Oct 25 '22 11:10

Juned Ahsan