Unreachable code error vs. dead code warning in Java under Eclipse?

后端 未结 8 1466
眼角桃花
眼角桃花 2020-11-28 06:59

Does anyone know why:

public void foo()
{
    System.out.println(\"Hello\");
    return;
    System.out.println(\"World!\");
}

Would be rep

8条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-28 07:32

    I made some try on eclipse and think there are 3 kinds of dead code handling of JDK: 1) no warn, 2) warn and 3) error.

    For a typical "IF" conditional compilataion code, JDK detect this and didnt report it as dead code. For a dead code that is caused by a constant boolean flag, JDK detect this and report it at warning level. For dead code that is cause by the program's control flow, JDK detect it as error.

    Below is my try:

        public class Setting {
            public static final boolean FianlDebugFlag = false;
        }
    
    
        class B {
        .....
    
        // no warn, it is typical "IF" conditional compilataion code
        if(Setting.FianlDebugFlag) 
            System.out.println("am i dead?");   
        if(false) 
            System.out.println("am i dead?");   
    
    
        // warn, as the dead code is caused by a constant boolean flag
        if(ret!=null && Setting.FianlDebugFlag) 
            System.out.println("am i dead?");   
    
        if(Setting.FinalDebug)                  
            return null;                                                            
        System.out.println("am i dea?");        
    
        // error, as the dead code is due to the program's control flow
        return null;
        System.out.println("am i dead");        
        }
    

提交回复
热议问题