#Java unsigned types availability.

1 messages ยท Page 1 of 1 (latest)

scarlet forgeBOT
#

@jagged vortex has a question:

bshmanair

does java have unsigned types like in c and c++

#

<@&987246399047479336> please have a look, thanks.

weary adder
#

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

jagged vortex
#

like, for example, ID

weary adder
#

okay, u cant prevent that using unsigned types. also not in c++

jagged vortex
#

oh yea fair

weary adder
#

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

jagged vortex
#

so this means if / switch statements are the only solution to validation?

weary adder
#

yes, in java and also in c++ and anywhere else

jagged vortex
#

got it

raven yoke
# jagged vortex maybe there's user input and i just don't want them to insert values outside my ...

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

bold remnant
#

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

strange furnace
#

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

bright hedge
#

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 ๐Ÿ˜„

verbal lake
bright hedge
#

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"

verbal lake
#

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.

bright hedge
#

Thanks for reminding me I have to watch that video ๐Ÿ™‚

verbal lake
#

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.

bright hedge
#

Can't even get my team to agree to when to use checked and unchecked exceptions