loading...
Cover image for Java is too old, What should you learn in 2018?

Java is too old, What should you learn in 2018?

erajasekar profile image Rajasekar Elango Originally published at hackernoon.com Updated on ・1 min read

Java is too old, What should you learn in 2018?

Java has become legacy. It can’t evolve in to a modern language while keeping its backward compatibility. But it has given us a wonderful JVM ecosystem and lead to creation of many good languages Groovy, Scala, Clojure, Kotlin.

Welcome the newest baby Kotlin

Kotlin was born in 2011, But it gained popularity last year after google announced it as official language for Android. Kotlin has brought powerful features from many other JVM languages. Let take a quick glance at benefits of Kotlin for a Java developer.

Never worry about NPEs

Every Java programmer hates Null Pointer Exceptions thrown at Runtime. Kotlin provides first class support to avoid null references at compile time. All objects are non-nullable by default and you have to use ? operator to define nullable types. Compiler will force you to use safe call operator ?. to access nullable objects. You can also use elvis operator ?: to assign default values.

No need to provide explicit type declaration.

Kotlin automatically infers types, so you don’t need to declare it explicitly. You can simply define variables using val for final variables and var for non-final variables. Note type can be inferred only if both declaration and assignment is done in single statement.

Avoid convoluted String formatting.

In Kotlin you can use String templates for easier formatting of Strings. $is used to reference a variable and you can use ${} for complex expressions

No boiler plate code required to create simple POJOs.

Kotlin provides data classes for objects used to simply hold values. It automatically generates equals , hashCode , toString , copy , getters and setters ( for properties defined as var) methods for data classes. You can also do object deconstruction of data class to extract properties to variables.

You can avoid Builder classes and redundant method overloads.

Kotlin supports named method parameters so you don’t need to create builders in most cases. Also, Kotlin supports default method parameters so you don’t need to create redundant overloaded methods to pass default values.

You no longer need guava library to statically initialize collections.

Kotlin provides concise way to initialize collections inline using listOf , mapOf , setOf methods. Maps also support intuitive syntax key to value for initialization. It also provides deconstruction of Map key, values for easy iteration.

You don’t need a complicated way to create Singletons.

Kotlin supports object declaration to create Singletons in single line.

You don’t need unnecessary local variables

In Kotlin, constructs like try and when are expressions that returns value. For e.g You can assign result of try to a variable instead of creating a local variable. Similarly for when can be used as expression. when is equivalent to switch in Java, but it is much more powerful.

You will avoid Class Cast exceptions.

Kotlin provides is operator (equivalent to instanceOf in Java) to check if object is specific type . Using is operator will automatically do casting for you. This will prevent Class Cast exception when you cast to wrong type.

You don’t need to repeat the variable name to call sequence methods in same object.

Kotlin provides with construct to easily call sequence of methods on same object without having to repeat the variable name. We generally use builder pattern and method chaining to that in Java. Kotlin makes it easy to do similar thing even for non-builder classes.

Kotlin also provides apply extension function to achieve the same thing.

You don’t need boilerplate code to use delegation or decorator pattern.

To favor composition over inheritance, we often use delegation or decorator pattern, but we had to duplicate every method of delegated class in wrapper class. Kotlin provides first class support to simplify delegation using by operator. It will automatically implement necessary methods to call methods of delegated class. Of course, you can override specific methods when need to.

You don’t need to a Class to create static functions.

Kotlin supports functions outside of classes, so you don’t need to create a class just for static utility functions.

You don’t need a hack to modify non-final variables in lambdas.

Java supports lambda by automatically substituting it with anonymous inner classes, but you can’t modify non-final variable inside lambdas. But in Kotlin you can also modify non-final variables within lambda.

Lazy loading doesn’t require hard work.

Kotlin provides very simple way to lazily initialize a property using lazy keyword.

You don’t have to totally switch to a new language.

Kotlin interoperates with Java seamlessly, so you can easily integrate with legacy Java code. You can continue to leverage third party Java libraries and frameworks. Unlike Scala, Kotlin doesn’t have it’s own collection library, but extends JDK collections. So you don’t need to write glue code to convert between Java and Kotlin collection types.

You can write concise and more readable code.

Kotlin uses method name convention to overload many operators for readability. For e.g method plus is used to overload + operator, minus for - operator, times for * operator, div for / operator and so on. It supports overloading many more operators like %, += , +- , ++,--

Kotlin provides concise way to define ranges using .. operator. It also provides until keyword for excluding boundaries and step operator for skipping items. It has in operator to check for something in range. We can also overload .. operator and in operator by implementing rangeTo and contains methods.

You can easily extend existing JDK classes.

Kotlin support easy way to add extension functions to existing classes. This is a very power feature that helps us to easily expand core language API.

Why should you use Kotlin?

It boosts your productivity by many fold

  • It’s statically typed language, so you will catch lot of bugs at compile time.

  • It has great tooling support. No doubt it’s created by an IDE company

  • It is created by JetBrains a IDE company which cares for developer productivity.

  • Avoid many of day to day frustrations of a Java developer.

