GIGO: words unreadable aloud
Mishrogo Weedapeval
 

 

  Monday 24 May 2004
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   comment/     


Click here to visit the Radio UserLand website. Click to see the XML version of this web page. © Copyright 2007 Doug Landauer .
Last update: 07/2/6; 12:39:55 .
Click here to send an email to the editor of this weblog.

May 2004
Sun Mon Tue Wed Thu Fri Sat
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Apr   Jun

Previous/Next