Java编译器:停止抱怨死代码

出于测试目的,我经常开始在现有项目中键入一些代码。所以,我想要测试的代码出现在所有其他代码之前,如下所示:
public static void main(String[] args)
{
    char a = '%';
    System.out.println((int)a);
    // To know where '%' is located in the ASCII table.

    // But, of course, I don't want to start the whole project, so:
    return;

    // The real project starts here...
}
但由于以下“死代码”,编译器抱怨
return
语句。 (在C ++中,编译器服从程序员并简单地编译return语句) 为了防止编译器抱怨,我写了一个愚蠢的
if
语句:
if (0 != 1) return;
我讨厌它。为什么编译器不能按我要求做?是否有一些编译标志或注释或其他什么来解决我的问题? 谢谢     
已邀请:
没有标志可以改变这种行为。使死代码成为编译时错误的规则是JLS(§14.21无法访问的语句)的一部分,无法关闭。 循环中存在明显的漏洞,允许这样的代码:
if (true) return;

someOtherCode(); // this code will never execute, but the compiler will still allow it
这是明确地允许“注释”或条件编译(取决于某些
static final boolean
标志)。 如果你很好奇:漏洞是基于这样一个事实:当检查
if
语句内或之后的代码的可达性时,不考虑
if
语句的条件表达式的已知常量值。类似的情况发生在
while
,其中考虑了已知常量值,因此此代码将无法编译:
while (true) return;

someOtherCode(); // this will be flagged as an unreachable statement
    
你不应该在你的项目中有很多死鳕鱼,但是我有两种方法来解决原型问题。 使用/ * * /注释掉代码。
    // But, of course, I don't want to start the whole project, so:
    /*
    // The real project starts here...


    */
}
或创建第二种方法。
    // But, of course, I don't want to start the whole project, so:
    // realProject();
}

public static void realProject()
    // The real project starts here...
}
    

要回复问题请先登录注册