• Post Reply Bookmark Topic Watch Topic
  • New Topic

which is better??  RSS feed

 
Vijay Raj
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which is better to use in terms of efficiency and not code readability.

1.




2.

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 15862
81
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In terms of efficiency, there is most likely no difference between those two at all. Try compiling both pieces of code and disassembling them with javap - you'll see that the bytecodes will probably be the same.

In any case, the difference will be so small that it's not worth the effort to worry about the difference.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 15862
81
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just tried it with the following source code.

I compiled this and disassembled it with "javap -c Jesper". Indeed, the bytecode for both constructions is exactly the same.

Line 0 to 15 is the code of the if-statement.
Line 16 to 30 is the code of the ? : statement.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!