Tutorial :How to write efficient Java code? [closed]


As all of you may know, Java code is compiled and interpreted by JVMs. My questions deal with optimization: Is it optimized at run-time by the JVM only or also at compile-time?

In order to write efficient code, where can I find the list of supported optimizations? Or are JVM optimizations powerful enough so that I just have to write code that is readable and easy to maintain regardless of speed performance?


Most optimisation is done by the JVM. There's generally more scope for optimisation at the JIT level than at compile-time. (The "optimise" flag was actually taken out of javac, because it turned out that some "optimisations" actually hurt performance in the real world.)

In general (and this applies to many languages/platforms, not just Java):

  • Write code that is as readable and maintainable as possible.
  • Have performance goals and benchmarks so you can always measure performance
  • Put effort into making your architecture perform; it's hard to change that later (compared with changing implementation)
  • Think about performance more in terms of complexity of algorithms than "make it 10% faster": a change from O(n^2) to O(n) is (generally) much more important. (It very much depends on what the real world usage will be though - if you're only going to be dealing with small values of n, the "theoretically better" algorithm can easily end up being slower due to constant factors.)
  • Use a profiler to determine where your bottlenecks are
  • Only micro-optimise at the cost of readability when the profiler suggests it's worth doing
  • Measure after such an optimisation - you may not have actually had much impact, in which case roll back


The Java HotSpot JIT compiler can detect "hotspots" and adaptively modify the executing code so it delivers better perfomance. Read about it here.

On the other hand, if you want to write code which is efficient to begin with, read a book such as "Hardcore Java" by Robert Simmons or "Java Concurrency in Practice" by Brian Goetz.


I'd definitely choose writing code for readability and maintainability over supposed optimisations.

Premature optimisation is generally viewed as a bad thing. http://en.wikipedia.org/wiki/Optimization_(computer_science)#When_to_optimize

Of course, measuring and proving bottlenecks with profiling tools is another matter altogether. If you do this and can prove there are areas that need optimising and you can then measure the benefits go ahead and optimise away.


Is it optimized at run-time by the JVM only or also at compile-time ?

Java compilers typically do very little optimization (apart from resolving compund literals), since "optimized" bytecode may impede the ability of the JIT compiler to optimize - and that's where it really matters.

Or are JVM optimizations powerfull enough so that I just have to write code that is readable and easy to maintain regardless of speed performances?

It's not a question of trusting in the JVM to optimize better than you do (though this is definitely a factor), it's a question of optimization being completely irrelevant 95% of the time, since the code is not executed frequently. If a piece of code accounts for 0.1% of your app's execution time, it's simply not worth bothering with. Even if you can speed it up 100 times, it gains you nothing. And this is the most common case.

As long as you avoid blatantly stupid things, you should forget about optimization until you have a concrete performance problem, and then only optimize exactly the pieces of code that a profiler tells you are hot spots in your code.


Although these tips are meant for Google Android's specific Java implementation, I guess these Google Android performance tips apply in the "normal" JVM as well. Note that Android's Java does not optimize code at runtime as far as I'm aware.


Don't worry about specific JVM optimization. Those details change from version to version.

Buy the "Effective Java" book from Josh Bloch -- this is the best book on this topic.


JVM optimization changes from version to version.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »