两个初始化之间有什么区别:Object x = new String(); String x = new String();

lys*_*030 2 java

两个初始化之间有什么区别:

Object x = new String(); 
String x = new String();
Run Code Online (Sandbox Code Playgroud)

在java中

谢谢!

The*_*ind 5

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
Run Code Online (Sandbox Code Playgroud)

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

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

}
Run Code Online (Sandbox Code Playgroud)


Smu*_*tje 5

初始化没有区别,只在声明中,因此代码的其余部分看到变量类型.