Parlor tricks. but it's supposed to be 127 or -128 or the trick shoudln't work.
You can also have fun with reflections and change the value of Integer.valueOf(1) to something else.
Parlor tricks. but it's supposed to be 127 or -128 or the trick shoudln't work.
You can also have fun with reflections and change the value of Integer.valueOf(1) to something else.
@snoofle said:
@DrPepper said:
@snoofle said:To be fair, this is a condensed example. The original code was spread out over ~60 lines, but still, I can't see any good reason why the designers of Auto Boxing didn't design it to auto-upconvert and just do the mathematical comparison.And he didn't see the "long" being used here -- where Integer should have been used. And no one doing a code review caught this? (you are doing code reviews, right?) And the junior developer didn't understand that when something is not working right, that should be, he should ask someone to take a look at the code? At least 3 WTF's here.Map<Integer,Integer> map = new HashMap<Integer,Integer>();
// populate it
...
long theKey = someObject.getTheKeyValue();
Integer theVal = map.get(theKey);
It's one of those syntactic-sugar features that sometimes causes more problems than it solves.
...And I've probably mentioned hundreds of times that nobody here except me does code reviews, and I've been pulled off of that for reasons to be explained in an upcoming front page post.
Consider the following example: Double a = 1.0; Double b = 1.1; Integer c = 1; boolean d = a.equals(c); boolean e = b.equals(c); boolean f = a.equals(b);Should d, e or f evaluate true? There are other worse examples, but I consider the strong type safety of Java one of it's strong points. I'm not so sure about autoboxing though..
The problem is imho that the Map interface has the method V get(Object key) instead of V get(K key). If Java had generics from the start, this would've been one less pitfall, but it still only something a junior Java developer should do.
C# never had any momentum before 2.0, so for MS it was a small cost to break backwards compatibility. For Sun (please don't mention the O-word) it was more important with backwards compatibility. 10 years later it's easy to say that it makes it easier to do a mistake, but I still think they made the right one.
This can't be considered a big issue anyway, since most static code analysis tools will find this one for you. A jenkins server that just compiles the java code and runs some of the open source code analysis tools can be run off any old desktop machine and used to email the developer in case you don't have a code review tool.
Snoofle, I've worked for at least one company where code review was dismissed by the programming architect with "tried it once and didn't work". So, while I'm looking forward to your front page post from whatever wtfcorp has done this time, it's not that unusual for a company not to see any point in it.