Akron
Akron

Reputation: 1534

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?

Upvotes: 1

Views: 170

Answers (2)

meriton
meriton

Reputation: 70584

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.

Upvotes: 1

aleroot
aleroot

Reputation: 72676

Java doesn't have unsigned types by choice, you are using the signed types that are larger than the original unsigned type to work around the limitation but so you are using twice of the needed memory.

In java there is no concept of unsigned .

Upvotes: 1

Related Questions