You get benefits of many best practices (Principles from Effective Java Book) by default.

  • All classes are final.

  • Collections are immutable.

  • Override mandatory keyword instead of optional annotation

  • No Checked exceptions

  • No Raw types

  • Cleaner support for generics.

Non-JVM support.

  • Kotlin also compiles to Javascript for front end development.

  • They are also working on adding native runtime to make it run without JVM.

Should you switch to Kotlin?

Kotlin is awesome but it’s not yet perfect. It will take some time to evolve in to a great language. JetBrains and Google are actively behind Kotlin so you can sure be that it will only get better.

If you are an Android developer, you should start using Kotlin immediately.

If you are a Java developer, you might have to consider other factors like team members, company adoption etc. But even if you can’t use it immediately, you should definitely learn this modern piece of beauty.

Finally, As per thoughtworks technology radar, companies can adopt using kotlin in projects when appropriate.

References

Discussion

pic
Editor guide
Collapse
martin profile image
Martin Beentjes

What task are you solving? Yes, Kotlin is fresh and new and superawesome. But Java is as well. Performance-wise it is the same as both compile to JVM.

For me there are no big improvements to move from Java to Kotlin. I prefer the more verbose way Java is doing things.

I tried to get used to Kotlin but it has a steep learning curve.

It really depends on the task that defines the toolkit being used to solve the problem.

Collapse
xgrimau profile image
Xavi

Additionally, I feel like a lot of the issues mentioned are already solved in new versions of Java

Collapse
jmfayard profile image
Jean-Michel Fayard 🇫🇷🇩🇪🇬🇧🇪🇸🇨🇴

Steep learning curve?
That was not my experience.
You should be able to get starting quite easily by git cloning the Kotlin Koans and fixing the unit tests in Intellij
github.com/Kotlin/kotlin-koans

Collapse
silverhusky profile image
SilverHusky

Can't agree more, it really varies and depends. I prefer to stick to Java if it doesn't make too much difference

Collapse
tux0r profile image
tux0r

Or Delphi, Lisp or C... it all depends on the task to solve!

Collapse
rhymes profile image
rhymes

While I was scrolling I was 99% sure you would have said something :D

Collapse
tux0r profile image
tux0r

Surprise!

Collapse
tedhagos profile image
Ted Hagos

+1 on this. It all depends on the task to solve.

It might also depend on what you're trying to do. If you're trying to get a job on company (in your area) that uses Java (and you happen to like Java and the company) then go with Java. If you're trying to break into the consulting gig and there's lots of Kotlin jobs, then learn Kotlin ... substitute Kotlin and/or Java for whatever language applies. At the speed of how the tech world changes, I think there's certain advantage to being language agnostic. Each of these languages reached widespread adoption for a reason

Collapse
gerbrandvd profile image
Gerbrand van Dieyen

If you want to learn to program, good to start in a language that's good for learning to program (felienne.com has quite some resources on that).
I'd agree with @tuxOr to try out different languages, not just depending on the task you want to solve, but also to broaden your point of view.

Collapse
okolbay profile image
andrew

this article is such a fanboi teenage view on languages, and especially java )
besides the fact that java evolves and evolve other platforms, it actually has local variable type inference, so comparison is outdated at its best. I urge you to take a closer look at Kotlin’s origins too, which is owners of JetBrains, that claimed in one of their interviews, that they would take full advantage of owning both language and IDE to make a perfect blend - might turn out to be a biased platform.

PS and yes if you haven’t heard of Lombok for java - don’t thank me, its way much more powerful than built-in Kotlin data-class ))

Collapse
stealthmusic profile image
Jan Wedel

Although I find fanboy teenage view a bit harsh to say, I have to agree here.

Java is actually very mature and got lots of new features in the past years.

All the same has been said about Scala when it was released and it was hyped. I actually liked it because it had some nice features borrowed from Erlang. Nevertheless, it didn’t get mainstream as Java is.

And Lombok actually pretty handy. Have a look at my article about modern software development in Java.

So, I wouldn’t burn all the Java books yet. It’s always good to have a look at new languages, even learn those languages but I’m still skeptic about if it will at some point in time take over the role that Java has right now.

Collapse
rapasoft profile image
Pavol Rajzak

Lombok is great, but the downside is that it is not part of the language. It's a separate project with it's own bugs and issues (especially compatibility) and you neef to enforce it's usage by IDE or Maven plugins (separate projects with their own issues and bugs).

Local variabled btw will be available in Java 11, which is still not releasef yet.

Collapse
khmarbaise profile image
Karl Heinz Marbaise

Java 11 has been released on 25. September 2018...

Thread Thread
rapasoft profile image
Pavol Rajzak

Ah! I thought it was scheduled for October. My bad! I bet everyone is on Java 11 by now.

Collapse
jmfayard profile image
Jean-Michel Fayard 🇫🇷🇩🇪🇬🇧🇪🇸🇨🇴

it actually has local variable type inference, so comparison is outdated at its best.

