Are there any differencies between these two code ?

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

and

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

Recommended Answers

All 3 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

In general both equals() and “==” operator in Java are used to compare objects to check equality but here are some of the differences between the two: ... In simple words, == checks if both objects point to the same memory location whereas .equals() evaluates to the comparison of values in the objects.

commented: Pathetic re-copy of previous answers. If you have nothing to say, don't post. -3
commented: These posts needs to be removed. -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.