Are there any differencies between these two code ?

    if (!a.equals("")) {
                editDate.setText(a)
     }

and

  if (a!="") {
        editDate.setText(a)
    }

Recommended Answers

== and != for objects test for RH and LH expressions both referring to exactly the same object (or not).
The equals method tests that the two objects are equivalent in some sense that depends on what kind of object they are. Eg for two Strings it tests that they …

Jump to Post

All 2 Replies

== and != for objects test for RH and LH expressions both referring to exactly the same object (or not).
The equals method tests that the two objects are equivalent in some sense that depends on what kind of object they are. Eg for two Strings it tests that they both comtain the same sequence of characters.
So if a refers to a String that happens to contain "" the first test will be true, but in general the second will not because they will be two different Strings that just happen to have the same characters.

What can make this a little confusing when trying it out is that Java is very smart about how it stores Strings, and it will re-use an existing String if it has a suitable one. eg

String a = "";  // creates a new ''" object
String b = "";  // re-uses the existing object
// a.equals(b)  and a==b are both true

but

String a = "";  // creates a new"" object
String b = new String("")  // creates another new "" object
// a.equals(b) is true but a==b is false
commented: You're right. I was sleepy and know better to write as I doze off. Removing my answer. +15

By and large the two equivalents() and "==" administrator in Java are utilized to contrast objects with check equity yet here are a portion of the contrasts between the two: In straightforward words, == checks if the two items point to a similar memory area though .rises to() assesses to the correlation of qualities in the articles.

commented: Adds nothing to existing answers -3