soc*_*soc 1 java null interop scala overloading
考虑这些方法 java.lang.String
/**
* 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);
}
Run Code Online (Sandbox Code Playgroud)
和这个Scala代码
val result = String.valueOf(null)
Run Code Online (Sandbox Code Playgroud)
结果
String.valueOf(null)
java.lang.NullPointerException
at java.lang.String.<init>(String.java:193)
at java.lang.String.valueOf(String.java:2852)
Run Code Online (Sandbox Code Playgroud)
如果您使用null除以外的任何其他类型进行注释,它将起作用Array[Char].
为什么Scala编译器似乎更喜欢Array[Char]来Object考虑Array是有效的最终的,不能被覆盖,并在其元素类型不变(使这种用法意的机会是非常小的)?
| 归档时间: |
|
| 查看次数: |
194 次 |
| 最近记录: |