*
The moose likes Java in General and the fly likes Ternary operator v/s if condn Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Ternary operator v/s if condn" Watch "Ternary operator v/s if condn" New topic
Author

Ternary operator v/s if condn

Chinmay Bajikar
Ranch Hand

Joined: Dec 08, 2001
Posts: 159
Hi,
I want to know which of the two is more efficient.
The ternary operator or the if condition.
dont mind the readablity and easily understandable
parameters.
I just want to know which one is faster at any given time.
thankz,
Chinmay.


The strength of the Wolf is the pack & the strength of the pack is the wolf....Rudyard Kipling
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Firstly: don't ignore readability! (pray that the person writing the code that you will one day maintain believes in readable code!)
Here's the (hardly conclusive) test I ran. The strange values are to attempt to reduce the ability of compilers to optimise code...

The results I got were 1252 and 1061 ms.
After 100 million iterations.
So yes, there does appear to be approximately 20% better performance for the ternary operator, but unless you're calling it multiple millions of times, its unlikely to make any difference to your code and the ternary operator is evil.
Strong words? I'm not so sure. I used to use it fairly often and wrote some completely unreadable code using it. I'm not sure anyone will thank you for using it, specially when there is no perceivable advantage.
Dave
[ April 09, 2002: Message edited by: David O'Meara ]
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Without denying that you can write unreadable code using the ternary operator, isn'tclearer and more immediately expressive thanGranted, many developers will need to look twice at the first option because they're not used to seeing the ternary operator. But that's hardly an argument, is it? -- avoiding the ternary operator because developers aren't used to seeing it because the ternary operator tends to be avoided because developers aren't used to seeing it because...
Avoid nested ternaries like the plague tho'...
- Peter
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Originally posted by Peter den Haan:
Avoid nested ternaries like the plague tho'...

Why? Just because you "aren't used to seeing it"...


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Now, Dirk - please play nice .
(Ignore the fact that I am laughing please).


"JavaRanch, where the deer and the Certified play" - David O'Meara
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Dirk Schreckmann:
Why? Just because you "aren't used to seeing it"...
Oh believe me, Dirk, I'm very much used to seeing it alright and worse...
- Peter
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4777
    
    8

Originally posted by Peter den Haan:
Avoid nested ternaries like the plague tho'...

I recently read an article in JavaWorld that had some nested ternaries. I thought the formatting helped a lot in making it more readable although I still did a double take on it. I agree that you should generally avoid them.
I think it was something along the lines of
Sigfred Zamo
Ranch Hand

Joined: Mar 04, 2002
Posts: 44
I agree with Peter.
I think a Java programmer should know what does the ternary operator mean.
If not, he should find the meaning, and then, using it, because it�s cleaner and smaller.
This way, we�ll make grow every Java programmer knowlegde! (kidding)


Sigfred Zamo<br />Software Development<p><a href="http://www.sigfredo.ingenieros.net/index2.html" target="_blank" rel="nofollow">My page</a>
Martin Zisler
Greenhorn

Joined: Mar 14, 2008
Posts: 4
Hi there,

I was wondering how ternary and if / else operators would behave with Java 1.6.

I took the small test class mentioned at the beginning of this thread, played around with different numbers ... and my measures showed that runtimes are identical.

My suggestion: For better readability and lower maintenance costs: Avoid inline conditionals (ternary operator).

My conclusion: Modern compilers produce the same binary code independent what operator you use in the source file.

Cheers, Martin
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14278
    
  21

Let's see.

Have a look at this program:


Now, let's compile it and then look at the byte code.

javac Example.java
javap -c Example


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

public static void main(java.lang.String[]);
Code:
0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
3: bipush 13
5: invokestatic #3; //Method method1:(I)Ljava/lang/String;
8: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
11: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
14: bipush 13
16: invokestatic #5; //Method method2:(I)Ljava/lang/String;
19: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
22: return

public static java.lang.String method1(int);
Code:
0: iload_0
1: bipush 10
3: if_icmpge 11
6: ldc #6; //String Less than 10
8: goto 13
11: ldc #7; //String Not less than 10
13: areturn

public static java.lang.String method2(int);
Code:
0: iload_0
1: bipush 10
3: if_icmpge 9
6: ldc #6; //String Less than 10
8: areturn
9: ldc #7; //String Not less than 10
11: areturn

}


Note that the byte code of the two methods is almost exactly the same. There's a small difference, in that the version with the ternary operator contains a goto instruction, while the version with the if just immediately does areturn. Which of these is faster or more efficient is hard to say.

In practice you will not notice any difference between using a ternary operator or using an if statement.

Do not use one or the other just because you think one is more efficient than the other. These kind of micro-optimisations almost never make any sense.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

And if they make sense then it's certainly not "Beginning Java". This topic has changed into something that is more suited in Java in General. So I'm moving it.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Ternary operator v/s if condn