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

后端 未结 8 1402
眼角桃花
眼角桃花 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:39

    The first does not compile (you got an error), the second compiles (you just got a warning). That's the difference.

    As to why Eclipse detects dead code, well, that's just the convenience of an integrated development tool with a built-in compiler which can be finetuned more as opposed to JDK to detect this kind of code.

    Update: the JDK actually eliminates dead code.

    public class Test {
        public void foo() {
            System.out.println("foo");
            if(true)return;
            System.out.println("foo");
        }
        public void bar() {
            System.out.println("bar");
            if(false)return;
            System.out.println("bar");
        }
    }
    

    javap -c says:

    public class Test extends java.lang.Object{
    public Test();
      Code:
       0:   aload_0
       1:   invokespecial   #1; //Method java/lang/Object."":()V
       4:   return
    
    public void foo();
      Code:
       0:   getstatic       #2; //Field java/lang/System.out:Ljava/io/PrintStream;
       3:   ldc             #3; //String foo
       5:   invokevirtual   #4; //Method java/io/PrintStream.println:(Ljava/lang/StrV
       8:   return
    
    public void bar();
      Code:
       0:   getstatic       #2; //Field java/lang/System.out:Ljava/io/PrintStream;
       3:   ldc             #5; //String bar
       5:   invokevirtual   #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
       8:   getstatic       #2; //Field java/lang/System.out:Ljava/io/PrintStream;
       11:  ldc             #5; //String bar
       13:  invokevirtual   #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
       16:  return
    
    }
    

    As to why it (Sun) doesn't give a warning about that, I have no idea :) At least the JDK compiler has actually DCE (Dead Code Elimination) builtin.

    0 讨论(0)
  • 2020-11-28 07:39

    The if (true) is a little more subtle than "unreachable"; because that hard-coded return will always make the following code unreachable, but changing the condition in the if could make the following statement reachable.

    Having a conditional there means that there's a chance condition could change. There are cases where something more complicated than a true is in the parentheses, and it's not obvious to the human reader that the following code is "deadened," but the compiler notices, so it's able to warn you about it.

    Eclipse is mentioned here, and it makes things seem a little more complicated to the user; but actually underneath Eclipse is just a (very sophisticated) Java compiler that happens to feature a lot of switches for warnings etc. that Eclipse can switch on and off. In other words, you don't get quite the breadth of different warnings/errors from a straight javac compile, nor do you have convenient means to turn all of them on or off. But it's the same deal, just with more bells and whistles.

    0 讨论(0)
提交回复
热议问题