Which Java-specific annoyance fixed in Scala reduces surprises like the ones discussed in Java Puzzl

Posted by soc on Stack Overflow See other posts from Stack Overflow or by soc
Published on 2010-03-29T15:11:14Z Indexed on 2010/03/29 15:13 UTC
Read the original article Hit count: 429

Filed under:
|
|
|
|

Example: In Java this code falls through and prints "Mhhh..."

    Integer i = new Integer(1);
    Integer j = new Integer(1);

    if (i == j) {
        System.out.println("Equal");
    } else if (i < j) {
        System.out.println("Smaller");
    } else if (i > j) {
        System.out.println("Bigger");
    } else {System.out.println("Mhhh...");}

In Scala the equivalent code does not even compile:

    val a = new Integer(1)
    val b = new Integer(1)
    println { if(a == b) "Equal" else if(a < b) "Smaller" else if (a > b) "Bigger" else "Mhhh..."}

© Stack Overflow or respective owner

Related posts about java

Related posts about scala