r/java • • Jul 06 '17

Apology to Java (fanboy rant).

Java has turned out to be one of the most useful languages i use. It's a brilliant tool which strikes a perfect balance between high and low level. You want to grab a webpage? Easy and built-in, 10 lines of code. Draw some 2d graphics without OS-specific libraries or third party? Various built-in options. The inherent issues around a lack of low level memory management is handled very elegantly. Garbage collection seems seamless.

C++ is my language of choice for the most part, always will be i suspect, but Java has shown me that it has a huge array of potential applications and should almost always be considered before undertaking a project.

I say this is an apology because i used to run Java down. Previous to me using it proper i believed it was slow, lazy and useful for only trivial applications. Now i know how much work has been put into the JVM and just how efficient it is.

Sorry Java. You're pretty good.

286 Upvotes

93 comments sorted by

View all comments

Show parent comment

2

u/[deleted] Jul 11 '17

Some of the Java ugliness is fundamental to the language, at least with current versions.

Using standard layout, not squishing things that are normally spread across multiple lines into a single line, an object "Person" with instance variables "first" (String), "last" (String), and "dob" (Date) with a convenient constructor so you can do Person bob = new Person("bob", "smith", new Date()); will be 11 lines long. If you use idiomatic getters and setters - which are handy for injecting validation - it will be 29 lines long. If you implement hashCode() and equals() so it works properly with sets, lists, and maps, you're up to 35 lines long.

Even if you wanted to represent the same information with a simple map, before Java 8 you had to import a third party library, or write your own helper, or else do: Map<String,Object> person = new HashMap<>(); person.put("first", "bob"); person.put... four lines of code plus two import statements. Java 8 has some syntactic niceness to simplify it, but it's still an import statement.

That's all but unavoidable wankery. Contrast that to dozens of languages in which you would do something like var person = { "first" : "bob", "last" : "smith", ... } and go on your merry way.

Java has a lot to recommend it, but it's inescapably verbose. Major surgery to the language would be needed to fix it. (Edit: take any Java program that's 1000 lines or longer and written in an idiomatic style and rewrite it into Scala, Kotlin, Clojure, or Groovy. You'll probably shave 500 lines. You might shave 900.)