Take the 2-minute tour ×

Possible Duplicate:
If statement using == gives unexpected result

Hi I'm using this code to add elements to my ComboBox, and I do not want to add empty elements, here's the code:

  public void elrendezesBetoltes(ArrayList<Elrendezes> ElrLista){
    int i;
    Elrendezes tmp;
    model = new DefaultComboBoxModel(comboBoxItems);
    for(i=0; i<ElrLista.size(); i++){
         tmp = ElrLista.get(i);
         if(tmp.getName()!="")comboBoxItems.add(tmp.getName()); //not working

My problem is that the if statement is not working, it still adds empty names to my combobox. What am I doing wrong?

share|improve this question
add comment

marked as duplicate by hmjd, jschoen, evilone, Ram kiran, Alessandro Minoccheri Nov 28 '12 at 7:29


7 Answers

up vote 1 down vote accepted

Always use equals method to compare Strings: -

if (tmp.getName()!="")

should be: -

if (!tmp.getName().equals(""))

or simply use this, if you want to check for empty string: -

if (!tmp.getName().isEmpty()) {
share|improve this answer
Thank you this did the job. –  Balázs Csíkos Nov 27 '12 at 15:34
@BalázsCsíkos.. You're welcome :) You should better use the last way, to check for empty string. –  Rohit Jain Nov 27 '12 at 15:34
add comment

Use equals method to compare string. By using != operator, you are comparing the string instances, which is always going the be true as they(tmp.getName() and "") are not same string instances.





Putting "" as first string in comparison will take care of your null scenario as well i.e. it will not break if tmp.getName() is null.

share|improve this answer
depending on the data maybe even !"".equals(tmp.getName().trim()) –  jlordo Nov 27 '12 at 15:53
add comment

Use equals():


Using == or != compares string references, not string contents. This is almost never what you want.

share|improve this answer
add comment

you have to compare Strings with "equals", then it will work

share|improve this answer
add comment

you are comparing for identity (==, !=) but each String instance has its own identity, even when they are equal.

So you need to do !tmp.getName().equals("").

Generally it is considered best practice to start with the constant string first, because it will never be null: !"".equals(tmp.getName())

However, I would recommend to use apache commons lang StringUtils. It has a notEmpty() and notBlank() method that take care of null handling and also trimming.

PS: sometimes identity will work for Strings. but it should not be relied upon as it is caused by compiler or jvm optimization due to String immutability.

share|improve this answer
add comment

Use String#isEmpty()




Always, check String equality with equals method. == operator only checks if two references point to the same String object.

share|improve this answer
add comment

Another alternative if not on Java 6 and isEmpty is unavailable is this:

if (tmp.getName.length()>0)

Checking for the length is supposed to be quicker than using .equals although tbh the potential gain is so small its not worth worrying too much about.

share|improve this answer
add comment
