[Solved] Performance of java if-else with return statement [duplicate]


As far as cpu time – NO DIFFERENCE. The compiler will probably optimise them to exactly the same byte-code anyway.

As far as technical debt is concerned – as soon as a real developer looks at this code and replaces it with:

private static final String[] numbers = {"ZERO", "ONE", "TWO"};

private String getString(int n) {
    return n >= 0 && n < numbers.length? numbers[n]:"-NA-");
}

you’ve just cost your company money.

solved Performance of java if-else with return statement [duplicate]