tags:

views:

147

answers:

5

I have this line of Java code:

return getValue() != null ? getValue() : 0

How often will getValue be executed? Once or twice?

EDIT: If this is compiler-dependant, I'm especially interested in the compiler from the Sun JDK.

+4  A: 

if getValue() == null - once

if getValue() != null - twice

Dev er dev
Of course the runtime bytecode -> machine code compiler (typically HotSpot), may inline it and do common subexpression elimination (CSE).
Tom Hawtin - tackline
Actually it cannot inline it, since it might have side effects, and compiler can't know that.
Dev er dev
The JIT-compiler COULD know if the method has side effects. The questions is just how much time it spends (if any at all) to test that.
Foxfire
No, it can't know, at least not in language like Java. You would have to mark each method in each class as side-effect free or not in order for JIT to figure it out. And bytecode doesn't have such info.
Dev er dev
Sorry but that is wrong. Though I don't know if the sun VM does this I have seen several JIT compilers that do this. And finding out if you method has side effects or not does not need any special bytecode support. As an example: if the implementation of getValue() is return null; then it is trivial to decide that there are no side effects. In fact in that case the compiler would optimize out the entire if-construct.
Foxfire
I have to append that the JIT compilers were compliant Java JITs.
Foxfire
+2  A: 

Cleared answer:

From a developer perspective:

if getValue() == null - Will be called once

if getValue() != null - Will be called twice

From the JIT-Compiler perspective:

Depends on the compiler and your method. It will be at most called 2 times and at least 0 times.

  • Two times if not null; the compiler doesn't optimize; or the method has side effects
  • Zero times if first call to getValue() ALWAYS return null and has no side effects and the compiler does that optimization
Foxfire
+3  A: 

only once if getValue() returns null, twice if the first time returned something other than null.

Galghamon
+2  A: 

If getValue() returns a non-null value, twice.

public class Test {
public static void main(String[] args) {
 Test t = new Test();
 boolean x = t.doSomething()? t.doSomething():false;
}
public boolean doSomething(){
 System.out.println("calling doSomething");
 return true;
}
}

output:

calling doSomething
calling doSomething
akf
This is probably a bad test. I don't think this can be optimized away at all. If you just had `return true;`, it might be optimized into one call.
Thomas Owens
I you do "return true" the whole expression might be replaced by "true", so it won't be called at all.
Dev er dev
+2  A: 

Could you re-write it? I'm not familiar with Java, but in C++ you could say

return (x=getvalue()) != null ? x : 0

And if that wouldn't work in Java, could you move the assignment before the return? Does it need to be a single line?

x = getvalue();
return x != null ? x : 0

John C>

John C