Thursday, 16 June 2016

Can switch accept Object type?


I think, no.

This question comes from an open discussion.

Rules:
switch(x) may accept primitive and reference variable.

(1) primitives: byte, char, short, and int
(2) reference type: their boxing, i.e Byte, Character, Short, and Integer; as well as String and Enum

The above is all types that switch is able to handle.

The following code will give a compiling error, although reference types mentioned above are Object too. However, Object is not a String,  Byte or a Integer.

public class AcceptObject {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Object i = new Integer(9);
        
        switch(i){
            default:System.out.println("dd");
        }
        
    }
    
}


No comments:

Can Jackson Deserialize Java Time ZonedDateTime

Yes, but must include JSR310. Thus ZonedDateTime can be deserialized directly from JSON response to POJO field. <dependency> <g...