Another syntax for switch expressions

Apart from using -> to specify the return value, a switch expression can also use a colon (:) to mark the beginning of the code to execute and a break statement to return a value. Here's an example:

class Planet { 
    private static long damage; 
    public void use(SingleUsePlastic plastic) { 
        damage += switch(plastic) { 
            case STRAW : break 10;      // Use colon (:) to start code,  
// Use break to return val case BAG : break 11; case SPOON, FORK, KNIFE : break 7; case PLATE : int radius = 6; // no need
// of using curly brace break (radius < 10 ? 15 : 20); // Using
// break to // return a value case BOTTLE : break 20; }; } }
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.172.93