The long primitive
suggest changeBy default, long is a 64-bit signed integer. It can store a minimum value of -2^63 and a maximum value of 2^63 - 1. Java 8 methods such as Long.compareUnsigned treat those same bits as an unsigned value from 0 through 2^64 - 1.
long example = -42;
long myLong = 284;
long anotherLong = 73;
//an "L" must be appended to the end of the number, because by default,
//numbers are assumed to be the int type. Appending an "L" makes it a long
//as 549755813888 (2 ^ 39) is larger than the maximum value of an int (2^31 - 1),
//"L" must be appended
long bigNumber = 549755813888L;
long addedLongs = myLong + anotherLong; // 284 + 73 = 357
long subtractedLongs = myLong - anotherLong; // 284 - 73 = 211
The maximum and minimum values of long can be found at:
long high = Long.MAX_VALUE; // high == 9223372036854775807L
long low = Long.MIN_VALUE; // low == -9223372036854775808L
The default value of a long field is 0L.
long defaultLong; // defaultLong == 0L
Note: letter āLā appended at the end of long literal is case insensitive, however it is good practice to use capital as it is easier to distinguish from digit one:
2L == 2l; // true
Warning: Java caches Long and Integer instances from the range -128 to 127. The reasoning is explained here: https://blogs.oracle.com/darcy/entry/boxing_and_caches_integer_valueof
The following results can be found:
Long val1 = 127L;
Long val2 = 127L;
System.out.println(val1 == val2); // true
Long val3 = 128L;
Long val4 = 128L;
System.out.println(val3 == val4); // false
To properly compare 2 Object Long values, use the following code(From Java 1.7 onward):
Long val3 = 128L;
Long val4 = 128L;
System.out.println(Objects.equals(val3, val4)); // true
Comparing a primitive long to an Object long will not result in a false negative like comparing 2 objects with == does.