In Java 8, unsigned long support was introduced. Still, these are typical longs, but the sign doesn't affect adding and subtracting. For dividing and comparing, you have dedicated methods in Long. Also, you can do the following:

long l1 = Long.parseUnsignedLong("12345678901234567890");
String l1Str = Long.toUnsignedString(l1)

BigInteger is a bit different. It can keep huge numbers. It stores them as int[] and supports arithmetic.

Answer from xenteros on Stack Overflow
Top answer
1 of 3
35

In Java 8, unsigned long support was introduced. Still, these are typical longs, but the sign doesn't affect adding and subtracting. For dividing and comparing, you have dedicated methods in Long. Also, you can do the following:

long l1 = Long.parseUnsignedLong("12345678901234567890");
String l1Str = Long.toUnsignedString(l1)

BigInteger is a bit different. It can keep huge numbers. It stores them as int[] and supports arithmetic.

2 of 3
18

Although Java has no unsigned long type, you can treat signed 64-bit two's-complement integers (i.e. long values) as unsigned if you are careful about it.

Many primitive integer operations are sign agnostic for two's-complement representations. For example, you can use Java primitive addition, subtraction and multiplication on an unsigned number represented as a long, and get the "right" answer.

For other operations such as division and comparison, the Long class provides method like divideUnsigned and compareUnsigned that will give the correct results for unsigned numbers represented as long values.

The Long methods supporting unsigned operations were added in Java 8. Prior to that, you could use 3rd-party libraries to achieve the same effect. For example, the static methods in the Guava UnsignedLongs class.


Is BigInteger the signed long that Java supports?

BigInteger would be another way to represent integer values greater that Long.MAX_VALUE. But BigInteger is a heavy-weight class. It is unnecessary if your numbers all fall within the range 0 to 264 - 1 (inclusive).

🌐
Programming.Guide
programming.guide › java › unsigned-long.html
Unsigned long in Java | Programming.Guide
BigInteger bigInteger = BigInteger.valueOf(Long.MAX_VALUE) .add(BigInteger.valueOf(24193)); long lng = bigInteger.longValue(); System.out.println(Long.toUnsignedString(lng)); // 9223372036854800000 ... /** * Return a BigInteger equal to the unsigned value of the argument.
🌐
Oracle
docs.oracle.com › javase › tutorial › java › nutsandbolts › datatypes.html
Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)
The signed long has a minimum value of -263 and a maximum value of 263-1. In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 264-1. Use this data type when you need a range of values wider than those ...
Top answer
1 of 3
21

Update – Java 8 can treat signed int & long as if unsigned

In Java, the primitive integer data types (byte, short, int, and long) are signed (positive or negative).

As of Java 8 both int and long can be treated explicitly as if they are unsigned. Officially a feature now, but kind of a hack nonetheless. Some may find it useful in certain limited circumstances. See the Java Tutorial.

int: By default, the int data type is a 32-bit signed two's complement integer, which has a minimum value of -2³¹ and a maximum value of 2³¹-1. In Java SE 8 and later, you can use the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 2³²-1. Use the Integer class to use int data type as an unsigned integer. See the section The Number Classes for more information. Static methods like compareUnsigned, divideUnsigned etc have been added to the Integer class to support the arithmetic operations for unsigned integers.

long: The long data type is a 64-bit two's complement integer. The signed long has a minimum value of -2⁶³ and a maximum value of 2⁶³-1. In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 2⁶⁴-1. The unsigned long has a minimum value of 0 and maximum value of 2⁶⁴-1. Use this data type when you need a range of values wider than those provided by int. The Long class also contains methods like compareUnsigned, divideUnsigned etc to support arithmetic operations for unsigned long.

I am not necessarily recommending this approach. I’m merely making you aware of the option.

2 of 3
11

Short answer, there's no unsigned data type in java. long in C is 32-bit on 32-bit systems, but java's long is 64-bit, so you can use that for replacement (at least it would solve the overflow problem). If you need even wider integers, use BigInteger class.

🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Long.html
Long (Java Platform SE 8 )
October 20, 2025 - Any character of the string is not a digit of the specified radix, except that the first character may be a plus sign '+' ('\u002B') provided that the string is longer than length 1. The value represented by the string is larger than the largest unsigned long, 264-1.
🌐
Restack
restack.io › p › java-knowledge-unsigned-long-max-value
Java Unsigned Long Max Value | Restackio
In Java, the unsigned long data ... limits of the standard long type. The maximum value for a signed long in Java is 9223372036854775807, which is 2^63 - 1....
🌐
Guava
guava.dev › releases › 20.0 › api › docs › com › google › common › primitives › UnsignedLong.html
UnsignedLong (Guava: Google Core Libraries for Java 20.0 API)
Returns the value of this UnsignedLong as a double, analogous to a widening primitive conversion from long to double, and correctly rounded. ... Returns the value of this UnsignedLong as a BigInteger. ... Returns a string representation of the UnsignedLong value, in base 10. ... Returns a string representation of the UnsignedLong value, in base radix. If radix < Character.MIN_RADIX or radix > Character.MAX_RADIX, the radix 10 is used.
🌐
Quora
quora.com › How-come-the-maximum-value-of-int-is-2-147-483-647-in-Java
How come the maximum value of int is 2,147,483,647 in Java? - Quora
When you have 32 bits all of them are ones, you have 2^{32}-1 which equals 4294967295. So this is the maximum number that an unsigned int can hold. Note that the minimum number an unsigned int...
Find elsewhere
🌐
LabEx
labex.io › tutorials › java-how-to-handle-exceptions-when-parsing-an-unsigned-long-in-java-414050
How to handle exceptions when parsing an unsigned long in Java | LabEx
Before attempting to parse an unsigned long value, it's a good practice to validate the input data to ensure it is within the valid range. You can use the Long.MAX_VALUE and 0L constants to check the input value:
🌐
Delft Stack
delftstack.com › home › howto › java › long max value in java
Long.MAX_VALUE in Java | Delft Stack
October 30, 2023 - Then, we attempt to add 1 to a. Before performing the addition, we check if the result is less than the original value of a or greater than Long.MAX_VALUE. If this condition is true, it indicates that an overflow has occurred. Otherwise, the addition is within the valid range for long values and no overflow has occurred. ... Overflow detected! In Java, attempting to add 1 to the Long.MAX_VALUE constant will indeed result in overflow, causing the value to wrap around to the lowest possible long value, which is -9223372036854775808.
🌐
Marekhudyma
marekhudyma.com › java › 2019 › 05 › 01 › java-unsigned-integer.html
Java unsigned integer
For many years of development in Java, I rarely had the need to use unsigned integers, from a technical point of view. If we lose 1 bit from 32, it is not a big deal. If the Integer is too small, we can use Long with Long.MAX_VALUE is equal to nine quintillions (9223372036854775807).
🌐
Elastic
elastic.co › elastic docs › reference › elasticsearch › mapping › field data types › unsigned long field type
Unsigned long field type | Reference
By default, script values of an unsigned_long field are returned as Java signed Long, which means that values that are greater than Long.MAX_VALUE are shown as negative values.
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › lang › Long.html
Long (Java SE 17 & JDK 17)
January 20, 2026 - Any character of the string is not a digit of the specified radix, except that the first character may be a plus sign '+' ('\u002B') provided that the string is longer than length 1. The value represented by the string is larger than the largest unsigned long, 264-1.
🌐
Processing
processing.org › reference › long.html
long / Reference / Processing.org
When assigning literal values that are larger than this magnitude, it is necessary to also append the qualifier "L" to the number, as shown in the example above. Processing functions don't use this datatype, so while they work in the language, you'll usually have to convert to a int using the (int) syntax before passing into a function. ... long a; // Declare variable 'a' of type long and assign a large value: //a = 2147483648; // Error: The literal of type int is out of range a = 2147483648L; // Instead, add an "L" to the number to mark it as a long long b = -256; // Declare variable 'b' and assign it the value -256 long c = a + b; // Declare variable 'c' and assign it the sum of 'a' and 'b' int i = (int)c; // Converts the value of 'c' from a long to an int
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › lang › Long.html
Long (Java SE 11 & JDK 11 )
January 20, 2026 - Any character of the string is not a digit of the specified radix, except that the first character may be a plus sign '+' ('\u002B') provided that the string is longer than length 1. The value represented by the string is larger than the largest unsigned long, 264-1.
🌐
Guava
guava.dev › releases › 31.0-jre › api › docs › com › google › common › primitives › UnsignedLong.html
UnsignedLong (Guava: Google Core Libraries for Java 31.0-jre API)
Returns a string representation of the UnsignedLong value, in base radix. If radix < Character.MIN_RADIX or radix > Character.MAX_RADIX, the radix 10 is used.
🌐
GitHub
github.com › OPCFoundation › UA-Java-Legacy › blob › master › src › main › java › org › opcfoundation › ua › builtintypes › UnsignedShort.java
UA-Java-Legacy/src/main/java/org/opcfoundation/ua/builtintypes/UnsignedShort.java at master · OPCFoundation/UA-Java-Legacy
private static final long ... · /** Constant <code>L_MAX_VALUE=0xFFFFL</code> */ public static final long L_MAX_VALUE = 0xFFFFL; /** Constant <code>L_MIN_VALUE=0L</code> */ public static final long L_MIN_VALUE ...
Author   OPCFoundation