I am trying to assign 4294967295 to a long. That is (2^32-1) java(netbeans) gives the following error message "integer number too large"
in fact I tried to figure out the largest number that an int can handle(did it manually by hand) and found that it is 2147483647 (of course as obvious it is 2^31-1)
But surprisingly I found out that even the long type cannot handle a number larger than that. Isn't there any difference between int and long?java doc says long is 64 bit
Am I missing something?
The problem is that you're using 4294967295 as an int
literal - but it's not a valid int
value. You want it to be a long
literal, so you need to put the L
suffix on it. This is fine:
long x = 4294967295L;
From JLS section 3.10.1:
An integer literal is of type long if it is suffixed with an ASCII letter L or l (ell); otherwise it is of type int (ยง4.2.1).
See more on this question at Stackoverflow