Doubt about error type of data and values in Java


I am learning how to program and I have the following exercise. I have to initialize different variables with the type of data that contains the smallest number of bits for said variable, at a certain value that they have given us. The fact is that I give failures two types long and I do not 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 do not understand in the first where I fail considering that a type "long" is the largest data type for a whole number. As for the second, the same thing happens to me but also I do not understand what about


"May split declaration into a declaration and assignment"

asked by Sergio 11.02.2018 в 20:16

1 answer


What happens is that Java the integer literals by default treats them as int and since the value you have represented is greater than the maximum value for a int the compiler throws that error. To solve this you must add the letter L , ex. long num = 5468546L , (does not equal uppercase or lowercase, what the lowercase is very similar to number 1 and can bring confusions when reading the code, for that reason it is recommended to use the capital letter). The same happens with literals of decimal numbers, which by default are treated as double so if you want to assign value to a variable of type float , using a literal, you must add the letter f behind, eg. float decimal = 1524.23f (same as in the case of long the letter can be uppercase or lowercase).

answered by 11.02.2018 / 20:59