final EditText yaz = (EditText)findViewById(R.id.editText);
final Button bas = (Button)findViewById(R.id.button);
yaz.setText("500");
bas.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String afd = yaz.getText().toString();
if (afd == "1000") {
yaz.setText("2000");
}
}
}
);
}
Asked
Active
Viewed 40 times
-3

Cœur
- 37,241
- 25
- 195
- 267

Ali Fatih Demir
- 1
- 1
2 Answers
4
In Java, you cannot check equality between Strings
with the ==
operator. This operator checks equal references for reference types, and equal values for primitive types only. Since String
is a reference type, there's no surprise this is failing.
To check equality between Strings
(or Objects
for that matter), use this:
str1.equals(str2);
or in your case:
afd.equals("1000");

Gil Moshayof
- 16,633
- 4
- 47
- 58
-
thank you very much, good work – Ali Fatih Demir Mar 08 '15 at 10:41
2
Replace
if (afd == "1000")
with
if (afd.equals("1000"))
This should work.
The idea is that ==
checks values of entities that lie on the stack, and in Java, only primitive types and references are held on the stack. Objects lie on the heap, and to compare object state you need a runtime virtual method like equals()
(and not an operator like ==
).

Yash Sampat
- 30,051
- 12
- 94
- 120