1

I am messing around a little bit with Java and the return statement of a void-method.

In each of the methods, the compiler within my Eclipse IDE (Eclipse Mars 4.5.2, Java 7) has a different output. I've written the warnings/the error as comments over the regarding line:

    public static void foo() {
        System.out.println("foo() - 1");
        boolean b = true;
        if (b)
            return;
        System.out.println("foo() - 2");
    }

    public static void foo2() {
        System.out.println("foo2() - 1");
        // compiler warning: "The value of the local variable b is not used"
        boolean b;
        if (b = true)
            return;
        System.out.println("foo2() - 2");
    }

    public static void foo3() {
        System.out.println("foo3() - 1");
        if (true) {
            return;
        }
        // compiler warning: "Dead code"
        System.out.println("foo3() - 2");
    }

    public static void foo4() {
        System.out.println("foo4() - 1");
        return;
        // compiler error: "Unreachable code"
        System.out.println("foo4() - 2");
    }

Each method has the same behaviour. The if-statement is true and the return-statement is invoked. The last System.out.println(..) isn't invoked any more. But why does the compiler outputs different things?

Thanks for your help!

Kind regards

P.S.: My Eclipse looks like that: enter image description here

P.P.S.: if I compile this file via javac Test.java I don't get any warnings, although I have to disable them manually by using -nowarn (javac documentation), but I did not do that:

enter image description here

mrbela
  • 4,477
  • 9
  • 44
  • 79

3 Answers3

2

One of the main task of Compiler is to check valid syntax

Last method has compilation Error because of after retrun statement no other statement possible.

And previuos methods are compiled because the syntax is correct even though there is clear dead code warning.

So try it other way.

mmuzahid
  • 2,252
  • 23
  • 42
1

Its simply because in the first 2 there is no garauntee that condition will not change in runtime (as far as the compiler concerns) , while in the last 2 there is no way the condition will ever change in runtime.

Whats weird is that putting a final there didnt help compiler realize the dead code, altough it is gauranteed that b will never change.

It seems that the compiler doesnt make any effort to evaluate your code in any level to find dead code...

Ofek Ron
  • 8,354
  • 13
  • 55
  • 103
1

Code analysis is sensitive to "Stop Problem" in general. To obtain some information about the code, you usually have to run the code (if code contains infinite loops, the analyser would hang on during analysis). Because of the issue, in foo and foo2 code analyser doesn't predict future code behaviour.

foo4: It is simply Java syntactic error. It is not allowed to write a code after return statement.

foo3: Code is syntactically correct, but as RC mentioned, code analyser integrated with IDE is able to perform simple detection of a branch that will be never fired.

mpasko256
  • 811
  • 14
  • 30