Can Java Be Saved?

Posted on November 09, 2009 by Scott Leberknight

Java and Evolution

The Java language has been around for a pretty long time, and in my view is now a stagnant language. I don't consider it dead because I believe it will be around for probably decades if not longer. But it appears to have reached its evolutionary peak, and it doesn't look it's going to be evolved any further. This is not due to problems inherent in the language itself. Instead it seems the problem lies with Java's stewards (Sun and the JCP) and their unwillingness to evolve the language to keep it current and modern, and more importantly the goal to keep backward compatibility at all costs. Not just Sun, but also it seems the large corporations with correspondingly large investments in Java like IBM and Oracle aren't exactly chomping at the bit to improve Java. I don't even know if they think it even needs improvement at all. So really, the ultra-conservative attitude towards change and evolution is the problem with Java from my admittedly limited view of things.

That's why I don't hate Java. But, I do hate the way it has been treated by the people charged with improving it. It is clear many in the Java community want things like closures and a native property syntax but instead we got Project Coin. This, to me, is sad really. It is a shame that things like closures and native properties were not addressed in Java/JDK/whatever-it-is-called 7.

Why Not?

I want to know why Java can't be improved. We have concrete examples that it is possible to change a major language in major ways. Even in ways that break backward compatibility in order to evolve and improve. Out with the old, in with the new. Microsoft with C# showed that you can successfully evolve a language over time in major ways. For example C# has always had a property syntax but it now also has many features found in dynamically typed and functional languages such as type inference and, effectively, closures. With LINQ it introduced functional concepts. When C# added generics they did it correctly and retained the type information in the compiled IL, whereas Java used type-erasure and simply dropped the types from the compiled bytecode. There is a great irony here: though C# began life about five or six years after Java, it not only has caught up but has surpassed Java in most if not all ways, and has continued to evolve while Java has become stagnant.

C# is not the only example. Python 3 is a major overhaul of the Python language, and it introduced breaking changes that are not backwards compatible. I believe they provide a migration tool to assist you should you want to move from the 2.x series to version 3 and beyond. Microsoft has done this kind of thing as well. I remember when they made Visual Basic conform to the .NET platform and introduced some rather gut wrenching (for VB developers anyway) changes, and they also provided a tool to aid the transition. One more recent example is Objective-C which has experienced a resurgence in importance mainly because of the iPhone. Objective-C has been around longer than all of Java, C#, Ruby, Python, etc. since the 1980s. Apple has made improvements to Objective-C and it now sports a way to define and synthesize properties and most recently added blocks (effectively closures). If a language that pre-dates Java (Python also pre-dates Java by the way) can evolve, I just don't get why Java can't.

While it is certainly possible to remain on older versions of software, forcing yourself to upgrade can be a Good Thing, because it ensures you don't get the "COBOL Syndrome" where you end up with nothing but binaries that have to run on a specific hardware platform forever and you are trapped until you rewrite or you go out of business. The other side of this, of course, is that organizations don't have infinite time, money, and resources to update every single application. Sometimes this too can be good, because it forces you to triage older systems, and possibly consolidate or outright eliminate them if they have outlived their usefulness. In order to facilitate large transitions, I believe it is very important to use tools that help automate the upgrade process, e.g. tools that analyze code and fix it if possible (reporting all changes in a log) and which provide warnings and guidance when a simple fix isn't possible.

The JVM Platform

Before I get into the changes I'd make to Java to make it not feel like I'm developing with a straightjacket on while having to type masses of unnecessary boilerplate code, I want to say that I think the JVM is a great place to be. Obviously the JVM itself facilitates developing all kinds of languages as evidenced by the huge number of languages that run on the JVM. The most popular ones and most interesting ones these days are probably JRuby, Scala, Groovy, and Clojure though there are probably hundreds more. So I suppose you could make an argument that Java doesn't need to evolve any more because we can simply use a more modern language that runs on the JVM.

