5 ms·
Java is great, but here's a surprise: if (str1 == str2) { // oops }
by ape4 5y ago
Java is great, but here's a surprise:
if (str1 == str2) {
// oops
}
- dpratt 5y agoThat’s not an oops. You’re comparing pointers.
- jraph 5y agoStill, 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.
- jrsj 5y agoMost editors will warn you about this now as soon as you type it
- pritambarhate 5y agoLinters catch this: https://rules.sonarsource.com/java/RSPEC-4973 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.
- __float 5y agoIt's easy to make this mistake, still: https://github.com/openjdk/jdk/blob/722d639fad2e4fc6eb2aabd427e2719501899cfe/src/java.base/share/classes/sun/net/www/http/HttpClient.java#L310 https://github.com/openjdk/jdk/blob/722d639fad2e4fc6eb2aabd4...
- nsxwolf 5y agoI 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.
- jraph 5y ago> 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 ==.
- dtech 5y agoReference equality being the default instead of something you very explicitly have to ask for is a flaw.
- jraph 5y agoYeah, I hate that about Java.
- junon 5y agoIf 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.
- dtech 5y agoJust 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.
- marginalia_nu 5y agoWhat if you are interested in reference equality?
- __float 5y agoIn 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://bugs.openjdk.java.net/browse/JDK-8274779 https://github.com/openjdk/jdk/blob/722d639fad2e4fc6eb2aabd427e2719501899cfe/src/java.base/share/classes/sun/net/www/http/HttpClient.java#L310 https://github.com/openjdk/jdk/blob/722d639fad2e4fc6eb2aabd4...
- marginalia_nu 5y agoDunno, 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.
- 5y ago
- UncleMeat 5y agoIt 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.
- msgilligan 5y agoActually Project Valhalla is going to fix (well at least improve) this: https://openjdk.java.net/projects/valhalla/ https://openjdk.java.net/projects/valhalla/
- nkzd 5y agoReally? You are only "confused" by this if you are very new to programming in general.
- Cthulhu_ 5y agoOnly 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.
- Banana699 5y agoIt'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.
- MBCook 5y agoFor 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.
- vips7L 5y agoInteger == Integer actually works... sometimes, as long as you're within the Integer cache. It gets people all the time though.
- MBCook 5y agoYep. 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.
- marginalia_nu 5y agoEh, I'll take that over <https://dorey.github.io/JavaScript-Equality-Table/ https://dorey.github.io/JavaScript-Equality-Table/> any day. Boolean.getBoolean, however, now that is a proper Java land mine.
- azth 5y agoIn addition to what the other posters replied, the IDE will warn you about this.
- darkhorn 5y agoAnother surprise is it that it has issues on Turkish localized Windows operating systems.
- kaba0 5y agoWhat do you mean? Can you give a source where I can read more on that?
- darkhorn 5y agohttp://haacked.com/archive/2012/07/05/turkish-i-problem-and-why-you-should-care.aspx/ http://haacked.com/archive/2012/07/05/turkish-i-problem-and-... http://www.i18nguy.com/unicode/turkish-i18n.html http://www.i18nguy.com/unicode/turkish-i18n.html https://community.jaspersoft.com/questions/1065746/jasperreports-server-640-installation-issue-turkish-windows https://community.jaspersoft.com/questions/1065746/jasperrep... https://userapps.support.sap.com/sap/support/knowledge/en/1493845 https://userapps.support.sap.com/sap/support/knowledge/en/14... https://github.com/java-native/jssc/issues/80 https://github.com/java-native/jssc/issues/80 https://issues.apache.org/jira/plugins/servlet/mobile#issue/VELOCITY-908 https://issues.apache.org/jira/plugins/servlet/mobile#issue/...
- kaba0 5y agoWell, 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.