That's one feature, and it's only in Java 10.
Before that is available in, let say Android, we will all be dead.

I urge you to take a closer look at Kotlin’s origins too, which is owners of JetBrains, that claimed in one of their interviews, that they would take full advantage of owning both language and IDE to make a perfect blend - might turn out to be a biased platform.

What I read from this is they have both the skills and the means to provide both a good language and good tooling support, but feel entitled to have your own opinion :)

Collapse
xgrimau profile image
Xavi

Exactly! I've been checking comparisons Java vs Kotlin for the past months and I always feel like they are comparing it to Java 6-7

Collapse
alainvanhout profile image
Alain Van Hout

Coming back to ‘language’ as a metaphor, Kotlin might allow you to build concise sentences, but does not mean that it therefore allows you to better express yourself, and it does present you with a lot more cognitive load. That’s not to say Kotlin doesn’t have its merrits, but it’s a bit annoying that usually only the merrits are mentioned.

Collapse
erajasekar profile image
Rajasekar Elango Author

To me concise code always reduces the cognitive load especially during reading. You make an interesting point, but an example would help to better understand.

For e.g a code that is more concise in Kotlin but requires more cognitive load and same thing implemented in another language which is very expressive with a less cognitive load.

Collapse
alainvanhout profile image
Alain Van Hout

The only thing that really reduces cognitive load is less logic, while 'concise' very often tends to be an alias for 'dense', which is perhaps fun to write but terrible when it comes to maintainability. As to an example, Kotlin builds on top of java (bytecode) and as such adds concepts, which in turn add cognitive load, unless the new concepts can entirely replace more complicated old concepts (a good example of that is fat arrow functions in JS, which avoid the 'this' confusion).

As a very easy example of how extended vocabulary only adds cognitive load, I'm going to going to borrow from Ruby (sorry, guys): array.second requires you to know about and keep in mind the different variations that exist (and don't exist) in this form, while array[x] can do exactly the same at a fraction of the vocabulary.

I do agree that extra vocabulary can be beneficial, but legion people have a proclivity to extol the efficacy and delectableness of a prodigious and uncurbed patois ... at the cost of clear and straightforward language. If you know what I mean :).

edit: since I forgot to specifically answer the following

same thing implemented in another language which is very expressive with a less cognitive load.

keep in mind that it's not just a matter of implementing with less code. It also relates to having to know about the syntax concepts. There are a fair number of languages that can be very expressive indeed, if you know lots of the concepts of that language (a monad is a monoid in the category of endofunctors, and so on and so forth).

Thread Thread
stevenbruno profile image
Steven Bruno

legion people have a proclivity to extol the efficacy and delectableness of a prodigious and uncurbed patois

I know some of these words

Thread Thread
peiche profile image
Paul

Alain be like /r/iamverysmart

Thread Thread
alainvanhout profile image
Alain Van Hout

If that's what you got from that purposely difficult sentence then great. But using a thesaurus works just as well though ;)

Collapse
eljayadobe profile image
Eljay-Adobe

JVM platform has a lot of top-shelf language choices.

Java is a lot like C#. Java is longer in the tooth, and its stewards have been much less attentive compared to the stewards of C#.

Kotlin is a lot like Swift. However, the one downside to Kotlin in the JVM ecosystem is the developer still has to be aware of the Java peccadillos or be puzzled by otherwise surprising behavior. (Analogously, Swift devs still need to have some familiarity with Objective-C to understand some of Cocoa's peculiarities in a Swift world.)

Clojure is Lisp with Functional Programming added in. If you like Lisp, I'd think you'd find Clojure to be the perfect choice for JVM. I'm not sufficiently familiar with Clojure to contrast it to super-focused functional programming language Haskell, or "FP first" functional programming language F#. I am sufficiently versed in Lisp and Scheme to have due respect for the top-of-the-food-chain languages in which Clojure belongs.

Scala is an object-oriented language seasoned with functional programming. (For those Scala fans that may find my description to be flipflopped, I suggest you take a few months to learn Haskell or F#.)

Groovy is a kinder-gentler-friendly less fussy syntax flavor of Java. Groovy is to Java, as CoffeeScript is to JavaScript. However, the creator of Groovy jumped ship as soon as he saw Scala... so if you are leaning towards Groovy, you may want to seriously test the Scala waters before committing to Groovy. (In .NET land, Boo! is analogous to Groovy... except Groovy is a top-shelve JVM language, whereas .NET only has three top-shelf languages: VB.NET, C#, and F#.)

Collapse
maheshkay profile image
Mahesh K

Java and JVM is no longer needed to be honest. Web works just better without Java. I don't know about legacy projects. But my apps won't be running on java be it desktop or mobile. I just want to get rid of that dinosaurs. Same with .Net. It's too much for little projects.

Collapse
jason_espin profile image
Jason Espin

Little projects? As a .Net developer who has developed for some of the major financial institutions in the world I can assure you .Net is still very much at the top of the list.

Collapse
baso53 profile image
Sebastijan Grabar

Use Kotlin, because compiling Java doesn't take enough time already.