The main problem I have with that argument is simply that there is already a ton of Java code out there, and there are many organizations who are simply not going to allow other JVM-based languages; they're going to stick with Java for the long haul, right or wrong. This means there is a good chance that even if you can manage convince someone to try writing that shiny new web app using Scala and its Lift framework, JRuby on Rails, Grails, or Clojure, chances are at some point you'll also need to maintain or enhance existing large Java codebases. Wouldn't you like to be able to first upgrade to a version of Java that has closures, native property syntax, method/property handles, etc?

Next I'll choose what would be my top three choices to make Java much better immediately.

Top Three Java Improvements

If given the chance to change just three things about Java to make it better, I would choose these:

  • Remove checked exceptions
  • Add closures
  • Add formal property support

I think these three changes along would make coding in Java much, much better. Let's see how.

Remove Checked Exceptions

By removing checked exceptions you eliminate a ton of boilerplate try/catch clauses that do nothing except log a message, wrap and re-throw as a RuntimeException, pollute the API with throws clauses all over the place, or worst of all empty catch blocks that can cause very subtle and evil bugs. With unchecked exceptions, developers still have the option to catch exceptions that they can actually handle. It would be interesting to see how many times in a typical Java codebase people actually handle exceptions and do something at the point of exception, or whether they simply punt it away for the caller to handle, who in turn also punts, and so forth all the way up the call stack until some global handler catches it or the program crashes. If I were a betting man, I'd bet a lot of money that for most applications, developers punt the vast majority of the time. So why force people to handle something they cannot possible handle?

Add Closures

