r/java • • Dec 29 '21

Why everyone hates Java?

I dont understand why java is one of the most dreaded lenguages. Java got fantastics frameworks and libraries to work with it. I dont know if im skipping something or I dont work enough with Java because I like java. What do you think??

Here is the Stack Overflow Survey

268 Upvotes

367 comments sorted by

View all comments

33

u/atpeters Dec 30 '21 edited Dec 30 '21

The four main complaints I've heard from some people are:

  1. Generics

  2. Null

  3. It's not functional

  4. Boilerplate

These are people that want to work with closure, erlang, Haskell, etc instead.

Personally I don't mind Java much except for working with JSON due to generics and cast checking. Admittedly I'm stuck in JDK 8 and I don't know if that has been improved upon.

0

u/[deleted] Dec 30 '21

[deleted]

16

u/fufucum Dec 30 '21

Agreed, plus who cares about boilerplate code? always use auto-generation, don't write getter setter by your hand,

7

u/monbis Dec 30 '21

I kind of agree, but there are two issues I see with boilerplate:

  1. Changes need to be done in several places, some of which your IDE may do for you, but sometimes you need to do

  2. More visual and cognitive clutter when reading the code

4

u/MaName678 Dec 30 '21

Or use Lombok <3

7

u/devcexx Dec 30 '21

Yes, the solution for a language is to let a third-party library to do what the compiler can't by hacking into each different commercial Java compiler and forcibly modify the AST of each file in your project because of the lack of an external interface for doing so, yes seems reasonable.

3

u/MaName678 Dec 30 '21

Chill, Java isnt perfect and neither is Lombok as a solution, but sometimes you must work with whats given XD.

Edit: BTW, I love the way C# handles this getter and setter matter

4

u/devcexx Dec 30 '21

Yeah ik, sorry I was not attempting to be aggressive, just adding some irony <3.

→ More replies (1)

4

u/ivancea Dec 30 '21

Comparing Java with JS or Python is like comparing oranges with lemons.

If you compare it with something similar, like C#, you see you have a lot of flexibility, and those problems doesn't exist. And it's also a top used language

0

u/[deleted] Dec 30 '21

[deleted]

4

u/ivancea Dec 30 '21

And Java and JS are languages. But little people eat lemons

Edit: little people => few people. I don't know what little people eats 😬

3

u/m02ph3u5 Dec 30 '21

We eat little food

-1

u/[deleted] Dec 30 '21

Kotlin adds so much boilerplate compared to java 😱

0

u/[deleted] Dec 30 '21

[deleted]

→ More replies (11)

9

u/morhp Dec 30 '21

Kotlin in my experience makes things shorter that shouldn't be short and makes things longer that shouldn't be long.

Like for example null should be scary and rarely/carefully used. I don't want careless programmers to plop ?. and ?: everywhere. If some value is null, you usually should handle that probably and create an error or whatever, Kotlin makes it way too easy to ignore it or use some default value instead.

On the other hand, writing down static hex constants like 0xFFFF_0000_0000_0000L is a huge pain in Kotlin, as are what would be static fields in Java.

(There are of course many things I like about Kotlin, but I question some design decisions)

6

u/[deleted] Dec 30 '21 edited Dec 30 '21

well yeah... this is the good part of kotlin... having the ? operator and ?. is much better than using Optional but that also makes me very angry when I know the field will be initialised later and now I have to declare everything with lateinit which turns the clean Java code private Car myCar; into this mess private lateinit val myCarr:Car; I know that the field will be initialised a bit later and cannot be null so I don't make it Optional by default but going with kotlin I have to add all that lateinit val/var everywhere and I every dev that reads this, now has to open up the kotlin docs to spend 30-60 minutes reading about lateinit, when to use it and how it works in relation with constructors, accessors, lazy function ...

When I want for everybody to know that a field could be null, I annotate it with @Nullable and that's it! the IDE/linter will tell you you're stupid when you access that field without a check

Also my guideline is: if is not annotated with @Nullable or Optional it cannot be null which means someone didn't initialised it in the first place where it should have been so the fix time is about 1-5 minutes at top

0

u/[deleted] Dec 30 '21

[deleted]

→ More replies (4)

2

u/devcexx Dec 30 '21

I don't want careless programmers to plop

?. and ?: everywhere

With great power comes great responsibility. If you feel the need of using ?. and ?: constantly in a Kotlin project just for silently the compiler warnings, that means that you're coding in Kotlin like you were doing Java, which is not good. The good thing about the safe nullability of Kotlin is that it allows you to code under the assumption that everything won't be null, or it is marked as such. If you code is full of `?.` or `?:` (which is something I also hate because it usually hides other issues) is because you're missing validation layers that your application needs to work correctly. If you function requires a Potato and requires that all the fields inside the Potato to be not null, make sure all the fields of the Potato are marked as not null and you're validating them correctly before calling the function, instead of filling the function with `?.` or `?:`. That's the whole point of that feature.

3

u/corbymatt Dec 30 '21 edited Dec 30 '21

Kotlin null checks are 'in place' and bound to the instance throughout the scope of the value. You can't use a ?'d kotlin val, for example, without "doing something about it" when you access it (as you rightly said).. however "what you do about it" is not the language's fault, any more than it was in Java of course. Its just far easier to spot the issue in kotlin.

As for the hex issue, it seems that intellij's kotlin code converter would prefer you to express 0xFFFF_0000_0000_0000 as 0x1000000000000L which probably makes more sense..

Likewise, if you declare a private const val outside of the class in the same file, you can effectively declare a constant without using a companion object.

What I do think is a bit awkward still in kotlin:

  1. Scripting. Yikes, imports and shebangs and classpaths oh my

  2. Many ways to do x, like constants (companion object? Object? Private const?)

  3. Java developers doing the same complex nonsense they do in Java because they don't know how to keep things simple.

1

u/[deleted] Dec 30 '21

There is too much idiomatic confusion with Kotlin. By far java is nice and clean for any one to understand

→ More replies (2)

3

u/minato3421 Dec 31 '21

Working with Json in Java is a pain in the ass. I recently had to write a Go service in my current organization and was surprised to see how easy it was to handle Json data in Golang.

3

u/ConstructedNewt Dec 30 '21 edited Dec 30 '21

Records take away so much boilerplate. Sadly classes are still expected to be 1 class per file, so you har some one-line files (or nest them)

Null is a real problem, but, like generics (and pain points on them), it's mostly the designers fault. All though language can promote bad/ good habits

3 is just stupid to complain about. Like, sure... use something else idc

Jackson JSON ObjectMapper to a record type is pretty simple, but hasn't changed much (since java 8) although I'm unsure about Snake case names

Edit: @JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)

4

u/Muoniurn Dec 30 '21

Sadly classes are still expected to be 1 class per file,

That’s only public classes. You can put non-public records outside your public class.

→ More replies (2)

3

u/deinlandel Dec 30 '21

You can just use Kotlin and fix all of these while maintaining full compatibility with an existing codebase and all JVM libraries and frameworks.

That's actually what is great about Java Ecosystem: you are not limited by one language.

→ More replies (1)
→ More replies (10)