aspose file tools*
The moose likes Performance and the fly likes ternary operator with strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "ternary operator with strings" Watch "ternary operator with strings" New topic
Author

ternary operator with strings

Mary McCartney
Greenhorn

Joined: Sep 26, 2005
Posts: 6
Consider these 2 methods, both of which convert null strings to empty strings:

private static final String N2E1(String str){
if (str == null){
str = "";
}
return str;
}

private static final String N2E2(String str){
return str == null ? "" : str;
}

Assuming it will be called a lot, is there a performance difference, or will the compiler just optimize N2E1 to do what N2E2 does anyway?

Thanks for your help!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

They would likely compile to identical bytecode.


[Jess in Action][AskingGoodQuestions]
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Almost.



compiles to

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by Ken Blair:
Almost.


Here the ternary operator compiles to slightly worse code!

But it's likely that HotSpot would optimize the double branch to the methodB version with the multiple returns.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
I'd also assume that a goto is a very cheap operation on most platforms.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Nishant Verma
Ranch Hand

Joined: Jun 14, 2006
Posts: 41
Ernest Friedman-Hill, Ken Blair, Ilja Preuss

You guys rock really


"Twenty years from now you will be more disappointed by the things that you didn't do than by the ones you did. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore. Dream. Discover." - Mark Twain
S Thanigaivel
Ranch Hand

Joined: Oct 06, 2005
Posts: 60
Hi Ken, can you explain in detail the below calculations and also how you derived it?


compiles to

[/QB]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

The disassembly comes from the "javap" tool that comes in the JDK. "javap -c MyClassName" will show a display like the one given here.
S Thanigaivel
Ranch Hand

Joined: Oct 06, 2005
Posts: 60
Originally posted by Ernest Friedman-Hill:
The disassembly comes from the "javap" tool that comes in the JDK. "javap -c MyClassName" will show a display like the one given here.



Thats interesting! Thank you Ernest
Mary McCartney
Greenhorn

Joined: Sep 26, 2005
Posts: 6
I was not aware of that javap tool. Thanks everyone for the info!
 
 
subject: ternary operator with strings