2

Possible Duplicate:
How do I compare strings in Java?

even whel length is 0 and flag9 is null only else part is executed.can someone help me

Log.d(TAG, "flag8 "+ getIntent().getExtras().getString("flag").length());
Log.d(TAG, "flag9 "+ getIntent().getExtras().getString("flag"));
if( getIntent().getExtras().getString("flag")=="0"   ||getIntent().getExtras().getString("flag").length()==0)
{
 Log.d(TAG, "hidebeta "+ getIntent().getExtras().getString("flag"));
            beta.putExtra("beta", "hidebeta");
            flag=1;
            beta.putExtra("flag", "1");
                }
else {
    beta.putExtra("beta","showbeta");
    Log.d(TAG, "showbeta "+ getIntent().getExtras().getString("flag"));
    flag=0;
    beta.putExtra("flag","0");
                }
Community
  • 1
  • 1
Payal
  • 145
  • 5

3 Answers3

5

In Java you cannot use == to compare Strings, you must use:

if(string.equals("example"))

So let's use equals() in your conditional and optimize it:

String flag = getIntent().getStringExtra("flag");
if(flag.equals("0") || flag.length()==0)

(Also you ought to be safe and check if getIntent() and flag are not null before trying to access them.)

Read How do I compare strings in Java? or the documentation on Comparing Strings for more information.

Community
  • 1
  • 1
Sam
  • 86,580
  • 20
  • 181
  • 179
5

use equals to compare strings, not == (which compares references to objects)

MByD
  • 135,866
  • 28
  • 264
  • 277
5

You should never use == operator for comparing two Strings, as it compares the actual references, not their values. Use equals() method instead. Hope this helps.

Egor
  • 39,695
  • 10
  • 113
  • 130