-4

I have this, in my onPostExecute :

    String test = currentActivity.getClass().getSimpleName();
    if(test == "SplashScreen"){
        Log.e("OK",test);

    } else {
        Log.e("OK",test);

    }

The problem is, the value of test is "SplashScreen" (that we can see with the log), but the code never goes in the if, only in the else.

Why does this happen?

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
deveLost
  • 1,151
  • 2
  • 20
  • 47

5 Answers5

3

As the others had already said you can not use the == operator on strings. The reason for this is that strings are objects and not primitive datatypes. With that said the == operator will check if the two strings has the same memory point in the memory if I'm not mistaken.

Payerl
  • 1,042
  • 2
  • 16
  • 33
2

use equals to compare strings:

 if(test.equals("SplashScreen"){ 
  Log.e("OK",test);

 } else {
    Log.e("OK",test);

}
Zied R.
  • 4,964
  • 2
  • 36
  • 67
2

You can't use == on strings. You should use:

"SplashScreen".equals(test)

Test might be null, it is better to call equals() on "SplashScreen" since you know that it is no null.

l-l
  • 3,804
  • 6
  • 36
  • 42
0

Don't compare string with == use .equals()

String test = currentActivity.getClass().getSimpleName();
if(test.equals("SplashScreen")){
    Log.e("OK",test);

} else {
    Log.e("OK",test);

}
Anthony Raymond
  • 7,434
  • 6
  • 42
  • 59
0

It's better to use equalsIgnoreCase(String string).

 String test = currentActivity.getClass().getSimpleName();
if(test.equalsIgnoreCase("SplashScreen")){
    Log.e("OK",test);

} else {
    Log.e("OK",test);

}
loumaros
  • 433
  • 3
  • 7