top | item 45552587

(no title)

sedro | 4 months ago

Autoboxing's evil twin, auto-unboxing should knock the score down a few points.

  Integer a = null;
  int b = 42;
  if (a == b) {} // throws NullPointerException

discuss

order

dcminter|4 months ago

Or my favourite...

  Short w = 42;
  Short x = 42;
  out.println(w == x); // true
  Short y = 1042;
  Short z = 1042;
  out.println(y == z); // false

prein|4 months ago

Once, after we had an application go live, we started getting reports after a few hours that new users were unable to log in.

It turns out, somewhere in the auth path, a dev had used `==` to verify a user's ID, which worked for Longs under (I believe) 128, so any users with an ID bigger than that were unable to log in due to the comparison failing.

kittko|4 months ago

I’ll bite. Why does this not work as you’d expect?

sedro|4 months ago

That's another gotcha-- interning of strings and boxed primitives.

Are there linters for this sort of thing? I don't write Java much any more.

hashmash|4 months ago

If JEP 401 is ever delivered (Value Classes and Objects), then this sort of problem should go away.

commandersaki|4 months ago

I'm not much of a PL nerd, what should it do?

blandflakes|4 months ago

Return false! They aren't equal. But of course we're comparing a reference to a primitive, so we either lift the primitive to a reference, or lower the reference... so here we are.

mrkeen|4 months ago

Fail to compile when you assign something which isn't an integer to an integer.

thiht|4 months ago

Seems reasonable to me