gpt4 book ai didi

java - 两次初始化: Object x = new String(); String x = new String();有什么区别

转载 作者:行者123 更新时间:2023-12-02 09:16:00 25 4
gpt4 key购买 nike

两种初始化有什么区别:

Object x = new String(); 
String x = new String();

在java中

谢谢!

最佳答案

Object x = new String(); // pointing to a String and saying - Hey, Look there! Its an Object
String x = new String();// pointing to a String and saying - Hey, Look there! Its a String

更重要的是:可以访问的 String 的方法取决于引用。例如:

public static void main(String[] args) {
Object o = new String();
String s = new String();
o.split("\\."); // compile time error
s.split("\\."); // works fine

}

关于java - 两次初始化: Object x = new String(); String x = new String();有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24524811/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com