This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to check for empty strings

 
praveen kumar gowda
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String name1="praveen";

String name2=new String(praveen);
if(name1=="")
{
}
if(name2=="")
{
}
my doubt is if i create string using new operator can we check against empty as shown in first if statment ..
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mostly No. Use compareTo() or equals()

WP
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Always use equals() to compare objects' states, not ==. This includes comparing Strings' contents.

Note, however, that String has an isEmpty() method.
 
Randall Twede
Ranch Hand
Posts: 4442
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i agree sometimes == will work but you should use the String method equals()
 
Randall Twede
Ranch Hand
Posts: 4442
3
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
isEmpty() certainly is better than equals("") thanks for that tip
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William P O'Sullivan wrote:mostly No.


No "mostly" about it. It won't work.

Use compareTo() or equals


No, not compareTo(). It would be better to use equals() than compareTo(), and better to use length() == 0 than equals(), and best of all to use isEmpty().
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this ... It does work! ;)


Now, For the record I am very against using the "==" for Strings, and recommend NOT using it at all!

WP
 
Koen Aerts
Ranch Hand
Posts: 344
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works because "" goes into the string pool and so you're comparing the same "pointers" (i.e. same objects) with the ==. With that logic, the following would work, too:
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William P O'Sullivan wrote:Try this ... It does work! ;)


The OP asked about
if i create string using new operator
. In that context (which I thought is what you were answering), it doesn't work at all.

And even in the context you posted, I wouldn't call it "working." The OP wants to compare the contents of 2 Strings. The code you posted doesn't do that. It happens to produce the same result for that particular case, but I don't call taht "working".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic