Gibbs
Gibbs

Reputation: 22974

if-else and if only when method has return statement - are they same?

private Object methodName(){
    if(cond) {
      return x;
    }
    return null;
}

private Object methodName(){
    if(cond) {
      return x;
    } else {
      return null;
    }
}

Out of curiosity, I have got this question. I written the code in the second way initially. Then when I reviewing it again, I changed it to first way as both does what I need.

Is there really any difference the way JVM handles? I understand the use case of if and else, if then followed by statements. I just want to know about the mentioned "returning something" scenario.

Would time complexity be same for huge amount of method calls?

Upvotes: 1

Views: 74

Answers (1)

knittl
knittl

Reputation: 265956

Let's look at the byte code, shall we? I wrote a class containing 3 methods, one with if-else, one with if-only and one with the ternary operator (4th method rand() is nothing more than a helper to get a random true/false value):

// class version 55.0 (55)
// access flags 0x21
public class com.stackoverflow/Ifs {

  // compiled from: Ifs.java

  // access flags 0x1
  public <init>()V
   L0
    LINENUMBER 5 L0
    ALOAD 0
    INVOKESPECIAL java/lang/Object.<init> ()V
    RETURN
   L1
    LOCALVARIABLE this Lcom.stackoverflow/Ifs; L0 L1 0
    MAXSTACK = 1
    MAXLOCALS = 1

  // access flags 0x1
  public ifElse()Ljava/lang/String;
   L0
    LINENUMBER 7 L0
    INVOKESTATIC com.stackoverflow/Ifs.rand ()Z
    IFEQ L1
   L2
    LINENUMBER 8 L2
    LDC "result"
    ARETURN
   L1
    LINENUMBER 10 L1
   FRAME SAME
    ACONST_NULL
    ARETURN
   L3
    LOCALVARIABLE this Lcom.stackoverflow/Ifs; L0 L3 0
    MAXSTACK = 1
    MAXLOCALS = 1

  // access flags 0x1
  public ifOnly()Ljava/lang/String;
   L0
    LINENUMBER 15 L0
    INVOKESTATIC com.stackoverflow/Ifs.rand ()Z
    IFEQ L1
   L2
    LINENUMBER 16 L2
    LDC "result"
    ARETURN
   L1
    LINENUMBER 19 L1
   FRAME SAME
    ACONST_NULL
    ARETURN
   L3
    LOCALVARIABLE this Lcom.stackoverflow/Ifs; L0 L3 0
    MAXSTACK = 1
    MAXLOCALS = 1

  // access flags 0x1
  public ternary()Ljava/lang/String;
   L0
    LINENUMBER 23 L0
    INVOKESTATIC com.stackoverflow/Ifs.rand ()Z
    IFEQ L1
   L2
    LINENUMBER 24 L2
    LDC "result"
    GOTO L3
   L1
    LINENUMBER 25 L1
   FRAME SAME
    ACONST_NULL
   L3
    LINENUMBER 23 L3
   FRAME SAME1 java/lang/String
    ARETURN
   L4
    LOCALVARIABLE this Lcom.stackoverflow/Ifs; L0 L4 0
    MAXSTACK = 1
    MAXLOCALS = 1

  // access flags 0xA
  private static rand()Z
   L0
    LINENUMBER 29 L0
    INVOKESTATIC java/util/concurrent/ThreadLocalRandom.current ()Ljava/util/concurrent/ThreadLocalRandom;
    INVOKEVIRTUAL java/util/concurrent/ThreadLocalRandom.nextBoolean ()Z
    IRETURN
    MAXSTACK = 1
    MAXLOCALS = 0
}

In case you are interested in the uncompiled Java code, here it is for reference:

public class Ifs {
    public String ifElse() {
        if (rand()) {
            return "result";
        } else {
            return null;
        }
    }

    public String ifOnly() {
        if (rand()) {
            return "result";
        }

        return null;
    }

    public String ternary() {
        return rand()
                ? "result"
                : null;
    }

    private static boolean rand() {
        return ThreadLocalRandom.current().nextBoolean();
    }
}

Upvotes: 4

Related Questions