March 2019
Intermediate to advanced
242 pages
6h 21m
English
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;
};
}
}
Read now
Unlock full access