简体   繁体   English

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

[英]what's the difference between the two initialization: Object x = new String(); String x = new String();

what's the difference between the two initialization: 两个初始化之间有什么区别:

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

in java 在java中

thank! 谢谢!

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

More importantly : The methods of String that can be accessed depend on the reference. 更重要的是:可以访问的String方法取决于引用。 For example : 例如 :

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

}

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

The difference is that in the first option x will be considered by the compiler as an Object and in the second it'll be considered as a String. 不同之处在于,在第一个选项中, x将被编译器视为Object,而在第二个选项中,它将被视为String。 Example: 例:

public static void main (String[] args) throws Exception {

    Object x = new String();
    test(x);
    String y = new String();
    test(y);
    // and you can also "trick" the compiler by doing
    test((String)x);
    test((Object)y);
}

public static void test(String s) {
    System.out.println("in string");
}

public static void test(Object o) {
    System.out.println("in object");
}

will print: 将打印:

in object
in string
in string
in object
Object x = new String(); 

here, x has access only to the Object 's methods and members. 在这里, x只能访问Object的方法和成员。 (To use String members, you have to typecast the x to String (using Downcasting in Java )) (要使用String成员,必须将x转换为String (使用Java中的Downcast ))

String x = new String();

here, x has access to all the methods and members of Object as well as String . 在这里, x可以访问ObjectString所有方法和成员。

Both are same, X will refer to the string object. 两者都相同,X将引用字符串对象。

But x variable in Object x = new String(); 但是Object x = new String(); x变量Object x = new String(); need to be Type casted to String , using x.toString() or (String)x before making use of it. 在使用它之前,需要使用x.toString()(String)x将类型转换为String。

As noted by the other answerers, both cases will result in the variable x holding a reference to a String. 正如其他回答者所指出的,两种情况都会导致变量x持有对String的引用。 The only difference is how subsequent code will see the reference: as an Object vs String, ie determining which operations the compiler will allow on the reference. 唯一的区别是后续代码将如何看待引用:作为Object vs String,即确定编译器将允许哪些操作引用。

Essentially, the question highlights the difference between a statically typed language (eg Java) and a dynamic one (eg Python, Javascript, etc.). 本质上,问题突出了静态类型语言(例如Java)和动态类型语言(例如Python,Javascript等)之间的区别。 In the latter you don't have to declare the type of the reference, so the code for this is simply something like: 在后者中,您不必声明引用的类型,因此这样的代码就像:

var x = new String();

In this case, the compiler infers the type at runtime , but you lose some static type checking at compile time . 在这种情况下,编译器在运行时推断出类型,但是在编译时会丢失一些静态类型检查。

In everyday Java code you will always use the form: 在日常Java代码中,您将始终使用以下表单:

String x = new String();

since that will allow you to treat x like a String and call, eg the method toUpperCase() on it. 因为这将允许你像对待字符串一样处理x并调用,例如对它的方法toUpperCase() If x were an Object , the compiler won't allow you to call that method, only Object methods, eg equals() . 如果x是一个Object ,编译器将不允许您调用该方法,只允许调用Object方法,例如equals()

However, the exact opposite is true in the following case: 但是,在下列情况下恰恰相反:

List list = new ArrayList();
ArrayList list = new ArrayList();

Unless you specifically want to use methods exposed by ArrayList (unlikely), it is always better to declare list as a List , not ArrayList , since that will allow you to change the implementation to another type of List later, without changing the calling code. 除非您特别想要使用ArrayList公开的方法(不太可能),否则最好将list声明为List而不是ArrayList ,因为这样可以让您稍后将实现更改为另一种类型的List,而无需更改调用代码。

Difference is you need to cast it a lot however you can use the same variable and reinitialize it with another type. 不同之处在于您需要多次使用它,但是您可以使用相同的变量并使用其他类型重新初始化它。 It is very usefull when you are working with changing variables.. 当您使用更改变量时,它非常有用。

Example: 例:

Object x = new String();
if(x instanceof String){
    System.out.println("String");
}
x = new ArrayList<String>();
if(x instanceof ArrayList){
    System.out.println("ArrayList");
}

returns: 收益:

String ArrayList String ArrayList

暂无
暂无

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

相关问题 “字符串”和字符串 x = “字符串”有什么区别 - What is difference between “string” and String x = “string” Java中新String()和新String(“”)的字符串初始化有什么区别? - What is the difference between String initializations by new String() and new String(“”) in Java? “文本”和新字符串(“文本”)有什么区别? - What is the difference between "text" and new String("text")? return(string expr)和返回New String(string expr)之间有什么区别? - What's the difference between return (string expr) and return New String(string expr)? java 中的 new String[]{} 和 new String[] 之间的区别 - difference between new String[]{} and new String[] in java java 中的 String[]::new 和 new String[] 有区别吗? - Is there a difference between String[]::new and new String[] in java? 调用Double.valueOf(String s)和new Double(String s)之间的区别是什么? - What's the difference between calling Double.valueOf(String s) and new Double(String s)? 字符串X,Y之间的区别; 和字符串X,字符串Y; - Difference between String X, Y; and String X, String Y; `String.class`和`new Class [] {String.class}`有什么区别? - What's the difference between `String.class` and `new Class[]{String.class}`? String s = new String(s.getBytes(“UTF-8”),“UTF-8”)之间的区别是什么? 和String s = new String(s.getBytes(),“UTF-8”); - What is difference between String s = new String(s.getBytes(“UTF-8”),“UTF-8”); and String s = new String(s.getBytes(),“UTF-8”);
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM