I am learning to program. The thing is that I have this code and it gives me an error and I don't understand why:
long millisecondssince1970 = 1298332800000;
long worldpopulation = 6775235741;
In both lines Netbeans tells me that "integer number too large". The thing is that as far as I know the long type encompasses the value of this variable. What am I doing wrong? Thank you very much, regards :)
When you use literals in Java, certain types are assumed:
Typing assumes that 1298332800000
valor = 1298332800000;
is ainteger
. So, if you want it to be saved as a long, you have to add an L to the end, like so:If you write
valor = 123.05;
Java assume it is adouble
, never afloat
, you need to add an f:valor=123.05f;
If the transformation is from a lower "capacity" type to a higher capacity type, then no problem, it's considered a safe casting :
long var= 123; //works
123 is an integer, which fits in a long
But the other way around doesn't work:
In those cases the casting has to be explicit to make it clear to the compiler that you know what you are doing: