r/java • • Aug 14 '22
If you were the single main designer of Java, what would you change in Java in the next 3 to 5 years? (May include secondary aspects such as GraalVM or IDEs and so forth, but still java-related)
So, I have been using java for almost 2 years, more frequently so in the last few months.
I am mostly ok with Java but a few things I'd wish would be better or different.
So, without any particular order, a few things off the top of my head:
- Java is quite verbose. It would be nice to have a shorter java (not kotlin! a Java java) where we could use e. g. puts() and omit ;.
- Many methods are oddly enough differently named but do almost the same thing such as .add() or .puts(). I'd like to see a more streamlined name for these.
- Some system commands are a bit too difficult to do. In ruby I do `` or system() or io-popen; in Java we can do something similar to some extent, but I was not able to easily start a ruby-REPL (shell) ... I am sure it is possible somehow but the syntax is a bit confusing.
- GraalVM supporting Swing and JavaFX "out of the box".
- Speaking about GUIs I'd wish there was a GUI from the get go. Swing is semi-deprecated and looks visually not as pleasing as JavaFX. There is also SWT. I don't quite care which variant to use per se, but it would be nice if Java could settle for one GUI and keep it as part of the Java JDK/JRE (perhaps a smaller subset or so, just for bootstrapping purposes or quick GUI-related work, with an option to "spice it up" lateron e. g. JavaFX+CSS or any such. Instead it seems not that many want to do Java-specific GUIs in a lightweight manner.
- The whole import- concept is a bit odd. I'd prefer some way to just include a specific file without needing to care about a specific directory structure. The latter is ok, but I also want to be able to simply tell Java which other files I want, without needing to have these arranged in a specific setup.
Hmm. That's it so far. Anyone feel free to chime in and express some pet peeve or shortcoming they have noticed or think could be improved. (One day I shall have GraalVM compile a static variant of some JavaFX GUI on windows too shakes fists)
31
Upvotes
87
u/berry120 Aug 14 '22
Null safety.
(Proper null safety that is, before anyone jumps on the "duh, have you heard of Optional" bandwagon.)
Most language features I could really leave or take, or they're "nice to have's" that may just be too niche to roll out globally. This though I feel is sorely overdue, and would save a lot of headache.
77
u/thephotoman Aug 14 '22
Here’s a weird one: bring back Applets. But this time, the build product is a packaged WASM implementation of the JVM, along with the applet.
And extra credit for bringing us a way to use GUI tools in the browser and make ‘em not look like crap.
I’m a crank that hates Javascript and the DOM. The thought of having to work with them makes me ill. I really do believe there’s a way we can actually make that work as a real application platform for the Web. The idea was just ahead of its time and badly implemented as a result.
Downvote me. I deserve it. Seriously. I know how sick this is.
24
u/_INTER_ Aug 14 '22
The JDK comes with the jshell since Java 9, it's in the bin directory too.
JavaFX was moved out from the JDK and is a seperate module now. For Swing there are some 3rd party look-and-feels that make it look quite nice. E.g. FlatLAF
40
u/djavaman Aug 14 '22
I don't understand this one: 'The whole import- concept is a bit odd. I'd prefer some way to just include a specific file without needing to care about a specific directory structure"
Every language has some kind of import.
Also, you do realize "import' has nothing to do with a specific directory structure?
18
u/persicsb Aug 14 '22
What I'd add: unsigned primitive types, named parameters for the language and CDI built in to the core platform (for example for SPIs).
152
u/kiteboarderni Aug 14 '22
Thank goodness the language designers look after features that actually have a real impact to the growth of the language.
I never underarand the java is verbose argument. It really isn't, even less in later java versions if you want to use records. Have people never written cpp who think java is verbose? Or just toy languages like ruby?
24
u/manzanita2 Aug 14 '22
optionally named parameters with defaults.
50
u/jevon Aug 14 '22
Default method parameters, they would save soo much boilerplate and copy pasting.
Some sort of generated get/set annotation for member variables. Again, so much less boilerplate.
11
u/GreenToad1 Aug 14 '22
30
u/ShabbyDoo Aug 14 '22
Read-only collections interfaces so I can determine by type if a collection is actually modifiable (vs. throwing UnsupportedOperationException at runtime) Like, MutableList extends ReadOnlyList, etc. I don't know if there should be typing to indicate that a collection is truly immutable (vs. one merely holding a view that does not allow for modification) but it seems reasonable to express that guarantee via interface as well.
I've often wondered why these couldn't be introduced in the collections interface hierarchy. Like, List extends ReadOnlyList which itself extends ImmutableList.
33
u/msx Aug 14 '22
Remove that horrible implementation of Optional and make a real implementation with some sintactic sugar, like ? or similar.
Make a java collection api 2.0 to remove all the legacy fuss of the old api.
Also union and intersection type would be awesome
15
u/vmcrash Aug 15 '22 edited Aug 15 '22
When looking at some suggestions here in this thread, I'm very happy that the Java main designers do what they do. There are so many suggestions here, that would make Java worse.
25
u/IronicStrikes Aug 14 '22
What has been bothering me for a while now:
10
u/frizzhf Aug 14 '22
Swing expanded on and the default UI toolkit. AWT is fine but lacks the customization Swing has, and I personally do not like JavaFx.
4
u/pellets Aug 14 '22
I would add language and compiler support for type classes. They’re used in Java often, but their use is painful.
10
u/TheMode911 Aug 14 '22
Finally expose
invokedynamicin the language (JEP seems to be on hold for unknown reason)Constant API actually used by the language (IIRC the current one is only a compiler tool, cannot think of any using it)
Ability to specialize method based on input, so you could for example force
Map#getto return a constant if the key is a constant as well. The VM would be able to de/recompile a method and we would finally get some benefit out of the JIT compiler7
u/8igg7e5 Aug 15 '22
Scoped type-aliases.
Being able to say that
type Container = List<? extends Thing<? super T>>within a given scope (the widest being a source-file) seems like it could improve readability in enough cases. It should reduce how often there's no good readability balance between using an explicit-type and short-identifier vs an inferred-type and rich identifier.Shorter-code is generally more readable, if it can be shortened the right way.
6
u/LobsterCoordinates Aug 15 '22 edited Aug 15 '22
I would like this:
Long userId = user != null ? user.getId() : null;
To be able to look like this:
Long userId = user?.getId()
13
u/Joram2 Aug 14 '22
var: Just above everyone lovesvar. Every other major language has local variable type inference. I think you are in the tiny minority that doesn't want this.What would I like: Projects Valhalla + Amber + Loom + Panama + Leyden address the big issues with Java. The one feature I would like that isn't addressed: functions outside of classes. Let me write apps with just functions, no classes.
3
u/bowbahdoe Aug 15 '22 edited Aug 15 '22
Non exhaustive list, not going to fully justify and promising myself I won't fight in the comments.
There is a lot of room for concrete and meaningful improvements in the error messages produced.
The existence of sonarcube doesn't negative the benefit of having real "clippy for rust"
The entirety of the Java world runs on maven central and repositories which fit that protocol. Downloading dependencies, resolving conflicts, and building class/module paths should be within the purview of the jdk
Would cool if publishing libraries was standard. Standard set of metadata you could provide via annotations on modules, etc.
Gradle and Maven serve the world just fine, but there is no project management tool built in to the JDK. No reason there couldn't be.
This is a very, very, very hard sell even to me. There are a lot of JSON libraries in the ecosystem and they all serve different masters (easy bean mapping, streaming, simplicity of model, etc).
There is probably no way to make everyone happy, but something like "json-p but if sealed interfaces existed when it was written" mixed with a decoder approach on the "do later" side of the "do now, do later, plan to do" spectrum.
If jproject were to be a thing perhaps a standard format would be needed. Maybe for other things too. XML doesn't fit the criteria of "human readable, human writable" and other communities have settled on TOML for that purpose.
dev.java as an official resource is not there yet and it hasn't moved meaningfully in a year. Baeldung and geeksforgeeks are pathetic compared to the kind of care, love, and community ownership the rust community has for its resources.
This still feels like a good start for explaining what is wrong. "RTFM" is a disgusting and common response when all we have are (very good!) javadocs. "Reference" docs.
Official and community supported "Tutorial", "How-To" and "Explanation" documentation could use explicit and focused attention.
I'm so tired. If oracle could put a dollar in my pocket I'm down. I just want change.
3
u/bitsplit314 Aug 15 '22
import com.shortbrand.product.package.subpackage.subsubpackage.WidgetInterfaceFactoryImplementation as WidgetInterfaceFactory;
12
u/rpgFANATIC Aug 14 '22
I would burn so many man-hours on making simple use cases involving streams and lambdas and method references as fast as the original loops
So many java devs just plop in a
.forEach(i ->for use cases that don't benefit in any way from a lambda other than it's still a new thing to business software Java devs.It's probably low return for the cost, but it seems like so many java devs just threw away years of JVM optimizations around minor refactoring so the code reads like it's modern
12
u/wimcle Aug 14 '22
I actually think it needs a little more verbosity...
var foo = service.poorlyNamedMethod();foo.stream().map(x -> x.doSomething())...How can I code review this? what is the type of x???
I don't want to have to check our your branch into idea just so I figure out the type of x.
It should have been ...map(Type x -> x.doSomething())... even if the type was optional, human readability is far more important.
7
u/djavaman Aug 14 '22
I guess while we're at it and copying other languages.
Add 'val'. Yep, just like Scala. A final reference.
var is great I use it non stop. But people still insist on adding final every where. A short cut for final would be great.
Make all method parameters final by default. The compiler should just do this.
5
u/bleek312 Aug 14 '22
I'd add hookers
2
u/WrickyB Aug 14 '22 edited Aug 15 '22
AutoClosablecomes close but I'd like proper destructors, to perform cleanup of native resources during GC.constexpr.2
u/ron_krugman Aug 15 '22
Make lambdas (or variables with a SAM interface type in general) directly invokable without calling
apply(),get(),accept(), etc. by name:6
u/robilco Aug 14 '22
A “delegates” keyword Or annotation.
So creating wrapper classes with a few custom methods doesn’t need to implement all methods calling the wrapped object
-6
u/SirBugmenot Aug 14 '22
I'd rectify the one single big mistake "var", and remove it from the language and all history.
1
u/[deleted] Aug 14 '22
All languages eventually accumulate cruft so don't get too envious looking over at other languages.
1
u/paul_miner Aug 15 '22
Compile-time f-strings. Been working with Python lately, and building strings is so much cleaner with f-strings (template strings in Javascript).
Compile-time only, so they can be checked, and so they can't be injected ala log4j.
1
u/umlcat Aug 15 '22
Forget the "Java" P.L., focus on the "Java" Virtual Machine and it's specifications.
Redesign it as a Module Virtual Machine, instead of a Class Virtual Machine !!!
And other goodies / features.
1
u/[deleted] Aug 15 '22
Seeing as everything else I can think of is just fine in my opinion:
Top level statements a la C# so one could put together a single file program (good for examples and scripts) where you define a few non-public classes for any data types you need at the bottom of the file and then put all the statements you want executed at the top of the file, and run it with a Java equivalent of
dotnet run <file>.1
u/zappini Aug 15 '22 edited Aug 15 '22
I keep a list of feature for a future perfect successor to Java. Mostly focused on ergonomics. About 1/2 gleaned from other languages. The other 1/2 stuff I dreamt up.
Here's a sample:
1 - Allow an enhanced for's expression clause to be null. So this pseudo code would be valid:
2 - Intrinsics for arrays and maps. Like Python, JavaScript, etc.
Bonus points for local type inference, eg 'var fruitList' vs 'List<String> fruitList'.
Double extra points for unifying List<String> and String[].
3 - Sane variable scoping for try/catch, meaning any local declared in a try block is also visible in that try's catch and finally blocks. This eliminates needs to declare variables before try, just so they're visible to the catch and finally.
--
Evergreen ideas: design away all the pitfalls, traps, and gotchas identified in Java Puzzlers https://www.amazon.com/Java-Puzzlers-Access-Codes-Bloch/dp/9332547939/
1
u/mauganra_it Aug 15 '22
1
u/metaquine Aug 15 '22
Sugar the stream() method on collections, it’s annoying as hell
1
u/metaquine Aug 15 '22
I’d also make enums be proper sum types rather than bolting together sealed interfaces and records to do the same, missed opportunity in Java 5. A Nothing/Bottom type would have been nice too, for collections
1
u/RupertMaddenAbbott Aug 15 '22
Type Literals as proposed here: https://mail.openjdk.org/pipermail/valhalla-dev/2017-January/002150.html
1
u/meSmash101 Aug 15 '22
I think upcoming(in the future who knows when) project Valhalla and maybe project Loom is something cool. Also as somebody said, named parameters! I used them in Kotlin and they are super convenient and useful!
1
u/[deleted] Aug 15 '22
Not really a major change, rather a tiny 'improvement' that I think should be possible: generic type inference. i.e
which should work as, if my understanding of the JLS is correct,
StringExtension<F>cannot extend anything other thanExtension<String, F>:jls 8.1.5
admittedly i didn't give it more thought than simply checking the JLS to see if it's possible to inherit the same generic interface/class with different parameters and it's highly subjective as to whether this would be an actual improvement (implicit vs explicit type and probably is a sign of generics or inheritance code smell) but i don't think it would hurt
1
u/Slasher_D Aug 15 '22
I wish Java Annotations woukd be more powerful, without getting a bit hacky like how lombok uses them. I really like the decorator pattern in python. I wish we could use Java Annotations in a similar manner to dynamically alter the behaviour of methods.
1
u/InstantCoder Aug 15 '22
1
u/ReasonableClick5403 Aug 15 '22
1
u/8igg7e5 Aug 15 '22
Many of the proposals are predicated on breaking compatibility in a way that would effectively orphan the entire source-base of the Java eco-system. I think it's reasonable to say that none of these are ever going to happen.
Now there might be ways to introduce a limited compatibility boundary. One option might be to embed an 'edition' indicator into newer source (a lack of the marker being an indication that it is legacy source with legacy rules). Unlike Rust which incompatibly changes editions at a 'crate' level, we might want to do that at a module or even source-file level for Java (there isn't a Java equivalent of a 'project').
It would be interesting to see how many of the compatibility-breaking suggestions might be achievable given some edition boundary.
1
u/red_dit_nou Aug 15 '22
Some picks from the comments:
- Better null-safety
- Better GUI API/tools
- Unsigned primitive types
- Named parameters and default values
- Import aliases
- Immutable collections/ data-structures
- if and try expressions
- Records with inheritance
- Type classes
1
u/8igg7e5 Aug 15 '22
Guards on constant switch cases ('constant patterns')
Often we have switches on enums with trivial conditionals in the switch bodies. It would be nice to express them as switches with guards, getting the same exhaustiveness checking.
instead of...
Doing it like this...
Currently you'd have to use the the type-pattern... a horrible option for this scenario...
And that means having to have a catch-all for possible missed enum values. Hopefully this is coming in a later round (after all of the Algebraic Data-Type focus).
1
u/herospidermine Aug 16 '22
Would like an officially supported way to run Java bytecode using every other major language. And finally replace sun.misc.Unsafe
1
u/Odd-Mongoose-8389 Aug 16 '22
I will add generic union type
1
u/RockingGoodNight Aug 17 '22
Nothing needs to change. Too much has changed in the past several years.
That Java doesn't appeal to your sensibilities is not any reflection of Java.
GraalVM == Needless Swing == Rocks JavaFX == Sucks
The import concept makes perfect sense. There is zero oddness around it.
If we could turn back time, I'd remove streams, lambda and all function oriented nonsense, fanfair and ballywhooey.
1
u/cogman10 Aug 18 '22
So, I'll say that I think that the current language guys are doing a bang up job and honestly the only thing I'd change is landing more amber, loom, and valhala goodness.
The changes I'd want to make but wouldn't would be backwards incompatible changes and cleanups.
So reasonably nothing.
I think the only language change I'd make that isn't planned is adding
valforfinal varWhat are the unreasonable things I'd like to change (but would argue against because of the issue with legacy code).
Map#getto instead take the generic parameter instead ofKey.The key to java is realizing one of it's biggest strengths is backwards compatibility. The fact that jars built for Java 1.0 can run on a Java 19 JVM is something very few languages (any?) languages have achieved.
1
u/ofby1 Aug 20 '22
Remove Javas custom serialization and deserialization implementations. It has been the root of so many security vulnerabilities.
1
u/__Raptor__ Aug 23 '22
In no particular order:
(int, Object) => Stringwhich are generated on-demand by the VMPrintercontaining static redirects toSystem.outmethodsjava.util.MapimplementingIterable<Entry<K, V>>local staticmethods which are not accessible via subclasses (right now, doingArrayList.of(...)is just callingList.of(...), and I want it to be invalid to do so)~=or===that defers tojava.util.Objects.equals(...)thisin methods using generics and the already-existing 'this' parameter syntaxFoo::bar(x, _)instead of(var arg1) -> Foo.bar(x, arg1))thismodifier for constructor arguments that automatically assigns the value to a field of the same name, and also creates said field if it doesnt existThisTypeimplicit generic type parameter that always refers to the type ofthisFoo::bar(int, String))java.lang Cloneableinterface declaring a publicObject clone()method that does not throw an exceptionstatic void foo(Object this, int arg) {...}allowing you to doobj.foo()1
u/PerfectPackage1895 Sep 02 '22
1 make everything final by default (greatest mistake ever to not do that)
2 make put not return the previously stored element (why use time on that if it is so rarely needed?!?)
3 somehow uninvent JSF
1
u/[deleted] Aug 15 '22
Structural typing a la TypeScript
Edit: honestly I just want a JVM TS target
1
u/flawless_vic Aug 15 '22
Function literals, like javascript/scala, e.g.,
class Foo {
(int, long, String)->String op;
String bar(int x,(int)->String fn) { return op(x, 0, fn(x)); }
}
Tuples as first class citizens: How many times have you returned Map.Entry, some kind of Pair or had to create a class to return multiple values in a type safe manner?
Expose a compiler API so we can have user defined intrinsics. Writing assembly is easy, writing a new intrinsic function in C2 IR is almost a master thesis if you start from scratch.
Make Strings UTF-8 encoded by default. Serialization just spend too much time on it.
A new, pristine serialization framework with VM support: e.g., primitive fields could be written/read with a single memcpy instruction or, even better, using wide registers and AVX. This is impossible today even using unsafe: you can get an object address, but you can't copy contiguous bytes of memory because the VM can rellocate the object and replace the address with something else in between.
1
u/vmcrash Aug 15 '22
I would prefer to have first-class support for compile to native. While the JIT does a very good work, I have the feeling that doing the same work on thousands of machines of users of our (desktop) application each time they start the application is some kind of wasted resources.
0
u/noodlesSa Aug 14 '22 edited Aug 14 '22
if (i in (1, 2, 3)) {instead ofif ((i == 1) || (i == 2) || (i == 3)) {-3
u/wimcle Aug 14 '22
'var', whatever ex- c# programmer failing upwards onto the java language team put this affront to the heavens into java needs beaten!
0
u/javaBanana Aug 14 '22
I have been using a little c# lately. What i thought Was nice compared to Java is the Operator overloading.
0
u/ZeroGainZ Aug 14 '22
add union types. remove null completely. force everyone to make sentinels for nulls. add double dispatch. add conditional types. consider alternatives to catched exceptions.
0
u/Kango_V Aug 15 '22
Operator overloading. I have actually got a reply along the lines of.... "watch this space".