Sumithra
Sumithra

Reputation: 6717

difference among ==,equals() and instanceof()

String s=new String("Computer");
if(s=="Computer")
    System.out.print("equals A");
if(s.equals("Computer"))
    System.out.print("Equal B");

Output is Equal B

Now why == doesnot produce equals A

what is instanceof?

Upvotes: 0

Views: 3959

Answers (5)

Adeel Ansari
Adeel Ansari

Reputation: 39907

The equals() method will return true, if two Strings have the same value. The == operator will only be true, if both Strings point to the same underlying Object. Hence two Strings representing the same content are guaranteed to be equal, when tested by the equals(Object) method; whereas when tested with the == operator, it will only be true with they refer to the same Object. So, we should always use equals() for String comparison.

The instanceof keyword can be used to test if an object is of a specified type. For example,

if(foo instanceof Bar) { // must not pass
}

For more insight, see here.

Upvotes: 3

iirekm
iirekm

Reputation: 9436

  • x instanceof ClassName returns true if, and only if x is an object of class ClassName or any subclass of it

  • x == y works differently for value types (char, int, float, etc) and reference types (Object, Integer, YourClass, int[], ...): for value types returns true if x and y are just the same value, whereas for reference types returns true if x and y denote the same object - for two objects with different values the result can be false. Just treat == on reference type like comparison of pointers in C++ - the result is true if integers representing pointers are equal, ie pointers denote to the same object. So: new String("abc") == new String("abc") is false - the same value but different objects, similarly "abc" == new String("abc") is false, but "abc" == "abc" is true, because if a String constant appears many times in a .class file, it is converted into one String object.

    • x.equals(y) is by default (in Object.equals) implemented just as x == y, but many subclasses override it, for example for Strings, x.equals(y) checks if those Strings have the same value, despite if they are same objects or not, so: new String("abc").equals(new String("abc")) is true, and "abc".equals("abc") is true

    • however, if all your Strings to compare have been 'interned', like: x = x.intern(); y = y.intern(), then x == y will work. So for example "abc" == new String("abc").intern() gives true.

Upvotes: 0

punkers
punkers

Reputation: 107

It would return true if you had defined the string as s="Computer" instead of creating a new object. this is because java stores string literals in a special memory space so s and "Computer" string would point to the same memory location. instanceof is an operator that returns true if the left side object is of the type of the class specified by the right side.

E.g.

String s=""; 
if(s instaceof java.util.String){
   ......
 }

The prevoius bit of code would return true.

Upvotes: 0

Cyntech
Cyntech

Reputation: 5572

Agree that this possibly a duplicate of another question, but I'll throw an answer in anyway.

The == operator is to determine whether two objects or raw data types (like an int) are identical.

Because a String is an object, using == to compare two Strings is attempting to see if the objects themselves are identical, which they are not. For you to get the "equals A" result, you would need to compare s with itself.

A String's .equals() and .equalsIgnoreCase() are methods that compare the text value of the String object, which is what you are attempting to do.

The instanceof keyword is used to discover an object's type (e.g. if an object is of type String or of type Integer).

Upvotes: 0

SCdF
SCdF

Reputation: 59428

== is referential equals, as in, 'is this exact object the exact same object as they other object?'.

When you made s you made a new object, so it won't be the same object as the string you're comparing it too (depending on the JVM you're using "foo" == "foo" will be true because it will use the same object internally).

When you called .equals() it (the String s)ran it's own logic to determine that it's the same object. In the case of String, it will compare it character by character.

instanceof is something else entirely, and will tell you if an object is an instance of a certain type of object, e.g. "foo" instanceof String is true.

Upvotes: 4

Related Questions