I am learning to program and I have the following exercise. I have to initialize different variables with the data type that contains the least number of bits for said variable, to a certain value that they have given us. The thing is that I get two types of errors long
and I don't know why:
long milisegundosdesde1970 = 1298332800000;
long poblacionmundial = 6775235741;
In the first Netbeans tells me
"integer number too large" and in the second "integer number too large. May split declaration into a declaration and assignment".
I don't understand where I am wrong in the first one, considering that a "long" type is the largest data type for an integer. As for the second, the same thing happens to me but I also don't understand what
"May split declaration into a declaration and assignment"
What happens is that Java treats integer literals by default as
int
and as the value you have represented is greater than the maximum value for aint
the compiler throws that error. To solve this you must add the letterL
, eg.long num = 5468546L
, (it doesn't matter if it's uppercase or lowercase, since the lowercase is very similar to the number 1 and can cause confusion when reading the code, that's why it's recommended to use the uppercase). The same goes for decimal number literals, which by default are treated asdouble
so if you want to assign a value to a variable of typefloat
, using a literal, you must add the letterf
after it, eg.float decimal = 1524.23f
(as in the case oflong
the letter can be uppercase or lowercase).