 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
David Kerber Guest
|
Posted: Fri May 04, 2007 7:00 pm Post subject: Java programming style question |
|
|
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
--
Remove the ns_ from if replying by e-mail (but keep posts in the
newsgroups if possible). |
|
| Back to top |
|
 |
Daniel Dyer Guest
|
Posted: Fri May 04, 2007 7:20 pm Post subject: Re: Java programming style question |
|
|
On Fri, 04 May 2007 15:00:35 +0100, David Kerber
<ns_dkerber (AT) ns_WarrenRogersAssociates (DOT) com> wrote:
| Quote: | 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.
--
Daniel Dyer
http://www.uncommons.org |
|
| Back to top |
|
 |
David Kerber Guest
|
Posted: Fri May 04, 2007 7:39 pm Post subject: Re: Java programming style question |
|
|
In article <op.trs3fvbv8kxvgr (AT) cgl0656 (DOT) chaucer.co.uk>, "Daniel Dyer"
<"You don't need it"> says...
| Quote: | On Fri, 04 May 2007 15:00:35 +0100, David Kerber
ns_dkerber (AT) ns_WarrenRogersAssociates (DOT) com> wrote:
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
|
Yeah, I already learned that lesson :-D
| Quote: | 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!
.....
--
Remove the ns_ from if replying by e-mail (but keep posts in the
newsgroups if possible). |
|
| Back to top |
|
 |
Mike Guest
|
Posted: Fri May 04, 2007 9:00 pm Post subject: Re: Java programming style question |
|
|
On May 4, 3:00 pm, David Kerber
<ns_dkerber (AT) ns_WarrenRogersAssociates (DOT) com> wrote:
| Quote: | 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
--
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. |
|
| Back to top |
|
 |
David Kerber Guest
|
Posted: Sat May 05, 2007 7:08 am Post subject: Re: Java programming style question |
|
|
In article <1178294413.418046.7710 (AT) n76g2000hsh (DOT) googlegroups.com>,
mike.mkaufman (AT) googlemail (DOT) com says...
| Quote: | On May 4, 3:00 pm, David Kerber
ns_dkerber (AT) ns_WarrenRogersAssociates (DOT) com> wrote:
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
--
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.
|
Yes, I'm using 1.5, so that's what I'll use. Thanks for the comments,
guys!
--
Remove the ns_ from if replying by e-mail (but keep posts in the
newsgroups if possible). |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|