当我们传递一个空字符串[duplicate]时,valueOf的String方法返回4

时间:2022-08-07 23:08:44

This question already has an answer here:

这个问题在这里已有答案:

I just found something interesting.

我发现了一些有趣的东西。

public class test {

    public static void main(String a[])
    {
        System.out.println(String.valueOf(null).length());
    }

}

Output

产量

Exception in thread "main" java.lang.NullPointerException
    at java.lang.String.<init>(Unknown Source)

This is what I kind of expected.

这是我所期待的。

But when i run this

但是,当我运行这个

public class test {

    public static void main(String a[])
    {
        String s=null;
        System.out.println(String.valueOf(s).length());
    }

}

Output

产量

4

There are two overloaded version of valueOf which gets called,they are

有两个被重载的valueOf版本被调用,它们是

/**
     * Returns the string representation of the <code>Object</code> argument.
     *
     * @param   obj   an <code>Object</code>.
     * @return  if the argument is <code>null</code>, then a string equal to
     *          <code>"null"</code>; otherwise, the value of
     *          <code>obj.toString()</code> is returned.
     * @see     java.lang.Object#toString()
     */
    public static String valueOf(Object obj) {
    return (obj == null) ? "null" : obj.toString();
    }

    /**
     * Returns the string representation of the <code>char</code> array
     * argument. The contents of the character array are copied; subsequent
     * modification of the character array does not affect the newly
     * created string.
     *
     * @param   data   a <code>char</code> array.
     * @return  a newly allocated string representing the same sequence of
     *          characters contained in the character array argument.
     */
    public static String valueOf(char data[]) {
    return new String(data);
    }

I didn't get why valueOf(Object s) method is giving special treatment to null. Thoughts/Comments?

我没理解为什么valueOf(Object s)方法对null给予特殊处理。思想/评论?

2 个解决方案

#1


3  

The problem is from the method invocation logic of JLS.

问题来自JLS的方法调用逻辑。

JLS's Choosing the Most Specific Method states,

JLS选择最具体的方法状态,

If more than one member method is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen.

如果多个成员方法都可访问并适用于方法调用,则必须选择一个为运行时方法调度提供描述符。 Java编程语言使用选择最具体方法的规则。

Now, in your first case, when you pass null directly,

现在,在您的第一种情况下,当您直接传递null时,

System.out.println(String.valueOf(null).length());

Both String.valueOf(Object) and String.valueOf(char[]) are applicable... So it uses the most specific method which is a char[].

String.valueOf(Object)和String.valueOf(char [])都适用......因此它使用最具体的方法char []。

The informal intuition is that one method is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time error.

非正式的直觉是,如果第一个方法处理的任何调用都可以传递给另一个没有编译时错误的调用,那么一个方法比另一个方法更具体。

But in your second case, you are actually passing a String even though it's null.

但是在你的第二种情况下,你实际上是在传递一个字符串,即使它是空的。

So only String.valueOf(Object) is applicable.

因此只有String.valueOf(Object)适用。

#2


2  

From valueOf docs

来自valueOf docs

if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.

如果参数为null,则字符串等于“null”;否则,返回obj.toString()的值。

#1


3  

The problem is from the method invocation logic of JLS.

问题来自JLS的方法调用逻辑。

JLS's Choosing the Most Specific Method states,

JLS选择最具体的方法状态,

If more than one member method is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen.

如果多个成员方法都可访问并适用于方法调用,则必须选择一个为运行时方法调度提供描述符。 Java编程语言使用选择最具体方法的规则。

Now, in your first case, when you pass null directly,

现在,在您的第一种情况下,当您直接传递null时,

System.out.println(String.valueOf(null).length());

Both String.valueOf(Object) and String.valueOf(char[]) are applicable... So it uses the most specific method which is a char[].

String.valueOf(Object)和String.valueOf(char [])都适用......因此它使用最具体的方法char []。

The informal intuition is that one method is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time error.

非正式的直觉是,如果第一个方法处理的任何调用都可以传递给另一个没有编译时错误的调用,那么一个方法比另一个方法更具体。

But in your second case, you are actually passing a String even though it's null.

但是在你的第二种情况下,你实际上是在传递一个字符串,即使它是空的。

So only String.valueOf(Object) is applicable.

因此只有String.valueOf(Object)适用。

#2


2  

From valueOf docs

来自valueOf docs

if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.

如果参数为null,则字符串等于“null”;否则,返回obj.toString()的值。