0

Created an ArrayList and filled its slots:

ArrayList<String> tempTable = new ArrayList<String>();

tempTable.add("1");
tempTable.add("12");
tempTable.add("2");
tempTable.add("11");
tempTable.add("5");

Now, I'd like to replace some of values, so created conditions:

for (int i = 0; i < tempTable.size(); i++)
{
    if (tempTable.get(i) == "11")
    {
        tempTable.set(i, "1st FBS");
    }
    else if (tempTable.get(i) == "12")
    {
        tempTable.set(i, "2nd FBS");
    }
    else if (tempTable.get(i) == "13")
    {
        tempTable.set(i, "3rd FBS");
    }
    else
    {
        // leave as is
    }
}

When I run it, I get the original values untouched, so it prints 1, 12, 2, 11, 5 instead of having 11 and 12 replaced. Made a test with ArrayList and it worked, but in this case I can't get it working as I wish.

Any hints what could be wrong in my code?

Sri Harsha Chilakapati
  • 11,744
  • 6
  • 50
  • 91
AbreQueVoy
  • 1,748
  • 8
  • 31
  • 52

2 Answers2

4

Use string equals() instead of == for string comparisons. As equals compares the content while == checks whether references are pointing to the same memory location or not.

Here is how your code should look like:

for (int i = 0; i < tempTable.size(); i++)
{
    if ("11".equals(tempTable.get(i)))
    {
        tempTable.set(i, "1st FBS");
    }
    else if ("12".equals(tempTable.get(i)))
    {
        tempTable.set(i, "2nd FBS");
    }
    else if ("13".equals(tempTable.get(i)))
    {
        tempTable.set(i, "3rd FBS");
    }
    else
    {
        // leave as is
    }
}
Sri Harsha Chilakapati
  • 11,744
  • 6
  • 50
  • 91
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • You're right. Just checked it and it works perfectly. Thanks a lot! – AbreQueVoy Jun 08 '13 at 14:44
  • It's not a good idea to compare as you did, cause it may throw a NPE if an element of the array is set to null. TrustNoOne provided a better alternative. – Alexis C. Jun 08 '13 at 14:44
  • @ZouZou In the current scenario or program it will not throw NPE. As user has initialized the list as tempTable.add("1"); tempTable.add("12"); tempTable.add("2"); tempTable.add("11"); tempTable.add("5"); – Juned Ahsan Jun 08 '13 at 14:45
  • I agree with ZouZou. A safer way would be `"stringliteral".equals(stringVariable)` – RaptorDotCpp Jun 08 '13 at 14:46
  • @JunedAhsan In this case yes. But I would just point out that, in general, it's better to do as TrustNoOne said. – Alexis C. Jun 08 '13 at 14:47
  • 1
    @RaptorDotCpp I agree althoug in the current scenario the NPE may not happen but good suggestion is accepted and updated the resolution. – Juned Ahsan Jun 08 '13 at 14:48
  • You weren't wrong ofcourse, but I think that good practices should always be noted :) – RaptorDotCpp Jun 08 '13 at 14:48
  • @RaptorDotCpp Agreed, Honestly speaking sometimes I overlook such things in a hurry to put the resolution. But I am always open for such good suggestions and never mind updating my resolution. Thanks! again – Juned Ahsan Jun 08 '13 at 14:50
4

In java you must use equals() to test string equality:

if ("11".equals(tempTable.get(i))) 
{
    tempTable.set(i, "1st FBS");
}

== on objects is an operator that tests Reference equality - i.e. that two references ("variables") point to the same instance in memory.

== might work on Strings too because literal strings are cached and preloaded by the jvm, but any dynamically created string will be a different object even if the textual content is the same. For instance, if you read from the command line a string, even if you type two times the same characters the String instance will be different.

As someone pointed in a comment to the other answer, the usual way of doing string comparisons with literals is the following:

"literal".equals(ref)

That's because calling a method on a literal string can never throw NullPointerException and just returns false if ref is null

Sri Harsha Chilakapati
  • 11,744
  • 6
  • 50
  • 91
Giovanni Caporaletti
  • 5,426
  • 2
  • 26
  • 39