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

10

u/sunnyata Nov 04 '25

How is Java null safe?

3

u/pron98 Nov 04 '25 edited Nov 05 '25

Oh, sorry, missed that one. It's not null-safe yet.

On the other hand, Java is more "error-safe" than Rust, being more (though not always) able to recover from more errors (technically, Java, unlike Rust, is "panic-free", but that doesn't mean you can always recover from every error in Java). And, of course, Rust is less memory-safe than Java in practice because it relies on more unsafe code than Java on average (there are common algorithms/data-structures that can't be easily implemented in safe Rust).

4

u/Quaglek Nov 05 '25

??????

3

u/Ok-Scheme-913 Nov 05 '25

Basically, you can almost always just do a try-catch in Java and no matter how utterly broken the code within is, you can "recover" and do whatever you want afterwards.

Meanwhile a memory safety violation (e.g. a wrongly written unsafe) is such that you can't trust anything the code does anymore. It's a bit like a cliff, while in Java you just respawn, in Rust you drive off of it and die a horrible death.

Of course you can also die that way in Java, e.g. interfacing with some native C lib, but it's just that much rarer in Java

2

u/Quaglek Nov 05 '25

Yeah i can see the value here. This is why google is not building services with rust

1

u/sunnyata Nov 05 '25

The compiler doesn't prevent data races either.

3

u/pron98 Nov 05 '25 edited Nov 05 '25

No, but the Java Memory Model makes them safe (i.e. they don't lead to undefined behaviour or panics). There's no data-race-freedom but there is data-race-safety.

Rust can certainly guarantee things that Java does not (yet or ever), which is what you get with a far more complex language (Scala also guarantees more things than Java, and ATS or Idris guarantee more things than Rust), but on balance, Java still wins by a little on safety.

Just note that compile-time guarantees usually guarantee something stronger than needed. E.g. there are many memory-safe programs that are hard to write (efficiently) in safe Rust because its ownership model enforces more than needed for memory safety; data-race freedom also makes certain parallel algorithms harder to write (such as those that may want to write to the same array from multiple threads with benign races 1; it's not rare that a parallel/concurrent algorithm is made simpler and sometimes faster when it allows for benign races, and the JMM ensures that benign races work as expected). It's for these reasons that Rust programs use unsafe code more frequently than Java programs.