How I’d Like Java To Be
I like Java. I enjoy programming in Java. But after using Python for a while, there are several things I would love to change about it. It’s almost purely syntactical, so there may be a JVM language that is better, but I’m not really interested since I still need to use normal Java for work. I realize that these changes won’t be implemented (although I thought I heard that one of them is actually in the pipeline for a future version); these are just some thoughts.
I don’t want to free Java up the way that Python is open and free. I actually often relish in the challenges that the restrictions in Java present. I mostly just want to type less. So, here are the changes I’d love to see in Java.
Get Rid of Semicolons
I realize that they serve a purpose, but they really aren’t necessary. In fact, they actually open up the code to possibly being harder to read, since shoving multiple lines of code onto the same line is almost always more difficult to read. Technically, with semicolons, you could compress an entire code file down to one line in order to reduce the file size, but how often is that done in Java? It may be done more than I know, but I don’t know of any case that it’s done.
Remove the Curly Braces
There are two main reasons for this. First off, we could end the curly brace cold war! Second, we can stop wasting lines of code on the braces.
Also, like I said earlier, I’m trying to reduce how much typing I’m doing, and this will help. Lastly, by doing this, curly braces can be opened up for new uses (you’ll see later).
Operator Overloading
When it comes to mathematical operations, I don’t really care about operator overloading. They can be handy, but methods work okay for that. My biggest concern is comparison, especially ==. I really wish Java had followed Python in having == be for equality checking (you can even do it through the equals
method) and “is” for identity checking. And while we’re at it, implementing Comparable
should allow you to use the comparison operators with them, rather than needing to translate the numeric return values yourself.
If you want, you can allow some way to overload math operators, too.
Tuples and/or Data Structures
I could use either one, but both would be better. Tuples are especially useful as a return type for returning multiple things at once, which is sometimes handy. The same can be done with simple data structures (essentially C structs), too, since they should be pretty lightweight.
A big thing for data structures is getting rid of Java Beans. It would be even better if we would be able to define invariants with them too. The big problem with Java Beans is that we shouldn’t have to define a full-on class just to pass some data around. If we can’t get structs, then, at the very least, I would like to get the next thing.
Properties
Omg, I love properties, especially in Python. Allowing you to use simple accessors and mutators as if it was a straight variable makes for some nice-looking code.
Default to public
I’ve seen a few cases where people talk about “better defaults”, where leaving off a modifier keyword (such as public
and private
, or static
) should be for the most typical case. public
is easily the most used keyword for classes and methods, so why is the default “package-private”? I could argue for private
to be the default for fields, too, but I kind of think that the default should be the same everywhere in order to reduce confusion, but I’m not stuck on that.
I debate a little as to whether variables should default as final in order to help push people toward the idea of immutability, but I don’t care that much.
Type Objects
This kind of goes with the previous thing about smart defaults. I think the automatic thing for primitives is to be able to use them as objects. I don’t really care how you do this. Preferably, you’d leave it open to get the true primitives in order to optimize if you want. How this works doesn’t really matter to me. It would be kind of cool if they’re naturally passed around as primitives most of the time, but they autobox into the objects simply by calling any of their methods. Parameters and return types should not care which one is being passed. This would also help to hugely reduce the number of built-in functional interfaces in Java, since a majority are actually duplicates dealing with primitives.
List, Dictionary, and Set Literals
For those of you who have used javaScript or Python, you really know what I’m talking about. I mean, how flippin’ handy is THAT? This, tied in with constructors that can take Streams (sort of like Java’s version of Generators. Sort of), would make collections a fair bit easier to work with. Dictionary literals and set literals make for some really good uses of curly braces.
Fin
That’s my list of changes that I’d love to see in Java. Like I said before, I don’t think these will ever happen (although I think I heard that they were working towards type objects), and it’s really just a little wish list. Do you guys agree with my choices?
Reference: | How I’d Like Java To Be from our JCG partner Jacob Zimmerman at the Programming Ideas With Jake blog. |
So, what you’re saying is, you want to use a programming language that isn’t Java. Go ahead; no one’s stopping you.
The answer is Groovy
you could try groovy or scala
I believe the appropriate phrase is those who don’t learn from history are doomed to repeat it.
So you want to have Python syntax in Java ;)
I agree with many of your comments but I don’t know if the point related to default visibility maybe be syntomatic of a poor understanding of how things should be designed. By default, classes inside a module (let’s name it, package) should collaborate together to achieve a well defined purpose and only part of the functionality of the package should be exposed externally -actually, only the part that external users of that package could be interested in. That’s why default visibility is mostly package scoped and to make something more visible (let’s say, “public) you have to explicitly tell by… Read more »
I have been wishing for a serious re-vamp of Java Beans, whether it be by syntactical sugar (implicit getter/setters, where the methods are overridable but not required), or the oft-suggested addition of data structures. I still haven’t figured out the issue with curlies; maybe I’m not as interested in source file size as others. Also, changing the defaults is a complete non-starter, unless you really want every line of existing java source to suddenly need refactoring. Plus, it’s fundamentally pointless.
The modifications you wanted in Java are the very reasons i hate script languages. How could you ask such a mess to happen to Java.
To those telling me to use a different language, I have 3 things to say: 1) I AM using a different language, python, for my personal projects 2) My job doesn’t provide me with much of a chance to branch out from Java, and I like where I work, so I’m not going to quite just to use a different language 3) Read the 1st paragraph: I still LIKE Java. Everything listed is preferences that would make using it a little nicer most of the time. To Jorge: While that DOES make sense, I prefer to think that the default… Read more »
You are essentially saying that you want a better Java. I am walking down the same path and I am currently learning Scala. It seems to offer everything you are looking for and more. OK, I am sure Scala has its quirks too but sofar it has been a pleasant surprise. Check it out!
And by the way, I DID read that you want those changes in Java :-). But then it wouldn’t be Java any more so that’s kind of pointless – some of your suggested changes are not backward compatible so will never happen. However (as you know already of course), the beauty with the other JVM languages e.g., Scala, is that you mix them with Java.
Jacob,
The Groovy ecosystem has a lot of the items you are looking for and makes for a very powerful scripting language.
Grails lets you build apps quickly with a spring backend
Gradle is one of the best build mangers I have seen
Chandra