• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Checking for empty(?) string

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic