Scala 趣题 14 当自己调自己的时候

真是无奇不有, 下面的两个println语句哪个抛空指针,那个输出8 ?


  val s1: String = s1
  println(s1.length)
  val s2: String = s2 + s2
  println(s2.length)























解释:

String类型默认被初始化成nulll

null引用出现在字符串的位置按照语言规范默认被转化成字符串“null”


Explanation


The definitions of the values s1 and s2 are valid and compile without an error. As the definitions are recursive, the type of the declared values has to be provided (SLS §4.1).


In Scala, object fields are preinitialized to their default value, and for the type String (and all other reference types) the default value is null.


This is as in Java. However, such a recursive definition of a field in a Java class body gives an illegal forward reference compiler error,


class Z {

   String s = s + s;

}


but the following compiles fine.


class Z {

   String s = this.s + this.s;

}


This demonstrates, that the checking which is done by the Java compiler is quite shallow. The Scala way to accept recursive definitions for all situations is more consistent.


What remains to explain is the result of this puzzler.


    Value s1 is initialized to null, therefore the expression s1.length ends in a NullPointerException at run-time.

    The initialization of the value s2 actually gets translated to byte code equvalent to


    s2 = (new StringBuilder()).append(s2).append(s2).toString()


    where the argument s2 has its default value null. According to the JLS, Section 15.18.1.1 the reference null is converted to the string "null", and the value which gets assigned to s2 is the string "nullnull" which has length 8. 


你可能感兴趣的:(scala)