- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Au contraire, there certainly is a null type, so speaketh the JLS.
Many type systems have some sort of bottom or omega type that is the subtype of all other types. But in Java, null has a distinct type, so it's not bottom, but anything that is typed to accept a reference may also accept a null. It's an odd state of affairs.
I'm on a terrible connection, but for TRWTF you might look up Tony Hoare's presentation on "Null References: the Billion Dollar Mistake".
Admin
It returns true if s1 == null because it's broken. However, since the previous version would (badly) generate an error if either argument were null, I suppose we're making progress.
The casts to String are just there because it was copied and pasted from the earlier version. The casts were necessary in that method because the arguments were of type Object. The casts to Integer are not necessary in either version.
Admin
And in RW people are often lazy, stupid or under time pressure. And they sometimes hardcode stuff that shouldn't be hardcoded.
Not a Java problem. More like "sure, I'll make that proper later" problem.
Admin
You can't overload operators in java. Hence, == is not a valid comparison for anything other than object instance. Most 'real' programmers would understand that just because a language lacks a feature it does not make it less real of a language.. just different.
Hey I like C# better, but saying java isn't a real language is just idiocy.
Admin
don't worry, he doesn't have real intelligence
Admin
How do you suggest that object equality be implemented for user-defined types?
In Java, null == null.
Admin
Admin
What's this!!!?!?! A WTF from a Java dev??? I was under the impression that all Java devs were the most "l33t" developers on the planet or at least that's what the smug b@#st@rds told me. I'm going to submit this one to MythBusters so we all can enrich our "learns".
Admin
Admin
Maybe so, but don't look at me in that tone of voice! Then again, maybe it was intended that way to make a point which clearly goes over the head of some readers.
Admin
This probably should be the WTF!
Admin
This is the real WTF
Admin
Don't knock nested ternaries. They're great for constructing switch statements in languages that don't support them (like Perl, before 5.10 or 5.12)
Yeah, it's "tricky" the first time you see something like that. Whatever. It's a simple normal form, and it even deals with the exceptional case nicely.
Admin
Admin
Looks to me like the work of a Lisp programmer who was upset that he had to work with Java, and decided to try and turn it into Lisp...
Admin
We just finished our server farm migration.
It went like this.
I guess the key point here is "recently joined a team", "His first task was" and "After a few days" do not add up to a migration expert.
Admin
The rWtf is, that in each version, comparisions compare s1.equals (s1).
Of course nobody sees it in all that casting-rubbish.
And the first version fails for every null-value very noisy, which implies, it never hit a null value, which means, the whole thing is useless trash.
It fails for it's most easy main purpose. WTF!
Admin
Well it's unusual to see ternary operators in the daily WTF. People here have usually barely heard of simple 'if' statements.
Anyway, I think the real WTF is writing a Java client. It's a bit like trying to paint a Photorealism work without any brush and with mittens.
Admin
Funny thing though, a bad C programmer may well become a decent Java programmer, once he learns to give up trying to (and, being bad C programmer, usually failing to) manage all the low-level stuff you have to manage with C, and can actually concentrate on the task at hand... ;-)
Admin
TRWTF is that they should use apache common-lang library which already have this and a lot of other usefull functions.
Admin
This ^^
Admin
It also overlooks the fact that if fed strings it does a case insensitive comparison. "foo" != "FOO"
Admin
Are you talking about the code from the article? I'm not sure you can really say that it's been "overlooked" when it's explicitly noted in the comments at the top of the method.
Admin
CAPTCHA: 'jugis', sheza gotta nice setta jugis
Admin
I stand corrected. I had forgotton that was part of the contract on equals.
Admin
TRWTF is that it's 2010 and Java still needs a method call to test for object equality. In my favorite language, you use == to compare objects (or at least strings) and null == null (or whatever term the language uses for null). Any language that doesn't look exactly like my favorite language is, of course, useless trash and not a real language.
There, fixed that for you.
If == did a value compare, then how would we compare whether two references referred to the same object? I suppose we could have another function for this, but it's not clear that the choice of the designers of Java to make == the reference compare and equals() the content compare is an obviously bad one.
Admin
[quote user="fnord"].
[quote]I actually prefer the ternary operator version here:
Of course, the original code accepted Objects, meaning the caller may want the method to return "false" when the two objects passed in aren't comparable.
Admin
I'll try to get this argument back to objective grounds. Probably to no avail, though.
Simple. With a dedicated operator, e.g. "is".First, since Java doesn't support operator overloading, there WAS no other choice than to make it behave this way.
Second, there a two common issues with this choice:
Admin
True, but the reason people make this mistake is, I think, because they're not taught the language properly. Take something like:
Integer d = new Integer(8675309);
A lot of the educational material on the Java language will tell you that the variable called "d" now holds an object of type Integer. It doesn't. It holds a reference to an Integer. So if you then say:
Integer e = new Integer(8675309);
Does d == e? No. That's very easy to understand if you know that d and e are references to objects, not objects. If people were taught this when they learned the language, this would all be a lot less confusing.
Admin
The master of the world, only you know The secret of a world, only you know www.coolcoachbags.com
Admin
<! -- comment -- not a comment -- comment -->
because you can have lots of other things appear between <! and >, in DTDs. See for example http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.3.1
Admin
@PS
Hi, perhaps there is an issue with the second version in the String compare.
In fact, the guy is going to do a println by adding Strings which may be null references. I suppose this should give a null execption first than null checks are done. The null case for Sting compare method is not going to be managed.
Am I right?
Admin
Nope. In Java, concatenating a String and a null reference does not result in a NullPointerException.
String concatenation of an Object effectively calls String.valueOf(Object) on that Object. As noted in the documentation, that method returns the String "null" if its argument is null. Try it.
Admin
public static boolean equalsAllowNull(Object o1, Object o2){
return (o1 == null) ? o2==null : (o1 instanceof String) ? ((String)o1).equalsIgnoreCase((String)o2) //thows ClassCastException if o2 is not String : o1.equals(o2); }
Admin
@by Someone You Know Thanks for pointing, I have thought, in this case, the Object is already a String... I fear String.valueoF is not going to be called. I have to try this immediatly.
Admin
@by Someone You Know
You are absolutely right! I have tried in Netbeans and it as worked exactly as you pointed.
Thanks for pointing.It looks quite strange String.valueOf() applies to String objects too.
Admin
Sorry, that may have been misleading. I don't know if it actually calls String.valueOf(Object).
But the behavior, which is defined here, is the same — any null reference, String or not, is converted to the String "null".
Bear in mind that a null reference isn't really a String, even if at some point it was assigned to a variable of type String.
Admin
I don't think anyone found this WTF: in both the old and new equalsAllowNull() for Integers, the call to equals() tests the first Integer against itself!
Others pointed out the problems with symmetry: if s1 is null and s2 is not null, the result is true, while if s1 is not null and s2 is null, the result is false.
And of course the String method has the gratuitous call to println().
Admin
Oh yeah, and the casts are now unnecessary in the new form, and you want "i1" and "i2" for the Integers.
Admin
Nope. May NPE if o1 is null and o2 is not. Try this:
Of course, you could avoid the tertiary operators for legibility.
Admin
I'm a bit late.. but thanks for the detailed infos