Home  Synchronization and concurrency  wait/notify  final  volatile  synchronized keyword  Java threading  Deadlock (and avoiding it)  Java 5: ConcurrentHashMap  Atomic variables  Explicit locks  Queues  Semaphores  CountDownLatch  CyclicBarrier

Performance of Java 'final' (ctd)

Following from our description of a test to investigate the relative timings of calls on instances of final vs non-final classes, we thus have six different test configurations. These comprise the combinations of category (non-final, final, extended) and whether or not the precise subclass is known to the compiler. Figure 1 below shows the results of this test (mean timing to make a million calls in each of these six configurations).

Graph: final vs non-final, known vs unknown subclass

Figure 1: relative timings of method calls on classes that are non-final (and not extended), final and non-final but extended by another class, in cases where the precise subclass is known or not to the compiler. Each timing is actually the mean of 50 runs reported by System.nanoTime(), under Hotspo 1.6.0 64-bit running on an Intel i7 under Windows 7.

What we observe is that specifying a class as final makes no performance difference. There is no evidence of any supposed optimisation that the JVM can make based on whether or not a class is final (or indeed because there exist objects of another class extending the class in question).

Irrespectively of whether or not the class is final, there is a potential optimisation if the precise subclass is known.

Article written by Neil Coffey (@BitterCoffey).


 LetterMeister (word puzzle game for iPhone)
 Currency Quoter (currency converter/predictor)
 French Vocab Games for iPhone/iPad
 Vocabularium: create Spanish vocab podcasts

Java programming articles and tutorials on this site are written by Neil Coffey (@BitterCoffey). Suggestions are always welcome if you wish to suggest topics for Java tutorials or programming articles, or if you simply have a programming question that you would like to see answered on this site. Most topics will be considered. But in particular, the site aims to provide tutorials and information on topics that aren't well covered elsewhere, or on Java performance information that is poorly described or understood. Suggestions may be made via the Javamex blog (see the site's front page for details).
Copyright © Neil Coffey 2015. All rights reserved.