47
cblurry
7y

So I'm cleaning up some buggy Java code when suddenly this dandy fine line appears:
if (new Double(a).toString() == "1.3") ...

Someone got paid for this :|

Comments
  • 4
    Oh man, that's bad
  • 3
    If it's Java, that won't even work. You need String.equals
  • 0
    Seeing this hurts more than it should
  • 1
    Seems like someone didn't know he should put a D after doubles in java
  • 4
    @AlgoRythm I know that in JS, there’d be some iffy type conversion to at least try to solve the comparison. So it change the type of the rand hand expression to the type of the left (so a String object). Since it’s using “==“ instead of .equals() as you observed, it’d be comparing if the objects are the same, that is, if they point to the same location in the heap. Since one is a literal, and one is actually declared at that very moment, they probably don’t point to the same location.

    So it’d be false. I’m not sure if Java does this though.
  • 1
    Dafuq
  • 2
    @enoon it does. So yeah, the condition is syntactically correct. It's also always false as it compares refs rather than values
  • 0
    Did he/she want to know if a == 1.3? 😶
  • 0
    hahahahaha.... Dead!
  • 0
    @AlgoRythm as has already been mentioned, this will definitely work. It will just always be false ;)
Add Comment