Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 576009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:00:05+00:00 2026-05-13T14:00:05+00:00

Does anyone know why: public void foo() { System.out.println(Hello); return; System.out.println(World!); } Would be

  • 0

Does anyone know why:

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

Would be reported as an “unreachable error” under Eclipse, but

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

Only triggers a “Dead code” warning?

The only explanation I can think of is that the Java compiler only flags the first, and that some extra analysis in Eclipse figures out the second. However, if that is the case, why can’t the Java compiler figure out this case at compile time?

Wouldn’t the Java compiler figure out at compile time that the if(true) has no effect, thus yielding bytecode that is essentially identical? At what point is the reachable code analysis applied?

I guess a more general way to think of this question is: “when is the reachable code analysis applied”? In the transformation of the second Java code fragment to the final bytecode, I am sure that at some point the “if(true)” runtime equivalent is removed, and the representations of the two programs become identical. Wouldn’t the Java compiler then apply its reachable code analysis again?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:00:06+00:00Added an answer on May 13, 2026 at 2:00 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 346k
  • Answers 346k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can write any Javascript code inside the onclick attribute.… May 14, 2026 at 6:03 am
  • Editorial Team
    Editorial Team added an answer I now found that GetVersionEx returns Vista when my application… May 14, 2026 at 6:03 am
  • Editorial Team
    Editorial Team added an answer I don't think you can put a condition into the… May 14, 2026 at 6:03 am

Related Questions

Does anyone know why the following generates an error on VC9? class Elem; class
I know it isn't possible to inherit from a generic type parameter, but it
I have a colleague in my company whose opinions I have a great deal
Does anyone know why this will not compile? I've tried both VS 2008 and
I'm getting an IllegalCastException on the following (see bold line): public void renderXML(final String

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.