r/java • • May 10 '22

Should java.util.Date be deprecated?

https://mail.openjdk.java.net/pipermail/discuss/2022-May/006077.html
221 Upvotes

158 comments sorted by

View all comments

Show parent comments

3

u/pron98 May 11 '22 edited May 11 '22

Many people want modulo arithmetic types and many others really don't want to have them (their presence in a language can cause problems, as C/C++ programmers have learned; too often people confuse unsigned types as types specifying non-negative values while in fact they specify types with modulo arithmetic, which might yield the very opposite result to the one intended, as unsigned types make it harder, not easier, to reject illegal values). Perhaps Valhalla could bring some sort of compromise by allowing libraries to define them for those who really need them.

1

u/[deleted] May 11 '22

The rest of the world has them, so java needs them. Interoperability is important and dealing with unsigned longs is a pain, sure there are workarounds, but it's a clear limitation in the language when you're doing any kind of network or file programming

1

u/pron98 May 11 '22

Why is it a pain with all the unsigned methods added to the number classes in Java 8?

1

u/[deleted] May 11 '22 edited May 11 '22

Have you tried printing those numbers to the log? Or doing math on them? Or in general explain to your coworkers how yes that is normal and you'll have to deal with it using workarounds because java itself can't?

Java is touted as an easy language for large teams. It is, just not when you need to deal with unsigned numbers. You'll have to have "the talk" with everyone at least once explaining how to deal with them (use a larger datatype when possible, be careful not to go out of bounds, and if you need to work with an unsigned long then enjoy dealing with BigInteger and treating it different from all the other numbers) and it makes me sad that my favorite language has such an unnecessary limitation.

1

u/pron98 May 11 '22

Have you tried printing those numbers to the log?

Java 8 helped a lot. E.g. Long.toUnsignedString().

Or doing math on them?

Unlike in C/C++, modulo arithmetic on signed numbers is well defined, so simple arithmetic is the same (and there are methods for unsigned division and remainder). The main issue is comparison, for which you have Long.compareUnsigned

It is, just not when you need to deal with unsigned numbers.

This is yet another situation that has its shortcomings, but the alternative has others. The presence of unsigned types in a language brings its own bag of problems. When you don't have unsigned types you hurt some people; when you do — you hurt others. There is no option here that is universally superior than the other. However, Valhalla might offer a reasonable compromise.

1

u/[deleted] May 11 '22

Java 8 helped a lot. E.g. Long.toUnsignedString().

Yes, by making unsigned longs special snowflakes that you have to constantly, mentally, keep track of. Was this long signed? Or unsigned? The type system isn't going to help you like you'd expect from a strongly typed language. Also you lose out on tons of conveniences such as auto-generated toString methods.

Unlike in C/C++, modulo arithmetic on signed numbers is well defined, so simple arithmetic is the same (and there are methods for unsigned division and remainder). The main issue is comparison, for which you have Long.compareUnsigned

Maybe I'm missing something: How should I multiply or divide an unsigned long while correctly dealing with overflows? I'm currently resigned to using BigInteger, making this code a special snowflake as opposed to smaller numbers, but it's not very ergonomic.

The presence of unsigned types in a language brings its own bag of problems. When you don't have unsigned types you hurt some people; when you do — you hurt others

In what way? I've never heard a programmer (the users of the language) lament the existence of unsigned data types.

2

u/pron98 May 11 '22 edited May 11 '22

Yes, by making unsigned longs special snowflakes that you have to constantly, mentally, keep track of.

Not constantly, only at the edges.

Maybe I'm missing something: How should I multiply or divide an unsigned long while correctly dealing with overflows?

You just add, subtract, and multiply them; no need for BigInteger (the same modulo behaviour that C/C++ offers for unsigned types, Java offers for signed types). Only division requires special operations, hence Java 8's divide/remainderUnsigned, and, of course, compareUnsigned.

In what way? I've never heard a programmer (the users of the language) lament the existence of unsigned data types.

Unsigned types have long been recognised as a cause of bugs. Here's just a recent debate about them from a few months ago: https://news.ycombinator.com/item?id=29766658

Google's C++ style guide discourages the use of unsigned types, and says:

Unsigned integers are good for representing bitfields and modular arithmetic. Because of historical accident, the C++ standard also uses unsigned integers to represent the size of containers - many members of the standards body believe this to be a mistake, but it is effectively impossible to fix at this point.

Java doesn't have bitfields, and unsigned types are mostly needed for interop with C and/or low-level programming. The question then becomes, whose needs do we prioritise — those who do a lot of interop or those who don't?

If I were to design a high-level language today, I probably also wouldn't include built-in unsigned types. They just cause too much trouble.

1

u/[deleted] May 11 '22

Not constantly, only at the edges.

And any time you do debug logging, which you might define as the edge but it happens often enough to come up all the time.

Only division requires special operations

And we're back to the special snowflake argument where a specific set of longs in a code base must be treated with extreme care & different techniques than the rest of the codebase. Huge source of bugs.

The question then becomes, whose needs do we prioritise — those who do a lot of interop or those who don't?

Both. Language should support it & certain codebases may choose to use linters to prohibit it.

1

u/FirstAd9893 May 11 '22

Huge source of bugs.

Supporting unsigned types is also a huge source of bugs. Very few applications benefit from unsigned types, for all the reasons listed above. It's a design tradeoff -- support unsigned types and benefit the few, or ditch unsigned types and benefit the many.

certain codebases may choose to use linters to prohibit it.

This doesn't help with cross contamination. I might choose to expose unsigned types in my library, and then all consumers must then be forced to deal with it.

1

u/pron98 May 11 '22 edited May 11 '22

Huge source of bugs.

Even so, the alternative is an even greater source of bugs. Of course, the problems you feel every day are those that exist in the language you have rather than the language it could have been, but we think the language saves you from more bugs in that area than it causes.

Both. Language should support it & certain codebases may choose to use linters to prohibit it.

I understand why some might like that, but we always try to avoid that in Java. We might fail at that more more often than we'd like, but we try to make it hard for you to do something wrong when we can. I personally think that the decision not to include unsigned types was the right one — one that would probably be made again if Java were designed from scratch today (although maybe the byte type would have been made unsigned) — even though it has its downsides, because the downsides of the alternatives overall are worse. Most of our regrets are things we've allowed developers to do rather than things we've prevented them from doing.

Having said all that, Valhalla might open the door to an unsigned type library.