JAVA.SWITCH.NODEFAULT

There is no default label in the switch statement.

Vulnerability and risk

If the default label does not exist, unexpected operations may occur due to function expansion.

Mitigation and prevention

Create a default label and explicitly describe the default behavior or the behavior when unexpected values ​​come.

Example 1

Copy
public class C03 {
    
    /* default located at first */
    public void testOK1(int num) {
        switch (num) {
        default:
            break;
            
        case 1:
            break;
        case 2:
            break;
        
        }
        
        
    
    }
    
    /* default located at last */
    public void testOK2(int num) {
        
        switch (num) {            
        case 1:
            break;
        case 2:
            break;
        default:
            break;
        }
    }
    
    /* no default */
    public void testNG2(int num) {
        switch (num) {            
        case 1:
            break;
        case 2:
            break;
        }
        
    }

}