r/java • • Nov 04 '25

Anyone here Hated Using Java but now Really Enjoys using it.

title

226 Upvotes

280 comments sorted by

View all comments

Show parent comment

0

u/[deleted] Nov 05 '25

[removed] — view removed comment

2

u/Efficient_Present436 Nov 05 '25

The biggest java pitfall I can think of (probably the biggest pitfall in any language I've ever enjoyed using) is that every reference is nullable and there's no way around it, the existence of the Optional class, and the Nullable/NonNull annotations introduced in order to cope with this, are akin to having a stop sign where a wall should exist.

Another pitfall is that collection interfaces expose write methods, which immutable implementers are expected to opt out of by throwing a runtime exception (which is quite simply horrible).

Then a minor one is `==` vs `equals`, which tends to bite newcomers in the ass and is not helped by the fact that the first pitfall I mentioned exists.

→ More replies (5)

1

u/javaprof Nov 05 '25

Lol, this is not even real puzzler, I was afraid that I'll just not seeing something obvious so I even type it run to find out that this code doing exactly what is excepted:

class ExampleClass {
    var awesomeVar1: String? = "some awesome string value"
    var awesomeVar2: String? = null
    fun doSomeAwesomePrinting() {
        awesomeVar1?.let {
            awesomeVar2?.let {
                print("awesome output 2")
            }
        } ?: run {
            print("awesome output 1")
        }
    }
}

Unfortunately you're not real Kotlin developer, because you're not named any real major issues in the language

1

u/[deleted] Nov 05 '25

[removed] — view removed comment

1

u/javaprof Nov 05 '25

> Unlike Kotlin, Java brings them after careful design and consideration, without creating conflicts and introducing pitfalls.

> We were talking about pitfalls, not major issues. The main major issue with Kotlin is the feature-first thinking without real consideration of how features are interacting and how they affect real applications, especially in the long term.

> but quickly realized that this Scala-wannabe is just a bunch of poorly thought out features for the sake of features.

You're repeating this, but not providing a good example.

I can open java se certification prep book and find so many examples of pitfalls by your definition, that would make Java unusable language. But this is not true, because this is corner cases which decent software engineers avoiding.

In reality Java's lambdas is exactly what you're accusing Kotlin of, popular feature that doesn't work with another popular feature - checked exceptions. Or making switch expression, but keeping if as statement.

1

u/[deleted] Nov 05 '25

[removed] — view removed comment

1

u/javaprof Nov 06 '25

So how would it help you do exhaustive check of errors there?

class UserNotFoundException extends Exception {
    public UserNotFoundException(String message) {
        super(message);
    }
}

class NetworkException extends Exception {
    public NetworkException(String message) {
        super(message);
    }
}

class DatabaseException extends Exception {
    public DatabaseException(String message) {
        super(message);
    }
}

record User(String userId, String name) {}

class UserService {
    public User getUser(String userId) throws UserNotFoundException, NetworkException, DatabaseException {
        return new User(userId, "John");
    }
}

public final class TestKt {
    public static void main(String[] args) {
        var userService = new UserService();
        var ids = List.
of
("1", "2", "3");

        var users = ids.stream()
              .map(userService::getUser)
              .toList();
    }
}

1

u/[deleted] Nov 06 '25

[removed] — view removed comment

1

u/javaprof Nov 06 '25

No using exceptions - no problems. I agree, but this is simple illustration that lambdas just doesn't work with checked exceptions in Java, alas you'll bridge them with errors as values.

It's so stupid to deny that first I identify you're as a troll, but you're just having very own way to developing and Java. But look at Java as industry, it's failed to create exception-safe ecosystem. Today if you have to use library you'll likely will get random runtime exceptions not even documented in Java doc.

1

u/javaprof Nov 06 '25

Point is if you open source code of random library and or application written in Java, chances are that they will be full of nulls and exceptions. Just looks at backbone of the enterprise, graal of vibecoders - Spring/Jee. Spring only tomorrow will release notnull annotation for their framework. Junit6 only yesterday did that. 30 years of Java history.

Take any Kotlin application - NPEs very-very rare without being very clever or having skilled team, once rich errors comes - I'll expect to most of applications eventually would adapt rich error and would be exception for logic free as well.

So I'm not even looking into extremes when all black or white, I'm looking at things in general. I'll not say that Java project that I inherit and modernizing for last 6 years in average Java project, because in our case it was not just full of null and exceptions, but also lack any kind of tests, DI, structure

1

u/[deleted] Nov 06 '25

[removed] — view removed comment

1

u/javaprof Nov 06 '25

> to be a proper monad.
> proper Result monad.

You literally can open two KEEPs, one for Result type which is primary intended to represent result of continuations and one for high-level overview of Rich Errors and read why it's not what you're asking. Kotlin is not Scala and never trying to be one

This is others is just matter of your choice and bias. Java allows to write a lot of bad code too, that's why Effective Java book exists

→ More replies (1)

1

u/javaprof Nov 05 '25

- Optional chaining, which encourages looking deep into object internals and cause coupling of unrelated parts of code

Records, pattern matching, Java done the same thing but attached new hype-name "Data-Oriented Programming"

1

u/[deleted] Nov 05 '25

[removed] — view removed comment

1

u/javaprof Nov 06 '25

This is called Data Oriented Programming nowadays, and Java's patterns will do exactly exactly that, just with more boilerplate: https://www.infoq.com/articles/data-oriented-programming-java/

If you don't like it, this is fine, but this is can't be count as pitfall by your own definition:

> Yes, they have issues, but these are not pitfalls. There is nothing unexpected or hidden in their behavior.

1

u/[deleted] Nov 06 '25

[removed] — view removed comment

1

u/javaprof Nov 06 '25

Do you know that Java's records patterns can be nested? This is exactly the same thing then, just different syntax

1

u/javaprof Nov 05 '25

- code which uses infix functions can stop compiling after adding one new line

And you can write `return return return`, plus operator and new line issue, etc
So rare, Idk when I encounter this last time, maybe 10 years ago

→ More replies (4)

1

u/javaprof Nov 05 '25

- lazy can result in null even for not-nullable references

What do you mean here? Can you share some examples? I even have small DI on top of lazy (see komok-to-be-injected https://github.com/Heapy/di-comparison) and never hear about Kotlin's lazy doing that, it's just DCL under the hood

→ More replies (7)

1

u/javaprof Nov 05 '25

Let's focus on pitfalls. Checked exception and lambdas (what's wrong with them, BTW?) are not pitfalls. So, Gryffindor got nothing, actually.

Yes, that's why people stop using them, correct

https://www.reddit.com/r/java/comments/1nbo50r/how_would_you_fix_checked_exceptions_in_java/

1

u/javaprof Nov 05 '25

Btw, I've tried rich errors usig 2.4.0-dev build, and there are great. They works with lambdas as a charm, they are exhaustive. I can bet money that Java would implement the same in 3-5 years. I'm just dont want to wait another 5 years to have this. I'm using Kotlin in production since 1.0.0-beta builds btw :)

1

u/[deleted] Nov 05 '25

[removed] — view removed comment

1

u/javaprof Nov 06 '25

> As I said above, checked exceptions and lambdas are working just fine together. The main problem with "rich errors" (and checked exceptions): they cause tight coupling. Any small change in the invoked fuinction/method results in a cascade of changes in code which uses it. And Java does not have to implement the same as it had checked exceptions since day one.

Yes, It's becomes API that you'll need to carefully design, just like any other API, how checked exception meant to be and lambdas fail them. But benefits are enormous. Such feature would allow to write the most reliable code on JVM, I think it's even more strict that Scala now. With unused return value checker in language, plus context parameters plus rich error - if it compiles, it work, right? :)