Jesus Zavarce
Jesus Zavarce

Reputation: 1759

Is there a performance penalty returning an int in a method that return a primitive double?

When you have a method that return a primitive double and you return an int value a Widening Primitive Conversion happens.

When we have a case like:

example 1:

double getSomething() {
   if (someConditon()) {
        return 0;
   }
   return someDoubleValue;
}

example 2:

double getSomething() {
   if (someConditon()) {
        return 0.0d;
   }
   return someDoubleValue;
}

Is there a performance penalty when you return an int value (example 1) instead of a double value (example 2) ?

If yes, the good practice is return the value with the accurate type. So example 2 that could return 0.0d instead of 0 ?

Upvotes: 1

Views: 157

Answers (3)

Durandal
Durandal

Reputation: 20059

It doesn't matter as long as the type of literal you type can be silently converted to double. The compiler will do the conversion at compile time, resulting in identical byte code for all of these (note even the weirder ones like long and char):

public class ReturnDouble {

    static double returnF() {
        return 0F;
    }

    static double returnD() {
        return 0D;
    }

    static double returnI() {
        return 0;
    }

    static double returnL() {
        return 0L;
    }

    static double returnS() {
        return (short) 0;
    }

    static double returnC() {
        return '\0';
    }

}

You can verify this by disassembling the class using the javap utility from the JDK. The disassembly output is:

public class ReturnDouble {
  public ReturnDouble();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  static double returnF();
    Code:
       0: dconst_0
       1: dreturn

  static double returnD();
    Code:
       0: dconst_0
       1: dreturn

  static double returnI();
    Code:
       0: dconst_0
       1: dreturn

  static double returnL();
    Code:
       0: dconst_0
       1: dreturn

  static double returnS();
    Code:
       0: dconst_0
       1: dreturn

  static double returnC();
    Code:
       0: dconst_0
       1: dreturn
}

Upvotes: 3

Cedric Reichenbach
Cedric Reichenbach

Reputation: 9319

Because your method declares that it will return a double it always needs to return a double. If you hardcode it, like in your examples, you can be sure that compilation will fail if types don't match and can't be converted.

TL;DR: Everything works in this particular case. If not, the compiler would tell you.

Upvotes: 0

Suresh Atta
Suresh Atta

Reputation: 121998

the value zero returned at the ex1, is changed to a double type value.

It's because the return type of it. Since you are returning an int primitive conversion will happen and int converted to double.

Since the conversion is int to double Widening Primitive Conversion- jls-5.1.2 happens

specific conversions on primitive types are called the widening primitive conversions:

byte to short, int, long, float, or double

short to int, long, float, or double

char to int, long, float, or double

int to long, float, or double

Upvotes: 1

Related Questions