compare with empty string uses equals method or == ??

M

Matt

I know when we do Java string comparision, we should use equals method.
But how about compare with an empty string? Is it an exception? or we
should still use equals method?

I saw people do both, but I don't know if == is ok?

s.equals("")
s == ""

please advise. thanks!!
 
A

andrewh1

Matt said:
I know when we do Java string comparision, we should use equals method.
But how about compare with an empty string? Is it an exception? or we
should still use equals method?

I saw people do both, but I don't know if == is ok?

s.equals("")
s == ""

please advise. thanks!!

1. With questions as elementary as these you should be using
comp.lang.java.help

2. With questions like these why not write a simple program to test
each and see what the result is. You will learn a lot better than
getting told.

3. Try

http://jroller.com/page/ethdsy/20040621#empty_strings

At the moment it has a discussion of a simple program which tests each
possibility. (And note the result with '=='.)

cheers

Andrew
 
M

Moritz Tuerk

Matt said:
I know when we do Java string comparision, we should use equals method.
But how about compare with an empty string? Is it an exception? or we
should still use equals method?

I saw people do both, but I don't know if == is ok?

s.equals("")
s == ""

please advise. thanks!!

To check for empty strings it's
better to check for a zero length:

if (s.length() == 0) {
//do stuff...
}

greetings
mo
 
T

Tony Morris

s.equals("")
s == ""

please advise. thanks!!

Neither.
Use (s.length() == 0).

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform
 
G

Gary Labowitz

Tony Morris said:
Neither.
Use (s.length() == 0).

I fully concur. However, it depends on how the String s is initialized. If
the user has the convention of setting the String using the literal "" then
the check using == should be okay.
I.e.
String s = "";
.....
if (s == "")
{ ...
will compare equal if s has not been changed in the intervening code. It's
strikes me as a dangerous practice, however, since it is misleading unless
the programmer is solid in understanding of the use of the literal pool. I
say this because the comparison string could be anything, not just "", e.g.
"empty" or "NULL" would be just as acceptable.
 
R

Roedy Green

will compare equal if s has not been changed in the intervening code. It's
strikes me as a dangerous practice, however, since it is misleading unless
the programmer is solid in understanding of the use of the literal pool. I
say this because the comparison string could be anything, not just "", e.g.
"empty" or "NULL" would be just as acceptable.

However if someone did

s = " ".trim();

if ( s == "" )
it will not match.

You could get away with it if you wrote:

s = " ".trim().intern();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top