News for hackers. Stuff that matters.
Re:Java 18 / JDK 18: General Availability by ape4 on Tuesday March 22, 2022 @11:18AM (#30767120)
Java is great, but here's a surprise:

    if (str1 == str2) {
       // oops
    }
Re:Java 18 / JDK 18: General Availability by junon on Tuesday March 22, 2022 @11:21AM (#30767159)
If you actually understand Java, you'll understand why this is wrong. Only newbies have confusion about this, most of which dissipates when you actually learn the language you're working with. Tired of this 20+ year old "flaw" being used as cannon fodder.
Re:Java 18 / JDK 18: General Availability by dtech on Tuesday March 22, 2022 @11:53AM (#30767601)
Just because something is a well known-flaw doesn't make it a glaring flaw.

There's a reason newer JVM languages like Kotlin use == as an alias for equals instead of reference equality.

Re:Java 18 / JDK 18: General Availability by marginalia_nu on Tuesday March 22, 2022 @12:29PM (#30768147)
What if you are interested in reference equality?
Re:Java 18 / JDK 18: General Availability by __float on Tuesday March 22, 2022 @12:39PM (#30768292)
In practice, how often are you interested in it? C# has `Object.referenceEquals()` because you _may_ want this sometimes. But to have reference equality be the default for something like `==` is a design flaw. Not one that can be fixed now, of course, but it will continue to cause bugs for years to come.

Sometimes, it happens in core Java libraries themselves: https://bugs.openjdk.java.net/browse/JDK-8274779

https://github.com/openjdk/jdk/blob/722d639fad2e4fc6eb2aabd4...

Re:Java 18 / JDK 18: General Availability by marginalia_nu on Tuesday March 22, 2022 @12:53PM (#30768500)
Dunno, I do a fair bit of low level java work, happens every once in a while.

Curiously, I don't do a lot of value-comparisons. Based on grepping in the repo for my search engine, I appear to do one every 1000 lines of code. May just be how I write code though. Some people seem to do that a lot more.

Re:Java 18 / JDK 18: General Availability by LordDragonfang on Tuesday March 22, 2022 @12:50PM (#30768458)
Python uses the `is` operator, which is semantically much more explicit (and arguably correct, though C#'s use of the `is` operator is probably more correct).

edit:

Kotlin uses `===`, which is another good option.

Re:Java 18 / JDK 18: General Availability by marginalia_nu on Tuesday March 22, 2022 @01:01PM (#30768619)
=== seems almost more dangerous. It's a lot easier to tell == from Objects.equals(a,b) than == from ===.
Re:Java 18 / JDK 18: General Availability by wiseowise on Tuesday March 22, 2022 @03:37PM (#30770486)
Seriously?
Re:Java 18 / JDK 18: General Availability by marginalia_nu on Tuesday March 22, 2022 @05:06PM (#30771553)
Yes.

Look, languages are moving away from permitting constructs like "if (x = 1)" because it's difficult to distinguish from "if (x == 1)". Adding "if (x === 1)" into the mix is arguably a step backwards in terms of clarity.

Re:Java 18 / JDK 18: General Availability by Banana699 on Tuesday March 22, 2022 @04:06PM (#30770835)
This is like saying that null references are a non-issue because it have been there since the 1970s and that "true" $LANGUAGE developers never collide head-first into invalid dereferences. If something is ridiculous and un-ergonomic, it stays ridiculous and un-ergonomic no matter what. Reference comparison by default is ridiculous and un-ergonomic.
Re:Java 18 / JDK 18: General Availability by junon on Wednesday March 23, 2022 @07:14AM (#30777246)
Null references exist in every language with the concept of null. It has nothing to do with Java specifically, and Java gives you a lot of information to debug them.
Re:Java 18 / JDK 18: General Availability by jraph on Wednesday March 23, 2022 @01:43PM (#30781156)
That's right, but that also doesn't answer the parent's point to which I fully agree.
Re:Java 18 / JDK 18: General Availability by MBCook on Tuesday March 22, 2022 @12:14PM (#30767903)
For what it’s worth any decent IDE will warn you about that. But as a Java developer you know it’s wrong, Strings are objects and that’s not how you compare objects.

The tricky one is numbers.

Integer == int // fine

int == int // fine

numVarOne == numVarTwo // Uh-oh

In that third example you better make sure they aren’t both boxed objects or BigDecimals.

I’ve almost never seen the string one for some reason. It’s always boxed numbers.

Again, a good IDE is worth a ton.

Re:Java 18 / JDK 18: General Availability by vips7L on Tuesday March 22, 2022 @03:05PM (#30770107)
Integer == Integer actually works... sometimes, as long as you're within the Integer cache. It gets people all the time though.
Re:Java 18 / JDK 18: General Availability by MBCook on Tuesday March 22, 2022 @03:24PM (#30770329)
Yep. I’ve been bitten by that myself. It’s sneaky because it may pass all your unit tests because you’re using small numbers. Then it gets to prod with record number 2718 and kablooie.
Re:Java 18 / JDK 18: General Availability by UncleMeat on Tuesday March 22, 2022 @11:24AM (#30767203)
It is true that C# improved this. Separating primitives and references in Java was definitely an error and it cannot be fixed at this point.

The good news is that a linter trivially detects this bug.

Re:Java 18 / JDK 18: General Availability by msgilligan on Tuesday March 22, 2022 @12:23PM (#30768062)
Actually Project Valhalla is going to fix (well at least improve) this:

https://openjdk.java.net/projects/valhalla/

Re:Java 18 / JDK 18: General Availability by Cthulhu_ on Tuesday March 22, 2022 @11:56AM (#30767629)
Only if you skip all the tutorials and don't use an editor that warns you about it. Which is another selling point for Java; it has the best editor support and quality assurance tools out there. It took years for e.g. Javascript to catch up and it's still a far cry from a proper typed language with tooling.

I mean by that same logic, Javascript will confuse you as well because `{} != {}`, or C++ because of pointer mechanisms and custom operators.

Re:Java 18 / JDK 18: General Availability by Banana699 on Tuesday March 22, 2022 @04:16PM (#30770947)
It's really says a lot that you have to compare with JS or Cpp so that Java comes out on top. Those are 2 extremely flawed languages that are the butts of cliché jokes well-known across every programming circle, their history alone has "HACK" written all over it.
Re:Java 18 / JDK 18: General Availability by jraph on Tuesday March 22, 2022 @11:19AM (#30767139)
Yeah, I hate that about Java.
Re:Java 18 / JDK 18: General Availability by marginalia_nu on Tuesday March 22, 2022 @12:14PM (#30767917)
Eh, I'll take that over <https://dorey.github.io/JavaScript-Equality-Table/> any day.

Boolean.getBoolean, however, now that is a proper Java land mine.

Re:Java 18 / JDK 18: General Availability by azth on Tuesday March 22, 2022 @01:55PM (#30769273)
In addition to what the other posters replied, the IDE will warn you about this.
Re:Java 18 / JDK 18: General Availability by darkhorn on Tuesday March 22, 2022 @07:23PM (#30772915)
Another surprise is it that it has issues on Turkish localized Windows operating systems.
Re:Java 18 / JDK 18: General Availability by kaba0 on Wednesday March 23, 2022 @03:02AM (#30775782)
What do you mean? Can you give a source where I can read more on that?
Re:Java 18 / JDK 18: General Availability by kaba0 on Wednesday March 23, 2022 @06:03PM (#30784072)
Well, I think it’s just the usual gotcha of programmers assuming things about languages that are not true.

Though I’m not sure why wasn’t two new i’s added instead of modifying the latin i? It is a different letter after all in this case.

Re:Java 18 / JDK 18: General Availability by dpratt on Tuesday March 22, 2022 @11:19AM (#30767135)
That’s not an oops. You’re comparing pointers.
Re:Java 18 / JDK 18: General Availability by jraph on Tuesday March 22, 2022 @11:21AM (#30767158)
Still, coming from other languages, it is all too easy to write this and this silently fails without any warning.

This is consistent with the rest of the language, but I'd bet one rarely wants referential equality when comparing strings.

People commenting "yeah but that's because you are a newbie / you are comparing references and you should know better" miss the point I think. I can easily see myself making this mistake while fully understanding what's going on.

Re:Java 18 / JDK 18: General Availability by pritambarhate on Tuesday March 22, 2022 @11:29AM (#30767273)
Linters catch this: https://rules.sonarsource.com/java/RSPEC-4973

Basically the tooling around Java is very evolved and Java static type system helps the IDEs a lot. One of the main reason enterprises prefer Java. Also majority of the tools and IDEs is free for commercial use.

Re:Java 18 / JDK 18: General Availability by __float on Tuesday March 22, 2022 @12:41PM (#30768316)
Re:Java 18 / JDK 18: General Availability by nsxwolf on Tuesday March 22, 2022 @04:54PM (#30771402)
I learned this 25 years ago when I got started with the language. It tripped me up once, I learned about how references work in Java, and that was that.

We are expected to learn about our tools.

Re:Java 18 / JDK 18: General Availability by jraph on Tuesday March 22, 2022 @07:19PM (#30772874)
> We are expected to learn about our tools.

I hope making mistakes is ok though?

I learnt C years before Java, it's also the first language I properly learnt. And a little bit of C++ too, so I've known the concepts of pointers and references way before learning Java. In C, you also either do pointer equality or explicit value equality (with strcmp) for string equality tests. I never had any issue understanding how things work in Java. It's not about learning anything. It's about doing mistakes. And I won't take "you should know better" as a counter argument to a mention of some rough edge in an API or a programming language. It's like Javascript: you might know the differences between == and === in Javascript full well, but still happen to write == by mistake because you just wrote some C/Java/Python code just before. I'll probably not forget about .equals(...) in Java most of the time but the fact that == would be silently accepted and not do what is wanted is still a bit concerning (and .equals(...) is ugly, too).

A linter will do indeed and that's a fair point. I'd argue that linters are there to work around rough edges of programming languages, but that's fair enough too, every programming language has its gotchas and I'll accept considering a programming language + its linter(s) instead of a programming language alone. However, that's still not ideal because you might have to work on non linted code and deal with these gotchas without any safety net.

I hadn't thought about number comparisons (mentioned in other comments) but that's even worse indeed.

It's no wonder Kotlin and Groovy both chose value comparison for ==.

Re:Java 18 / JDK 18: General Availability by jrsj on Tuesday March 22, 2022 @11:29AM (#30767270)
Most editors will warn you about this now as soon as you type it
Re:Java 18 / JDK 18: General Availability by dtech on Tuesday March 22, 2022 @11:54AM (#30767606)
Reference equality being the default instead of something you very explicitly have to ask for is a flaw.
Re:Java 18 / JDK 18: General Availability by nkzd on Tuesday March 22, 2022 @11:39AM (#30767429)
Really? You are only "confused" by this if you are very new to programming in general.