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

  • SEARCH
  • Home
  • 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 7601401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:09:19+00:00 2026-05-30T23:09:19+00:00

In situations such as the one in the java snippet below, is it faster

  • 0

In situations such as the one in the java snippet below, is it faster or better practice to surround the return false with an else block compared to having it just after the else block?

public boolean test(boolean var){
    if (var == true){
        return true;
    }
    return false;
}

compared to

public boolean test(boolean var){
    if (var == true){
        return true;
    }else{
        return false;
    }
}

Obviously this is an extremely simple example which can be shortened to one line, but my question is whether there is a jvm optimization which makes it more efficient to use the else block in the second snippet, or if it’s better practice since both snippets are logically equivalent. So far, I have been using the first snippet simply because there’s a little less code.

  • 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-30T23:09:20+00:00Added an answer on May 30, 2026 at 11:09 pm

    I compiled both examples and the resulting bytecodes are identical:

      public boolean test(boolean var);
        0  iload_1 [var]
        1  ifeq 6
        4  iconst_1
        5  ireturn
        6  iconst_0
        7  ireturn
          Line numbers:
            [pc: 0, line: 5]
            [pc: 4, line: 6]
            [pc: 6, line: 8]
          Local variable table:
            [pc: 0, pc: 8] local: this index: 0 type: scratch.Else1
            [pc: 0, pc: 8] local: var index: 1 type: boolean
    
      public boolean test(boolean var);
        0  iload_1 [var]
        1  ifeq 6
        4  iconst_1
        5  ireturn
        6  iconst_0
        7  ireturn
          Line numbers:
            [pc: 0, line: 5]
            [pc: 4, line: 6]
            [pc: 6, line: 8]
          Local variable table:
            [pc: 0, pc: 8] local: this index: 0 type: scratch.Else2
            [pc: 0, pc: 8] local: var index: 1 type: boolean
    

    As for which one you should use? In this contrived example, the answer is a resounding NEITHER. The best code that matches the behavior is this:

    public boolean test(boolean var){
        return var;
    }
    

    Given the choices of “if { return } else { return }” vs. “if { return } return”, my answer is usual the latter because I prefer less indentation. But I think it really depends on how much code is in each branch. More else code points to the latter, more if code points to the former.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In what situations should one catch java.lang.Error on an application?
Using ASP.NET MVC there are situations (such as form submission) that may require a
Just a note : I know that in Java finally block should never throw
I know about the existance of question such as this one and this one
In situations where two interfaces apply to an object, and there are two overloaded
In situations where a browser is under intense load I find that animated gifs,
In which situations should I choose to use a functional programming language over a
Are there any situations when you would use assertion instead of exceptions-handling inside domain
In many situations, JavaScript parsers will insert semicolons for you if you leave them
What are some situations where unit testing and TDD and the like are more

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.