0

i have the following

String newWord = (String) addNewWordEdTxt.getText().toString();
Log.d(TAG_WORD, "A:"+ String.valueOf(newWord.trim() == "" ));
Log.d(TAG_WORD, "B:" + String.valueOf( TextUtils.isEmpty(addNewWordEdTxt.getText().toString().trim() )));

anyone know why A is false and B is true for an empty EditText

thanks

Jock Mahon
  • 116
  • 1
  • 9

2 Answers2

3

anyone know why A is false and B is true for an empty EditText


Because in A case, you are comparing references and not quality so you have to do it like that:

Log.d(TAG_WORD, "A:"+ String.valueOf(newWord.trim().equals("")));


Note: There is one golden rule: If you want to compare Strings, always use equals() method!

Here is nice explanation:

Community
  • 1
  • 1
Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
1

i thinks java a string compare with any string use .equals() method not used ==

that's like

String.valueOf(newWord.trim().equals(""));
Android_coder
  • 9,953
  • 3
  • 17
  • 23