1

This is my code:

Log.d("inf", loader);
if (loader == "Klasse"){
    Log.d("inf", "Klasse yes");
    OPTS = new String[] { //Something };
} else {
    Log.d("inf", "Klasse no");
    OPTS = new String[] { //Something };
}

In the first Line, the Output is exactly "Klasse" but the next output is everytime "Klasse no". The loader String is set as follows:

String loader = "";
Bundle exta = getIntent().getExtras();
    if(exta !=null) {
        try {
            json = new JSONObject(exta.getString("json"));
            loader = exta.getString("order");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

Do you guys have any Idea whats wrong?

derekerdmann
  • 17,696
  • 11
  • 76
  • 110
Styler2go
  • 604
  • 2
  • 12
  • 32

4 Answers4

11

The == comparison operator compares references rather than values. You need to compare the values and so need to use the equals() method.

if (loader.equals("Klasse"))
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
0

There is a difference between identity (==) and equality (equals()). Identity tests for whether or not the object pointers are the same so almost never is correct. Equality tests whether or not the object "values" are the same, so it's almost always what you need instead.

user268396
  • 11,576
  • 2
  • 31
  • 26
0

String comparison should always be done with the equals method.

if(loader.equals("Klasse")) { .... }

This is because String is a class and by using '==' you check whether they are the same object, which they a not. Equals checks that the strings you are comparing are equal.

Thorkil Holm-Jacobsen
  • 7,287
  • 5
  • 30
  • 43
0

if == or === does not work, use .equals

Klinetel
  • 302
  • 1
  • 7
  • 27