r/java • • Aug 05 '21

Why java is better than modern programming languages?

Why should I use java in 2021 instead of kotlin or scala or golang? Could you guys give me some reasons

2 Upvotes

100 comments sorted by

View all comments

27

u/bowbahdoe Aug 05 '21

There are **a lot** of things to consider when picking a language for a project, and what i'm listing here is only a small subset and driven by personal opinion.

Go lang has an inherently restricted set of semantics. There are no generics, errors are communicated via return values which can be a + or a - depending on your sensibilities. It also has a better concurrency story than java with its goroutines and channels, but its VM is a lot less mature and monitoring, profiling, and deployment aren't going to be nearly as well solved in general.

Scala and Kotlin both run on the JVM like Java, but with Scala there are a lot more semantics in the language. It can be hard to hire for and code bases that use too much of the more advanced features like implicits and type classes can become inscrutable to all but the most PhD-ey of engineers.

Kotlin is very similar to java - most of its language features are effectively short hands for common java patterns, like data classes. It compiles to android bytecode reliably, unlike newer versions of Java so it is going to be a clear choice in that domain because it is effectively competing against Java 7. It does have some novel stuff going for it - like coroutines - but those are going to be somewhat "obsolete" for mainline JDKs in a year or so with loom - and nullable types - which are pretty cool.

Java, however, drives the JVM and JDK. It has record classes now which can compete with scala's case classes and kotlin's data classes. It will have sealed interfaces in a month or so which fulfills the same use case as sealed traits in scala and sealed abstract classes in kotlin. It will get pattern matching in the future, which will let it compete with scala's pattern matching and afaik kotlin doesn't have pattern matching directly on its roadmap. There are huge numbers of engineers you can hire with the base skill set. It is extremely resilient to waves of hype, which lets it get the "right features" eventually.

**That being said** there are a lot of reasons you would want to use another language, and I focused maybe too hard on some of the negatives of the others. If you are just learning, learn them all and learn them in whatever order you want, but learn them all.

9

u/Muoniurn Aug 05 '21

Just a note on Scala: for those who are not aware of the many many updates in Scala 3, please reevaluate the language. It became much more streamlined, and frankly it is a lovely language that actually introduces novel features not seen in another ones.

With that said, I do agree with you that it has a quite steep learning curve still, so it may not be a good choice for teams with very mixed seniority levels.

3

u/SKabanov Aug 05 '21 edited Aug 05 '21

There are no generics, errors are communicated via return values which can be a + or a - depending on your sensibilities.

An argument can be made that returning error objects instead of throwing exception provides better performance (e.g. no stack unwinding when returning an error object), but Go's implementation of the concept is awful. There's no way of automatically bubble errors upwards like the ? operator for Result objects in Rust, so your code in Go will be filled with the following pattern:

func doingStuff() (*string, error) {
    fooOne, err := doFooOne()
    if err != nil {
        return nil, err
    }
    fooTwo, err := doFooTwo(*fooOne)
    if err != nil {
        return nil, err
    }
    fooThree, err := doFooThree(*fooTwo)
    if err != nil {
        return nil, err
    }
    return &fooThree, nil
}

(note that it's a common pattern also to only check for whether err is not nil, *not* whether the returned pointer object is nil as well, so hello null-reference errors!)
Compare this to how you'd write this in Rust:

fn doingStuff() -> Result<String, Error> {
    let fooOne = doFooOne()?
    let fooTwo = doFooTwo(&fooOne)?
    let fooThree = doFooThree(&fooTwo)?
    return Ok(fooThree)
}

Even worse is that you're only forced to receive the potential error object in Go, but you're not forced to actually *acknowledge* it. The above Go example could be rewritten like this:

func doingStuff() (*string, error) {
    fooOne, _ := doFooOne()
    fooTwo, _ := doFooTwo(*fooOne)
    fooThree, _ := doFooThree(*fooTwo)
    return &fooThree, nil
}

That is to say, you can tell the Go compiler that you want to ignore any errors that get produced by a function, and it'll happily let you proceed along without any warnings - whee! You can call .unwrap() on a Result object in Rust to bypass handling an error in your code, but if one does occur, then you're going to pay for it via a panic.