开发者

if("a" == "a") not working [duplicate]

This question already has answers here: How do I compare strings in Java? 开发者_如何学编程(23 answers) Closed 9 years ago.
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.beta);

    ed_code = (EditText) findViewById(R.id.et_beta_01);
    bu_ok = (Button) findViewById(R.id.bu_beta);

    bu_ok.setOnClickListener(new OnClickListener() {    
        @Override
        public void onClick(View v) {
                // TODO Auto-generated method stub
            String code = ed_code.getText().toString();
            String target = "vsi8";

            Log.v(TAG, code+"="+target);

            if(code == target){
                    Intent intent = new Intent(BetaCheck.this, AppMenu.class);
                startActivity(intent);
            }
            else {
                    Toast.makeText(getApplicationContext(), "wrong", Toast.LENGTH_SHORT).show();
                    ed_code.setText("");
            }
        }
    });
}

It seems that the the if statement does not understand that the 2 values are equal.

Thanks for the help


Strings, should be compared using .equals and not ==. (== checks for reference equality and not for content equality.)

That is, change

if(code == target)

to

if(code.equals(target))

Related question:

  • How do I compare strings in Java?


If you want compare string values, you should use the equals() method, as in str.equals(value).


This is a common pitfal in java. Basically what aioobe said. Here's the code... It can be tricky. If you do:

 if( "a" == "a" )

You will get true because the compiler will just see two static strings that are equal just 'reuse' one. The == operator for String compares the REFERENCES, meaning it's testing to see if they are the same object. Even a case of:

String a = "a" ;
if (a == "a") {

You'll still get true because again the string gets recycled when the compiler optimizes that code to reuse the first "a" for the second to save space.

Now in the following case, we generate an empty string, manipulate it by appending "a"(not really, strings are immutable, so we end up generating a 3rd string BUT that is a different one since the JVM isn't going to waste its time looking for an existing string that's the same.

class tmp {

public static void main(String arg[]) {
String a = "" ;
a = a+"a" ;

if( a == "a" ) {
    System.out.println("true") ;
}
else {
     System.out.println("false") ;
}

if( "a".compareTo("a") == 0 ) {
    System.out.println("true") ;
}
else {
     System.out.println("false") ;
}

System.out.println("a = '" + a + "'") ;

}

}


Use code.equals(target) instead of code == target

http://bimal4u.wordpress.com/2007/03/29/what-is-the-difference-between-aequalsb-and-a-b/


Replace code == target with code.equals(target)


You should try the following code, because references of two strings, even if the strings are the same, are not the same.

if (something.equals(someOtherThing)) {
  // …
}


comparing two strings wont work with == . It is only when u compare a value... so plz try something like below

if (strcmp(code, target) == 0)

or

if (code.equals(target))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