java 具有空值的字符串构造函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4802474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 08:05:15  来源:igfitidea点击:

String constructor with null value

javastring

提问by Jesper

why String (String) constructor with null value cause compile-time error? I think there is 2+ constructor that takes Object and when init. it with null it doesn't know which to start. I'm wonder if there is another reason

为什么具有空值的 String (String) 构造函数会导致编译时错误?我认为有 2+ 个构造函数接受 Object 和 when init。它与 null 它不知道从哪个开始。我想知道是否还有其他原因

String s = new String(null); // compile time error


StringBuffer sb = new StringBuffer(null); // no error

回答by Adeel Ansari

Because, compiler couldn't figure out which constructor to call. See herethat how many one-argument-constructor it has.

因为,编译器无法确定要调用哪个构造函数。在这里看到它有多少个单参数构造函数。

[Edited]You said, if there is another reason. So why not try out yourself. Do something like this,

[编辑]你说,如果还有其他原因。那么为什么不自己尝试一下。做这样的事情,

byte[] b = null;
String s = new String(b); // complier should be fine with this

char[] c = null;
String s = new String(c); // complier should be fine with this

.... // you can try other constructors using similar approach.

回答by Jesper

Normally, when you call a constructor or method for which multiple overridden versions might apply, Java will choose the most specific constructor or method. Section 15.12.2of the Java Language Specification explains this in detail.

通常,当您调用可能应用多个覆盖版本的构造函数或方法时,Java 将选择最具体的构造函数或方法。Java 语言规范的第 15.12.2 节详细解释了这一点。

Suppose you have two overloaded methods, like this:

假设您有两个重载方法,如下所示:

public void method(Object o) {
    // ...
}

public void method(String s) {
    // ...
}

When you call method(null), both these methods apply. Java chooses the most specific one, which is in this case the second method, that takes a String- because Stringis a more specific type than Object.

当您调用 时method(null),这两种方法都适用。Java 选择最具体的一个,在本例中是第二种方法,它采用String- 因为String是比Object.

However, sometimes the most specific constructor or method cannot be determined. If we look at the constructors of class Stringthat take one argument:

但是,有时无法确定最具体的构造函数或方法。如果我们看看String带一个参数的类的构造函数:

String(byte[] bytes)
String(char[] value)
String(String original)
String(StringBuffer buffer)
String(StringBuilder builder)

Note that there is no hierarchy between the types byte[], char[], String, StringBufferand StringBuilder, so it's not possible to say that one of these constructors is more specific than the others. So, the Java compiler doesn't know which constructor to choose and will give you an error.

请注意,有各类型之间没有等级之分byte[]char[]StringStringBufferStringBuilder,所以它不可能说这些构造函数,一个是比别人更具体。因此,Java 编译器不知道选择哪个构造函数,并且会给您一个错误。

回答by Atul S.

Just to add more clarity to the solution:

只是为了增加解决方案的清晰度:

Take this example:

拿这个例子:

public class Test {
public Test(Object obj) {
    System.out.println("Object");
}
public Test(String obj) {
    System.out.println("String");
}
public static void main(String[] args) {
    new Test(null);
}

}

It will not throw any compile time error as compiler will choose the most specific constructor of available same types. However, if I change the class to:

它不会抛出任何编译时错误,因为编译器会选择可用的相同类型的最具体的构造函数。但是,如果我将课程更改为:

public class Test {
public Test(Long lg){
    System.out.println("Long");
}
public Test(Object obj) {
    System.out.println("Object");
}
public Test(String obj) {
    System.out.println("String");
}
public static void main(String[] args) {
    new Test(null);
}
}

It will throw compile time error. As two separate constructor hierarchies are involved here and compiler has no way to make a decision. One hierarchy is: Object-->String and second is Object-->Long so no way to choose between Long or String type constructors.

它会抛出编译时错误。由于这里涉及两个独立的构造函数层次结构,编译器无法做出决定。一个层次结构是:Object-->String 和第二个层次是 Object-->Long 所以无法在 Long 或 String 类型构造函数之间进行选择。

回答by u290629

String has manyconstructors. null is available for them.

String 有很多构造函数。null 对他们可用。

Edit: String has 5 one-argument-constructor, in Java 6. Thanks BoltClock and Adeel Ansari!

编辑:String 有 5 个单参数构造函数,在 Java 6 中。感谢 BoltClock 和 Adeel Ansari!