In C++, I enjoyed having access to a 64 bit unsigned integer, via unsigned long long int, or via uint64_t. Now, in Java longs are 64 bits, I know. However, they are signed.

Is there an unsigned long (long) available as a Java primitive? How do I use it?

  • 54,432
  • 29
  • 203
  • 199
  • 6,301
  • 11
  • 37
  • 48
  • 5
    **NOTE** The accepted answer is outdated as of Java 8 and later. See the [Answer by GigaStore](http://stackoverflow.com/a/25248688/642706) for the new feature where you can ask Java to regard a number as unsigned. Not for everyday use, but handy when you need it. – Basil Bourque Sep 27 '15 at 04:17

10 Answers10


Starting Java 8, there is support for unsigned long (unsigned 64 bits). The way you can use it is:

Long l1 = Long.parseUnsignedLong("17916881237904312345");

To print it, you can not simply print l1, but you have to first:

String l1Str = Long.toUnsignedString(l1)


  • 2,420
  • 2
  • 17
  • 26
  • @j10, `Long ul1 = Long.parseUnsignedLong(objScannerInstance.next("\\d+"));` Not exactly elegant because it lacks a range check, but it would let you pull in long numeric inputs that would otherwise possible exceed the range of a signed long. _(Leverages the fact that `Scanner::next(...)` can also accept either a Pattern object or String pattern.)_ – Spencer D Jan 17 '18 at 03:41
  • it is difficult. – Alex78191 Dec 06 '18 at 07:00

I don't believe so. Once you want to go bigger than a signed long, I think BigInteger is the only (out of the box) way to go.

  • 6,899
  • 9
  • 48
  • 67
Sean Bright
  • 118,630
  • 17
  • 138
  • 146
  • 19
    This answer is a little outdated (it was posted 2009). Starting Java 8 (released March 2014), there is support for unsigned long. Check an example I posted below as an answer. – Amr Jan 19 '15 at 08:44

Nope, there is not. You'll have to use the primitive long data type and deal with signedness issues, or use a class such as BigInteger.

Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589

No, there isn't. The designers of Java are on record as saying they didn't like unsigned ints. Use a BigInteger instead. See this question for details.

  • 1
  • 1
Paul Tomblin
  • 179,021
  • 58
  • 319
  • 408
  • 23
    I respect Gosling for what he's done, but I think his defense of no unsigned ints is one of the dumbest excuses I've ever heard. :-) We've got waaaay more wonky things in Java than unsigned ints... :-) – Brian Knoblauch Feb 03 '09 at 20:16
  • 1
    Gosling at JavaPolis 2007 gave an example that confusingly doesn't work for unsigned ints. Josh Bloch pointed out it doesn't work for signed ints either. Arbitrary sized integers ftw! – Tom Hawtin - tackline Feb 03 '09 at 21:20
  • Brian Knoblauch - yep, its created vulnerabile code practice. See [Second "Master Key" Style APK Exploit Is Revealed Just Two Days After Original Goes Public, Already Patched By Google](http://www.androidpolice.com/2013/07/11/second-all-access-apk-exploit-is-revealed-just-two-days-after-master-key-goes-public-already-patched-by-google/). – jww Jul 14 '13 at 05:06
  • 2
    @PP.: I don't think it's possible to define sensible rules which allow free interaction between signed and unsigned types when at least one of them has defined wrapping behavior. That having been said, unsigned byte or unsigned short would have caused *zero* trouble since bytes don't interact with other types anyway. A bigger issue is having defined wrapping behavior for types that are used to represent *numbers*, as distinct from having separate wrapping types for those rare occasions (like hashcode calculations) when wrapping behavior is actually useful. – supercat Feb 25 '14 at 23:52
  • 3
    @PP.: I wish language designers would recognize the importance of distinguishing numbers from algebraic rings (what "wrapping integer" types are). Any size number should implicitly convert to any size ring, but rings should only convert to numbers via function or via explicit typecast to the *same size* number. The behavior of C, where unsigned types generally behave as algebraic rings but sometimes behave as numbers is probably the worst of all possible worlds; I can't fault Gosling for wanting to avoid that, though he took totally the wrong approach for doing so. – supercat Feb 25 '14 at 23:58

Java 8 provides a set of unsigned long operations that allows you to directly treat those Long variables as unsigned Long, here're some commonly used ones:

And additions, subtractions, and multiplications are the same for signed and unsigned longs.

  • 5,814
  • 7
  • 40
  • 79
  • 2
    A quick look in the source code tells me to be a bit cautious with these methods. When the longs are indeed negative (i.e. there is a difference from the signed case) the BigInteger class will be used. This means that upto 8 new BigIntegers will be allocated, this is quite a lot and definitely a performance drop. – Toonijn Sep 24 '16 at 18:57

For unsigned long you can use UnsignedLong class from Guava library:

It supports various operations:

  • plus
  • minus
  • times
  • mod
  • dividedBy

The thing that seems missing at the moment are byte shift operators. If you need those you can use BigInteger from Java.

  • 26,885
  • 12
  • 107
  • 96

Depending on the operations you intend to perform, the outcome is much the same, signed or unsigned. However, unless you are using trivial operations you will end up using BigInteger.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130

Java does not have unsigned types. As already mentioned, incure the overhead of BigInteger or use JNI to access native code.

  • 29,624
  • 9
  • 57
  • 79

The org.apache.axis.types package has a

UnsignedLong class.

for maven:

  • 2,565
  • 1
  • 25
  • 26

Seems like in Java 8 some methods are added to Long to treat old good [signed] long as unsigned. Seems like a workaround, but may help sometimes.