I specifically listed removing checked exceptions first, because to me it is the first step to being able to have a closure/block syntax that isn't totally horrendous. If you remove checked exceptions, then adding closures would seem to be much easier since you don't need to worry at all about what exceptions could possibly be thrown and there is obviously no need to declare exceptions. Closures/blocks would lead to better ability to handle collections, for example as in Groovy but in Java you would still have types (note I'm also using a literal property syntax here):

// Find all people whose last name is "Smith"
List<Person> peeps = people.findAll { Person person -> person.lastName.equals("Smith");   } 
// Create a list of names by projecting the name property of a bunch of Person objects
List<String> names = people.collect { Person person ->; }

Not quite as clean as Groovy but still much better than the for loops that would traditionally be required (or trying to shoehorn functional-style into Java using the Jakarta Commons Collections or Google Collections). Removal of checked exceptions would allow, as mentioned earlier, the block syntax to not have to deal with declaring exceptions all over the place. Having to declare checked exceptions in blocks makes the syntax worse instead of better, at least when I saw the various closure proposals for Java/JDK/whatever 7 which did not get included. Requiring types in the blocks is still annoying, especially once you get used to Ruby and Groovy, but it would be passable.

Native Property Syntax

The third change should do essentially what Groovy for properties does but should introduce a "property" keyword (i.e. don't rely on whether someone accidentally put an access modifier in there as Groovy does). The syntax could be very clean:

property String firstName;
property String lastName;
property Date dateOfBirth;

The compiler could automatically generate the appropriate getter/setter for you like Groovy does. This obviates the need to manually code the getter/setter. Like Groovy you should be able to override either or both. It de-clutters code enormously and removes a ton of lines of silly getter/setter code (plus JavaDocs if you are actually still writing them for every get/set method). Then you could reference properties as you would expect: is the "getter" and = "Fred" is the "setter." Much cleaner syntax, way less boilerplate code. By the way, if someone used the word "property" in their code, i.e. as a variable name, it is just not that difficult to rename refactor, especially with all the advanced IDEs in the Java community that do this kind of thing in their sleep.

Lots of other things could certainly be done, but if just these three were done I think Java would be much better off, and maybe it would even come into the 21st century like Objective-C. (See the very long but very good Ars Technica Snow Leopard review for information on Objective-C's new blocks feature.)

Dessert Improvements

If (as I suspect they certainly will :-) ) Sun/Oracle/whoever takes my suggestions and makes these changes and improves Java, then I'm sure they'll want to add in a few more for dessert. After the main course which removes checked exceptions, adds closures, and adds native property support, dessert includes the following:

  • Remove type-erasure and clean up generics
  • Add property/method handles
  • String interpolation
  • Type inference
  • Remove "new" keyword

Clean Up Generics

Generics should simply not remove type information when compiled. If you're going to have generics in the first place, do it correctly and stop worrying about backward compatibility. Keep type information in the bytecode, allow reflection on it, and allow me to instantiate a "new T()" where T is some type passed into a factory method, for example. I think an improved generics implementation could basically copy the way C# does it and be done.

Property/Method Handles

Property/method handles would allow you to reference a property or method directly. They would make code that now must use strings strongly typed and refactoring-safe (IDEs like IntelliJ already know how to search in text and strings but can never be perfect) much nicer. For example, a particular pet peeve of mine and I'm sure a lot of other developers is writing Criteria queries in Hibernate. You are forced to reference properties as simple strings. If the lastName property is changed to surname then you better make sure to catch all the places the String "lastName" is referenced. So you could replace code like this:

	.add(Restrictions.eq("lastName", "Smith")

with this using method/property handles:

	.add(Restrictions.eq(Person.lastName, "Smith")

Now the code is strongly-typed and refactoring-safe. JPA 2.0 tries mightily to overcome having strings in the new criteria query API with its metamodel. But I find it pretty much appalling to even look at, what with having to create or code-generate a separate "metamodel" class which you reference like "_Person.lastName" or some similar awful way. This metamodel class lives only to represent properties on your real model object for the sole purpose of making JPA 2.0 criteria queries strongly typed. It just isn't worth it and is total overkill. In fact, it reminds me of the bad-old days of rampant over-engineering in Java (which apparently is still alive and well in many circles but I try to avoid it as best I can). The right thing is to fix the language, not to invent something that adds yet more boilerplate and more complexity to an already overcomplicated ecosystem.

Method handles could also be used to make calling methods using reflection much cleaner than it currently is, among other things. Similarly it would make accessing properties via reflection easier and cleaner. And with only unchecked exceptions you would not need to catch the four or five kinds of exceptions reflective code can throw.

String Interpolation

String interpolation is like the sorbet that you get at fancy restaurants to cleanse your palate. This would seem to be a no-brainer to add. You could make code like:

log.error("The object of type  ["
    + foo.getClass().getName()
    + "] and identifier ["
    + foo.getId()
    + "] does not exist.", cause);

turn into this much more palatable version (using the native property syntax I mentioned earlier):

log.error("The object of type [${}] and identifier [${}] does not exist.", cause);

Type Inference

I'd also suggest adding type inference, if only for local variables like C# does. Why do we have to repeat ourselves? Instead of writing:

Person person = new Person();

why can't we just write:

var person = new Person();

I have to believe the compiler and all the tools are smart enough to infer the type from the "new Person()". Especially since other strongly-typed JVM languages like Scala do exactly this kind of thing.

Elminate "new"

Last but not least, and actually not the last thing I can think of but definitely the last I'm writing about here, let's get rid of the "new" keyword and either go with Ruby's new method or Python's constructor syntax, like so:

// Ruby-like new method
var person =

// or Python-like construction
var person = Person()

This one came to me recently after hearing Bruce Eckel give an excellent talk on language evolution and archaeology. He had a ton of really interesting examples of why things are they way they are, and how Java and other languages like C++ evolved from C. One example was the reason for "new" in Java. In C++ you can allocate objects on the stack or the heap, so there is a stack-based constructor syntax that does not use "new" while the heap-based constructor syntax uses the "new" operator. Even though Java only has heap-based object allocation, it retained the "new" keyword which is not only boilerplate code but also makes the entire process of object construction pretty much immutable: you cannot change anything about it nor can you easily add hooks into the object creation process.

I am not an expert at all in the low-level details, and Bruce obviously knows what he is talking about way more than I do, but I can say that I believe the Ruby and Python syntaxes are not only nicer but more internally consistent, especially in the Ruby case because there is no special magic or sauce going on. In Ruby, new is just a method, on a class, just like everything else.

Conclusion to this Way Too Long Blog Entry

I did not actually set out to write a blog whose length is worthy of a Ted Neward blog. It just turned out that way. (And I do in fact like reading Ted's long blogs!) Plus, I found out that speculative fiction can be pretty fun to write, since I don't think pretty much any of these things are going to make it into Java anytime soon, if ever, and I'm sure there are lots of people in the Java world who hate things like Ruby won't agree anyway.


You should become a member of the JCP and push for these changes. If anyone could do it, it would be you.

Posted by Carl Fyffe on November 09, 2009 at 12:12 PM EST #

I think they would probably kill me first. :-)

And let's be honest. There are way, way smarter people than me who probably have campaigned for changes like this and lost. That, or they just went and wrote their own language, which I am not smart enough to do.

Posted by Scott Leberknight on November 09, 2009 at 01:10 PM EST #

"chomping at the bit"

Champing at the bit. Not "chomping" at the bit.

Posted by David Adams on November 09, 2009 at 10:23 PM EST #

David Adams: This is a blog post, not an AP news item. Dialectic variation is perfectly acceptable. Especially when the variant in question is dominant in use.

Posted by grant on November 10, 2009 at 04:04 AM EST #

Have you tried groovy yet? You really should. This is the way to code java the way you want it.

Posted by mgr on November 10, 2009 at 04:30 AM EST #

I must say I agree with all your points.
Hope at least properties become a reality.

Posted by Carlos Ferreyra on November 10, 2009 at 04:33 AM EST #

The two code examples in "Property/Method Handles" are identical :-)

Posted by Nja on November 10, 2009 at 04:43 AM EST #

I agree with mgr: you can use groovy (or Scala, JRuby, Clojure) over JVM, so you'll have all the desired benefits, and still, you can use the Java APIs, framework, etc. On the other hand, Java remains backward compatible.

Posted by Tamas Rev on November 10, 2009 at 08:15 AM EST #

Tamas and mgr: I actually do use Groovy quite a bit, and I have played with both Scala and Clojure a little bit, mostly by being a reviewer on Venkat Subramaniam's Programming Scala book and Stu Halloway's Programming Clojure book. On a recent project which has been in production since October 2007 we've converted all unit tests to Groovy and about 40% of the production code to Groovy. We've been pleased with the reduction in number of lines of code and boilerplate, and the flexibility we get from Groovy. Note this is not an extremely high-performance high-volume app, rather it is a webapp that talks to an RDBMS mostly, so we haven't seen really any performance issues with Groovy. Plus it is still a Spring/Hibernate webapp with all the goodness (or badness depending on your view) which those entail. The only difference is the language is Groovy not Java.

Posted by Scott Leberknight on November 10, 2009 at 09:24 AM EST #

Just a note about pythons class constructors; no magic is going on their either. The following is an approximation that simplifies what happens from the outside:

A class statement creates an object that is a descendant of Type. Type implements __call__ to a) call its own __new__ method to create a construct a new instance and then call that instance's __init__ method to initialize that instance. The class object is bound to a name and thus becomes a firstclass callable just like any other.

It only appears to be magic if you've never looked inside.

Posted by andrew on November 10, 2009 at 02:08 PM EST #

Wouldn't an organization that will only use Java also be an organization that would be slow to adopt Java 7/8/X? In such an organization, you might be lucky if you can use Java 5 features, let alone something like closures.

Posted by Scot McSweeney-Roberts on November 10, 2009 at 04:10 PM EST #

@Scot Yes, you're probably right. During the architecture BOF at the Reston No Fluff Just Stuff conference this past weekend, there were still several people on Java 1.4! And the majority were on Java 5, with only a few on Java 6. That was only a sampling of about 15 people, though. But I never understand why organizations continue to restrict new applications to the same 5+ year old platform that apps developed over 5 years ago were built on. At the least I would think it easier to allow newer apps to run on new versions. And if an organization does any maintenance at all on apps, then perhaps more emphasis should be placed on keeping relatively current with versions. But then again, what do I know? I'm no CIO, CTO, nor do I have control over a multi-million dollar infrastructure.

Posted by Scott Leberknight on November 10, 2009 at 06:50 PM EST #

@andrew Thanks for the info on Python. I've only done very minimal scripting in Python and have only read one beginner book on Python several years ago.

Posted by Scott Leberknight on November 10, 2009 at 07:07 PM EST #

I've been using Java as primary choice language for years in order to satisfy everyday corporate requirements/sh** and starting to have less and less fun. On the other hand all my private projects are done in Python and I enjoy it a lot.

I never questioned Java, nor tried to reinvent some of its over-complexed aspects. But, after reading your post it all came together and its so obvious.
Thanks for awesome post. Cheers!

Posted by bojan babic on November 12, 2009 at 12:03 AM EST #


A Newardian post deserves a Newardian reply. :)

I like your language additions. But those kind of additions are going to be happening in the context of the JCP, and many don't have the patience for that. I think some of Spring, Guice, and CDI's enhancements to the programming model are novel ways of evolving given the restrictive "all things to everyone", bureaucratic nature of Java at this stage of the game. I think you can directly attribute people's despise for Java, or maybe more aptly, their enthusiasm for new languages, to the difficulty involved in making language changes to Java. Regardless, there is still a freshness to the innovation in the Java world, but people are looking for it in the wrong place: it's in the programming model, not the language.

Your description of a native property syntax reminded me of another blog I read recently:

Sure, it lacks the punch of a native language feature - but is it that much different than what you're proposing? Do I really need type inference? I see it as easier to read and more powerful to remove the duplication of repeating a variable declaration and constructor call by removing the call to the constructor, as you point out, by not using the new keyword. I can annotate a method as a producer and have the exact instance injected. Again, this is a programming model enhancement that's independently evolved and proven itself.

I think there is still a lot of room for evolution, and a lot of work being done. As you point out, the 800 lb gorilla seems to be continued by-in and enthusiasm from vendors - but most if not all of the real innovation that is occurring in the community is through open source software. It's interesting how you have a rift in the open source space, in how, e.g. the Spring and the Seam teams have approached developing open source. Do you work around the standards, which have tended to be over engineered themselves and riddled with problems? Or is the best plan to evolve a more realistic specification from frameworks that have been tested in the wild? This is a fundamental issue plucking at the heart-strings of Java's future. I believe you alluded to it in a talk on Real World Hibernate Tips I listened to you give not too long ago. There are some big philosophical differences as to how we evolve and innovate in Java. But I'm not sure we need to reconcile the two models - they both have merit in their own right. The fact that there are competing models says to me that there is some competition, and the fate of Java is not yet sealed. The programming model can evolve outside of language features, as we've seen with Spring, Guice, CDI and to a certain extent new JVM languages. And vendors seem to be more open to evolving the programming model than the language at this point.

I certainly welcome any of your thoughts on this. Thanks for the interesting article.

Posted by Chris Wash on November 13, 2009 at 07:58 PM EST #

I started typing out a response but it got too long to post here. I looked around for a way to email it to you but couldn't find one so I posted it on my blog here.

Appreciate any thoughts/comments!

Posted by Chris Wash on November 13, 2009 at 08:54 PM EST #

Post a Comment:
Comments are closed for this entry.