Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Checking for empty(?) string  RSS feed

 
Dick Summerfield
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've just started writing Servlets.
If I wanted, in an application, to ascertain whether a String had content I would write:
or

In a Servlet, that first option does not appear to work... The actual code is:

With the code as shown here a String with content is processed fine but an empty String is not recognized and the else block is not executed - the other test, based on length, works fine for filled or empty String. Can anyone please give me a tip why the test with the "" doesn't (appear to) work in a Servlet?
[ February 17, 2008: Message edited by: Dick Summerfield ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should almost never do string comparisons with the == or != operators.
Use the String.equals() method instead.

This is discussed often here so you should be able to find a more detailed explanation with the search tool. This is also covered at the beginning of almost every book on Java that I've ever seen.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, in a servlet, you will want to test your string for null before trying to call its equals() method. The getParameter method will return null if the specified parameter does not exist in the request.

 
Dick Summerfield
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ben,
I had an uneasy feeling I shouldn't be comparing Strings with != but couldn't put my finger on it. In view of what you say below:
Originally posted by Ben Souther:
This is also covered at the beginning of almost every book on Java that I've ever seen.

...it's clearly time to go back for the first time and review those early chapters...
Of course a String is not a primitive and I know I shouldn't compare two objects with == ( unless I want to compare the references). I suppose a similar (or the the same) thing is happening here - I'm gonna give it some thought.

Thanks for reminding me about the possibility of a null being returned - I came across it in earlier experiments when names in HTML and Servlet didn't match. I'll build the check in.

But what about checking a String for non-empty with length()?

Does this get the stamp of approval?
[ February 17, 2008: Message edited by: Dick Summerfield ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The real tough thing about using the == operator to compare strings is that, because of the way the JVM pools string constants and interned string, it sometimes works. It just isn't working for the reasons that you think it is.
So...
Unless you really know what you're doing and have a real good reason for doing so don't ever compare strings with ==. Always use String.compare().


Checking the length is fine.
Just make sure that the String isn't null before trying to call its .length method or you could end up with a NullPointerException.
See my earlier note about getParameter.
 
Dick Summerfield
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the clear (and quick!) answer. I promise not to use == and != with Strings until (If ever) I really understand what's going on .
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!