0

I come up with the scenario & can't understand the logic behind it ? I understood the first one only, rest two unable to understand ? Please explain how internally it work?

public class Test {
    public static void main(String[] args) {

        String s = "Karan";
        Object o = "Karan";

        System.out.println(s.equals(o));         // True
        System.out.println(o.equals(s));        // True 
        System.out.println(s == o);            // True
    }
}
Karan Dodwani
  • 31
  • 1
  • 5
  • because both are string literals and pointed to same reference in string constant pool – Ryuzaki L Jan 04 '20 at 17:11
  • There is no 'object literal', both are string literals (which due to the string constant pool are resolved to the same instance), one just happens to be assigned to a variable typed as `Object` instead of `String`. The object itself however is still a `String`. – Mark Rotteveel Jan 04 '20 at 17:11
  • Can you be bit more precise? What exactly is confusing and why? – Pshemo Jan 04 '20 at 17:12
  • @Deadpool but we are creating String of Object type? then how it will point to SCP ? – Karan Dodwani Jan 04 '20 at 17:13
  • @Pshemo I don't understand why Object type of String pointing to SCP ? – Karan Dodwani Jan 04 '20 at 17:15
  • 2
    "but we are creating String of Object type" no, in case of `Object o = "Karan";` we are creating (or actually reusing from String Constant Pool) *object* of String type and we are storing *reference* to that object in *variable* of Object type. – Pshemo Jan 04 '20 at 17:15
  • Maybe this will help a little for a start: [What is the difference between a variable, object, and reference?](https://stackoverflow.com/q/32010172) – Pshemo Jan 04 '20 at 17:19
  • duplicate of this [https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Mandar Dharurkar Jan 04 '20 at 17:27
  • 1
    @MandarDharurkar no, that is not a duplicate. – Turing85 Jan 04 '20 at 17:28
  • Does this answer your question? [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Mandar Dharurkar Jan 04 '20 at 17:39

2 Answers2

1

There is a difference between the static type of an object and the dynamic type of a variable.

The static type is the type known at compile time. For Object o = "Karan";, the static type is the one at the left of o, i.e. Object. Now, one could ask why that is the case since at compile-time, it can be inferred that o is a String. Well if we consider something like

Object o;
if (someCondition) {
    o = "Karam";
} else {
    o = new StringBuffer();
}

then we cannot know whether o is a String or a StringBuffer, thus its static type is Object (since o is defined as Object).

The dynamic type is the type at runtime. In the example above, after the if-statement has been executed, o is either a String or a StringBuffer, but not both and nothing else. In your example, the dynamic type of Object o = "Karan"; is String.

When variables are compared through equals(...), they must have the same dynamic type in order for the comparison to return true (notice that this property is necessary, but not sufficient). Since o in your example is, in fact, a String AND the content is equal to s, s.equals(o) == o.equals(s) == true.

As to why s == o returns true: the question is answered in this post.

Turing85
  • 18,217
  • 7
  • 33
  • 58
  • Technically the "static type" only matters for **variables**. *Objects* only have the dynamic type. Understanding the distinction between the object and a variable that references that object is very important for this question. – Joachim Sauer Jan 04 '20 at 17:48
  • True that. I reworded my answer. – Turing85 Jan 04 '20 at 17:53
1

String s = "Karan" this is a String object referred by a String reference.

Object o = "Karan" this is a String object referred by an Object reference. Super class can refer to object of sub-class.

For both s.equals(o) and o.equals(s) the underlying objects equal() is called, since the objects are of type String (even if reference 'o' is of type Object) String's equals is called which compares the value of the string which are equal. That is what you got true as result.

s == o compares the object reference of s and o. They were created using double quote which uses the String pool, creates the object once and refers it again. So both s and o are exactly same String object. This makes == return true.

Salim
  • 2,046
  • 12
  • 13