This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Validating empty string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Validating empty string" Watch "Validating empty string" New topic

Validating empty string

Nico Yukiko

Joined: Aug 24, 2005
Posts: 21
i wrote if (email==""){
but it does not show this is i leave emai as empty???
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

You should always compare Strings using the equals method. == will not always return true.


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
Michael Ernest
High Plains Drifter

Joined: Oct 25, 2000
Posts: 7292

The == operator here tests whether "" and email point are in the same memory location. Even if the variable email did in fact contain an empty string, the == doesn't know that's what you mean. The equals() method in the String class is a test for what's in the String, rather than where the thing is located.

Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Stephen Boston
Ranch Hand

Joined: Jul 14, 2005
Posts: 165
Comparing Strings was difficult for me as well. I think this is what you are looking for.

[ September 13, 2005: Message edited by: Stephen Boston ]

Steve<br /> <br />No matter where you go, there you are.<br /> <br />"My evil self is at the door, and I have no power to stop it."
I agree. Here's the link:
subject: Validating empty string
It's not a secret anymore!