20 Iulii 2006

Nope, Java’s still evil

I swear, I didn’t write yesterday’s Java growl to get attention. Get it I did, however, and in spades—a month’s worth of CavLec-related email, all to tell me that in Java, strings are objects, and == applied to two String objects checks to see whether they’re the same object, not whether they contain the same string.

I won’t forget it again. Believe me, I won’t!

The best explanation I have for yesterday’s attack of pure stupidity is that I imprinted on string-related programming. I was a text wrangler. Regular expressions and fancy search-and-replace tricks and splitting strings up and jamming them back together, that was what I did. I once wrote a program that decomposed Quark’s XPress Tags to translate them into CSS. I swear I did. It was a heinously bad program in hindsight (I didn’t know nothin’ ’bout writin’ no parser), but it worked. It even understood defaults and left them out of the result—no background-color: white; color: black outta me, no sir!

And I did it in Python, which kindly treats strings as basic data structures. You compare two strings, you’re comparing two strings. None of this object nonsense to get in the way. Because really, how often do you need to check whether two String objects are the same object? Compared to how often you need to know whether they contain the same sequence of characters?

I mean, sheesh, even strings being objects isn’t necessarily a barrier to sane comparison behavior. If you were to implement strings as objects in Python (and for all I know, they are implemented that way; I’ve never peered that far under the hood—I doubt it, though, because I suspect that stuff is all in C), you could simply make == Do The Right Thing with a smidgen of operator overloading—

Oh. Wait. Java doesn’t do operator overloading. (As far as I can tell, anyway. Google says that people have been talking about it since 2001 or thereabouts!)

*headdesk*

I give up. Sorry, y’all, Java is still Teh Evil. I do appreciate the efforts of all my Java-aficionado readers to teach me the One True Java Way, however.