Search code examples
javaunsigned

What is the accepted way to handle unsigned data in java


I read (and am currently using this) that I can use a long whenever I need an unsigned int. This is working fine for me, but feels like a poor design. Is there a better, more accepted, way to do it? What about unsigned longs?


Solution

  • Since the only unsigned primitive type in Java is char, which has a very specific meaning to denote a unicode character, using long is the most idiomatic way to go.

    By default, JAXB maps xsd:unsignedInt to long, too. (source)

    Edit: If you know the top bit to be 0, or only use operations where the sign doesn't matter (for instance, you're just passing data through), int is probably more common. JAXB only defaults to long because it can't know how you intend to use the data.