Tan Quang wrote:. . . if indeed java supports unsigned data types like other programming languages . . .
It doesn't. That has been a point of controversy for twenty‑five years, and I can't envisage unsigned ever being introduced.
Go through theIntegerandLongclasses and look for anything with unsignedXXX in its name. If that doesn't help, you may have to convert your int unsigneds into longs, and your bigint unsigneds intoBigIntegers.
[Additional] We had a bit of a discussion about unsigned integer types recently: here.
Jesse Silverman wrote:Java still does not allow you to declare the type of a primitive as unsigned, nor are there UnsignedInteger and associated other wrapper types defined.
There are a great number of methods (far too many to list here, in fact), in both the Integer and Long classes that have been added somewhat recently, mostly in Java 8 and 9 that make working with unsigned values much easier, please refer:
So you can't declare a Data Type to be unsigned, but for operations, you sure can specify to treat the operands and results as unsigned.
I spent 11+ years working on a giant multi-lingual product (really more of a platform, actually) that had the same values represented in C/C++, SQL, C# and Java.
There was more than a little pain around this (okay, sometimes a LOT) and these methods that are now part of the standard wrapper classes could probably have helped us some.
The lack of there being a way to actually declare a Java VALUE unsigned still would cause us a lot of headaches even today, and I think we would still try to avoid exposing unsigned types to user api's. Sadly, because we were working with many native and .Net api's, that was not always possible. Hilarity sometimes ensued.
I will add that actually needing the full RANGE of unsigned long is spectacularly uncommon. I feel like 100% of the use cases I would be using the methods found in the provided links would be for compatibility with non-Java API's, but I may be forgetting something.
When working in Pure Java (a luxury I rarely have had) I would generally avoid unsigned types wherever possible, it is not a good state of affairs to be having values that need to be reminded that they are indeed unsigned every time one operates on or compares them.
Thank you, it's thanks to you that I came across something called Pure Java. I hope to work with it at least once in my life before switching to another programming language.
As for this topic, I don't think there are any perfect answers anymore, so I think it's over.
Many thanks for the helpful answer.
I would much prefer to throw an exception than print error messages and still return an incorrect value. I don't think line 8 will ever be reached.
I would also prefer never to see floats again . And nobody seems to have taken the possibility of a NaN being passed into consideration.
This all shows some of the drawbacks of binary floating‑point arithmetic.
Lots of interesting and important things got mentioned in it, but also a lot of silly ones that lost the spirit of the original webpage the poster was working from.
The last note before I forget is that "Pure Java" referred not to any special version of the language, but rather to Java running only Java, without interacting with API's using unsigned integer quantities in them because they were written in C/C++, FORTRAN, Go or what have you. I rarely got to work with "Pure Java" because we were always interacting with API's written in one or more of those other languages.
As described, unsigned data coming from those was awkward and clunky to work with in Java, a little less so now.
Anyway, a lot of good stuff was discussed in here, hopefully future readers will focus on that stuff and not the bits that got plain silly.
RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)