#Java unsigned types availability.
1 messages ยท Page 1 of 1 (latest)
not directly, no. the question would be why u think u need it
and based on ur response to that ull get proper better alternatives
maybe there's user input and i just don't want them to insert values outside my intended range
like, for example, ID
okay, u cant prevent that using unsigned types. also not in c++
oh yea fair
u never can prevent an user actually entering sth. u dont have control over their keyboard.
but u can ofc check what they entered, validate it and if wrong show an error and repeat
so this means if / switch statements are the only solution to validation?
yes, in java and also in c++ and anywhere else
got it
Like zabu said, unsigned types don't solve that
int max value is around 2 billions, so what if you use unsigned int with a size of 4 billions so even if the user enter 3 bilion, it still work. But then what if the user enters 5 billions ? Do you use long ? what if a user enter a number bigger than any fixed size int ? Do you use BigInteger ? What if the user enters a number so big it crashes your server ? Etc
So yea, validate inputs via ifs or catches
That said, java does have methods to handle unsigned ints, it's more annoying to use than unsigned types, but it's here
when reading raw memory you usually want to have it unsigned. signed byte only is kinda rubbish
i mean sure you do not read memory in java, but maybe you want to read raw bytes from a file
If you want an unsigned type they aren't in the language, but various unsigned types exist in libraries
But they are implemented by just " boxing up" the validation logic + operations
So in a sense they aren't as special as many would want
unsigned types in java, while annoying that they're missing are actually completely not required
You just need a small mental model on what operations you can and can't do straight up, and which ones you need a specfic function for
add subtract and multiply are all fine, divide toString and parsing isn't
there's an unsigned right shift that doesn't duplicate the sign bit
And when using the value make sure to do a toUnsignedXXX conversion that widens it
Other than that, it's really not that big of a difference, just a bit more code than it needs to be
I have a few of these ๐
At times I would like their convenience. And while I don't completely agree with some of the JDK team on the 'yeah but', there is some truth to the fact that having both signed and unsigned type does add a lot of "but what happens when I" combinations.
I don't miss them enough to have that much skin in the game - at least not on projects I've been most involved in the last decade.
Yeah, I've ported one of my libraries to C# and "I'll just use unsigned" turns into "regular ints are easier because otherwise I have to cast for array accesses and a bunch of other random crap"
I don't do anything with hardware, and rarely am I transforming binary formats where it matters.
While I think they probably should have been left in... It's not at all the thing I'd put any thought into if I was the one picking what to focus on to change now...
The exceptions topic that was posted recently is interesting.
Thanks for reminding me I have to watch that video ๐
Having encountered way too many libraries (in Java and .net) that don't document their exceptions, I really would like the ceremony and expressiveness barriers for more appropriate checked-exception use.
The compositional problem with checked exceptions is a pet topic of mine.
Can't even get my team to agree to when to use checked and unchecked exceptions