Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Tools / May 2007

Tip: Looking for answers? Try searching our database.

Java programming style question

Thread view: 
David Kerber - 04 May 2007 15:00 GMT
What is the preferred way of checking for an empty string that I already
know is not Null?

Is it better (however you define that) to check for .length = 0, or
.equals( "" )?

TIA!
Dave
Signature

Remove the ns_ from if replying by e-mail (but keep posts in the
newsgroups if possible).

Daniel Dyer - 04 May 2007 15:20 GMT
> What is the preferred way of checking for an empty string that I already
> know is not Null?
>
> Is it better (however you define that) to check for .length = 0, or
> .equals( "" )?

I don't think it really matters, as long as you don't use ==.  IntelliJ  
IDEA suggests using .length() for performance reasons, but I doubt it  
would make any noticeable difference in most cases.

Dan.

Signature

Daniel Dyer
http://www.uncommons.org

David Kerber - 04 May 2007 15:39 GMT
> > What is the preferred way of checking for an empty string that I already
> > know is not Null?
[quoted text clipped - 3 lines]
>
> I don't think it really matters, as long as you don't use ==.  IntelliJ

Yeah, I already learned that lesson :-D

> IDEA suggests using .length() for performance reasons, but I doubt it
> would make any noticeable difference in most cases.

That's kind of what I figured; thanks!

....

Signature

Remove the ns_ from if replying by e-mail (but keep posts in the
newsgroups if possible).

Mike - 04 May 2007 17:00 GMT
On May 4, 3:00 pm, David Kerber
<ns_dkerber@ns_WarrenRogersAssociates.com> wrote:
> What is the preferred way of checking for an empty string that I already
> know is not Null?
[quoted text clipped - 7 lines]
> Remove the ns_ from if replying by e-mail (but keep posts in the
> newsgroups if possible).

As of JDK 6, the String class now has an "isEmpty" method that returns
true if the length is zero. So I'd say use "isEmpty" if on JDK 6, or
otherwise check the length.
David Kerber - 05 May 2007 03:08 GMT
> On May 4, 3:00 pm, David Kerber
> <ns_dkerber@ns_WarrenRogersAssociates.com> wrote:
[quoted text clipped - 13 lines]
> true if the length is zero. So I'd say use "isEmpty" if on JDK 6, or
> otherwise check the length.

Yes, I'm using 1.5, so that's what I'll use.  Thanks for the comments,
guys!

Signature

Remove the ns_ from if replying by e-mail (but keep posts in the
newsgroups if possible).



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.