Sagewire Logo

JAVA programming style question

5 Message(s) by 3 Author(s) originally posted in java software tools


From: David Kerber Date:   Friday, May 04, 2007
What is the preferred way of checking for an empty string that I already
know isn't 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).


From: Daniel Dyer Date:   Friday, May 04, 2007
On Fri, 04 May 2007 15:00:35 +0100, David Kerber =

wrote in message:

What is the preferred way of checking for an empty string that I alrea=
dy
know isn't Null?
Is it better (however you define that) to check for .length =3D 0, or
.equals( "" )?


I do not think it really matters, as long as you do not use =3D=3D. Intel=
liJ =

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


From: David Kerber Date:   Friday, May 04, 2007
In article <op.trs3fvbv8kxvgr@xxxxxxxxxxx>, "Daniel Dyer"
<"You do not need it"> says...
On Fri, 04 May 2007 15:00:35 +0100, David Kerber
wrote in message:
> What is the preferred way of checking for an empty string that I already
> know isn't Null?
>
> Is it better (however you define that) to check for .length = 0, or
> .equals( "" )?
>
I do not think it really matters, as long as you do not 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!

....

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


From: Mike Date:   Friday, May 04, 2007
On May 4, 3:00 pm, David Kerber
wrote in message:
What is the preferred way of checking for an empty string that I already
know isn't 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.


From: David Kerber Date:   Friday, May 04, 2007
In article <1178294413.418046.7710@xxxxxxxxxxx>,
mike.mkaufman@xxxxxxxxxxx says...
On May 4, 3:00 pm, David Kerber
wrote in message:
> What is the preferred way of checking for an empty string that I already
> know isn't 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 will use. Thanks for the comments,
guys!

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



Next Message: Problem with deployment using Myeclipse and JBoss


Blogs related to JAVA programming style question

Rod Johnson – Understanding Enterprise Java Open Source
Although it’s still relatively new in this space, open source has developed unstoppable momentum and is having a big impact on enterprise-level Java. The question is whether it can be to work in the long-term enterprise picture. ...

Retro-fitting coding standards
Up to now I’ve avoided explicitly mentioned what programming language I’m talking about. If the language were Java, then defining our Style Guide really shouldn’t be an issue: we’d just cross-reference the Sun Java Programming ...

The right to criticize programming languages
So the cool things about Java. How can I not say portability ? There was never a language as portable as Java. I can take any Java program, put it on any machine, from a mobile phone up, and it will Just Work. Ever tried that with C ? ...

Axis Message Style
If you use the RPC provider, Axis needs to know what type of messages to expect and how to map them to Java objects. When using the Message provider, Axis just maps them to DOM, and it's up to the application to then figure out how to ...

Adobe Engage: Good Morning Apollo
The last White Knight to go up against — Java desktop — turned out more like Don Quixote than Sir Gawain. (Now, there’s been much scrambling of late to fix that, but the web, Microsoft, and things like Apollo have a huge jump over Java ...

A list of interesting Erlang links
Overview of Erlang; The Hitchhiker’s Guide to Erlang; A first Erlang program; A first Erlang program - part 2; Test-First Word Wrap in Erlang; Tutorial at erlang.org; Getting started with Erlang; Erlang for C, C++ and Java Programmers ...


Programming | Sports | Autos

copyright 2006
Valid XHTML 1.0 Transitional