« CBS Video | Main | Spam is funny »

Java String isEmpty()

I have heard the java string class will finally get the isEmpty method in an upcoming release. Lets hope it is in 1.6, because I can't wait much longer, I can't for the life of me see why this was ignored for so long. Why have I had to put this in so many freakin if statements when this could have been fixed by adding a very easy and simple but amazingly easy to read and useful method.

String randWord;

/*
random code or back in forth between many different classes
*/

String someFunction() {
if(randWord!=null && !randWord.equals("")) {
/*do something*/
}

....
}

all of this could just be replaced and make the code so much more readable with the isEmpty function, becoming the following...

if( !randWord.isEmpty() ) {
/*do something*/
}

I want this functionality and I want it now... I guess since java is now opensource I should just go override the String class to add one simple function! So much value and so simple.

Comments (1)

Ben:

Yeah, it's annoying when the library writers forget something that you find really useful. Don't even get me started on how annoying the .NET String class is. You can subclass the type, but that still sucks, because you can't create your type from literals ("foo" is always easier than new MyStringSubClass("foo")). That's why I think extension methods are such a cool feature - yes, it will be very easy to abuse them, but they do let you add to standard classes in a way subclassing just doesn't allow sometimes. Very useful.

Post a comment