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 8081239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:40:12+00:00 2026-06-05T16:40:12+00:00

I would like to know is there any difference in performance between these two

  • 0

I would like to know is there any difference in performance between these two codes.

String sample="hello";
    if(sample!=null)
    {
       if(!sample.equals(""))
        {
            // some code in here
         }
    }

or

String sample="hello";
    if(sample!=null && !sample.equals(""))
    {

            // some code in here
    }

As far as i have understood, in the first code, if sample is not null then only it will go in to the block. same is the case with 2nd piece of code.
What i would like to know is what is the difference in performance or better coding standards and why?

  • 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-06-05T16:40:13+00:00Added an answer on June 5, 2026 at 4:40 pm

    If you’re asking about performance you should always measure. But No, there shouldn’t be a difference. Besides, if that is your only performance-problematic code then I envy you, seriously.

    As for coding standards. Less nesting is almost always nicer to read and follow. Which means that putting both in a single if, especially since they are related is preferrable. The pattern

    if (check_foo_for_null && compare_foo)
    

    is very common and thus much less surprising than another nested if.

    EDIT: To back it up:

    I have the two little methods:

    static boolean x(String a) {
        if (a != null && a.equals("Foo"))
            return true;
        else return false;
    }
    
    static boolean y(String a) {
        if (a != null) {
            if (a.equals("Foo")) {
                return true;
            } else return false;
        } else return false;
    }
    

    which produce the following code:

      static boolean x(java.lang.String);
        Code:
           0: aload_0       
           1: ifnull        15
           4: aload_0       
           5: ldc           #16                 // String Foo
           7: invokevirtual #21                 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
          10: ifeq          15
          13: iconst_1      
          14: ireturn       
          15: iconst_0      
          16: ireturn       
    
      static boolean y(java.lang.String);
        Code:
           0: aload_0       
           1: ifnull        17
           4: aload_0       
           5: ldc           #16                 // String Foo
           7: invokevirtual #21                 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
          10: ifeq          15
          13: iconst_1      
          14: ireturn       
          15: iconst_0      
          16: ireturn       
          17: iconst_0      
          18: ireturn       
    

    So apart from an extraneous else jump target the code is identical. If you don’t even have the else:

    static boolean z(String a) {
        if (a != null) {
            if (a.equals("Foo"))
                return true;
        return false;
    }
    

    then the result is really the same:

      static boolean z(java.lang.String);
        Code:
           0: aload_0       
           1: ifnull        15
           4: aload_0       
           5: ldc           #16                 // String Foo
           7: invokevirtual #21                 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
          10: ifeq          15
          13: iconst_1      
          14: ireturn       
          15: iconst_0      
          16: ireturn       
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know if there are any differences in between the two
Just I would like to know, is there any difference between if (a==5) or
I would like to know, if there are any performance differences using NFS instead
Is there any performance difference (in C++) between the two styles of writing if-else,
I'm new to PowerBuilder 12, and would like to know is there any way
I would like to know if there is any possibility to get a Path
I would like to know if there are any xml coding standards. <?xml version=1.0?>
I would like to know if there are any templates for doing this in
I would like to know if there is any managed or unmanaged library for
I would like to know if there are any tools to automatically generate EJB3

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.