Chapter 4, Exercise 14
Bruce asks us to "Create a class containing an int and a char that are not initialized, and print their values to verify that Java performs default initialization."
But Scala requires that initialization to be a bit more explicit:
Try 1: val i : Int; gets this error message: class c4x14c needs to be abstract, since value i in class c4x14c is not defined.
Try 2: var i : Int; gets class c4x14c needs to be abstract, since variable i in class c4x14c is not defined ... (Note that variables need to be initialized to be defined).
Try 3 worked. The default value ("_") for Char appears to be zero, the NUL character.
package x;
class c4x14c {
var i : Int = _;
var ch : Char = _;
};
object c4x14 {
def main ( args: Array[String] ) = {
val ob = new c4x14c;
val obich : Int = ob.ch;
Console.println( "ob.i " + ob.i );
Console.println( "ob.ch [" + ob.ch + "] (or, as Int, " +
obich + ")." );
}
}
11:55:17 PM