简体   繁体   English

字符串字面量和对象字面量比较

[英]String Literal and Object Literal comparison

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
    }
}

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";对于Object o = "Karan"; , the static type is the one at the left of o , ie Object . ,静态类型是o左边的那个,即Object Now, one could ask why that is the case since at compile-time, it can be inferred that o is a String .现在,人们可能会问为什么会这样,因为在编译时,可以推断oString 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 ).那么我们无法知道oString还是StringBuffer ,因此它的静态类型是Object (因为o被定义为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.在上面的例子中,在if语句被执行之后, o要么是一个String要么是一个StringBuffer ,但不是两者都不是。 In your example, the dynamic type of Object o = "Karan";在您的示例中,动态类型Object o = "Karan"; is String .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).当变量通过equals(...)进行比较时,它们必须具有相同的动态类型,以便比较返回true (请注意,此属性是必要的,但还不够)。 Since o in your example is, in fact, a String AND the content is equal to s , s.equals(o) == o.equals(s) == true .由于o在您的示例中,实际上是一个String并且内容等于ss.equals(o) == o.equals(s) == true

As to why s == o returns true : the question is answered in this post .至于为什么s == o返回true :这个问题在这篇文章中得到了回答。

String s = "Karan" this is a String object referred by a String reference. String s = "Karan"这是一个 String 引用所引用的 String 对象。

Object o = "Karan" this is a String object referred by an Object reference. Object o = "Karan"这是一个由 Object 引用引用的 String 对象。 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.对于s.equals(o)o.equals(s) ,底层对象equal()被调用,因为对象是 String 类型(即使引用 'o' 是 Object 类型)String 的 equals 被调用,它比较了相等的字符串的值。 That is what you got true as result.这就是你得到true的结果。

s == o compares the object reference of s and o. s == o比较 s 和 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.所以 s 和 o 都是完全相同的 String 对象。 This makes == return true.这使得==返回 true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM