Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

ternary operator with strings

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They would likely compile to identical bytecode.
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Almost.



compiles to

 
Ernest Friedman-Hill
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd also assume that a goto is a very cheap operation on most platforms.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill, Ken Blair, Ilja Preuss

You guys rock really
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was not aware of that javap tool. Thanks everyone for the info!
 
Catch Ernie! Catch the egg! And catch this tiny ad too:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic