哪个“ if”结构是快语句还是三元运算符?

在 java 中有两种类型的 if语句: if {} else {}和简写: exp ? value1 : value2。一个比另一个快还是一样?

声明:

int x;
if (expression) {
x = 1;
} else {
x = 2;
}

三元操作员:

int x = (expression) ? 1 : 2;
90949 次浏览

neither - they will be compiled to the same.

Both of your examples will probably compile to identical or nearly identical bytecode, so there should be no difference in performance.

Had there been a difference in execution speed, you should still use the most idiomatic version (which would be the second one for assigning a single variable based on a simple condition and two simple sub-expressions, and the first one for doing more complex operations or operations that do not fit on a single line).

These are the same. Both of them are fairly fast, typically around 10-30 nano-seconds. (depending on usage pattern) Is this time frame important to you?

You should do what you believe is clearest.

There's only one type of "if" statement there. The other is a conditional expression. As to which will perform better: they could compile to the same bytecode, and I would expect them to behave identically - or so close that you definitely wouldn't want to choose one over the other in terms of performance.

Sometimes an if statement will be more readable, sometimes the conditional operator will be more readable. In particular, I would recommend using the conditional operator when the two operands are simple and side-effect-free, whereas if the main purpose of the two branches is their side-effects, I'd probably use an if statement.

Here's a sample program and bytecode:

public class Test {
public static void main(String[] args) {
int x;
if (args.length > 0) {
x = 1;
} else {
x = 2;
}
}


public static void main2(String[] args) {
int x = (args.length > 0) ? 1 : 2;
}
}

Bytecode decompiled with javap -c Test:

public class Test extends java.lang.Object {
public Test();
Code:
0: aload_0
1: invokespecial #1
4: return


public static void main(java.lang.String[]
Code:
0: aload_0
1: arraylength
2: ifle          10
5: iconst_1
6: istore_1
7: goto          12
10: iconst_2
11: istore_1
12: return


public static void main2(java.lang.String[
Code:
0: aload_0
1: arraylength
2: ifle          9
5: iconst_1
6: goto          10
9: iconst_2
10: istore_1
11: return
}

As you can see, there is a slight difference in bytecode here - whether the istore_1 occurs within the brance or not (unlike my previous hugely-flawed attempt :) but I would be very surprised if the JITter ended up with different native code.

Just to add to all the other answers:

The second expression is often called tertiary/ternary operator/statement. It can be very useful because it returns an expression. Sometimes it makes the code more clearer for typical short statements.

Ternary operator is faster than if-else condition.

public class TerinaryTest {
public static void main(String[] args)
{
int j = 2,i = 0;
Date d1 = new Date();
for(long l=1;l<100000000;l++)
if(i==1) j=1;
else j=0;
Date d2 = new Date();
for(long l=1;l<100000000;l++)
j=i==1?1:0;
Date d3 = new Date();
System.out.println("Time for if-else: " + (d2.getTime()-d1.getTime()));
System.out.println("Time for ternary: " + (d3.getTime()-d2.getTime()));
}
}

Test Results:

Trail-1:

Time for if-else: 63

Time for ternary: 31

Trail-2:

Time for if-else: 78

Time for ternary: 47

Trail-3:

Time for if-else: 94

Time for ternary: 31

Trail-4:

Time for if-else: 78

Time for ternary: 47