The idea here is not to remake the books “97 things every programmer…”
It is rather a non-exhaustive list of things that are not necessarily said when you learn java and that deserve to be looked at to improve:
- Java optimises the management of certain types of variables and uses “pools”: String pool, Long pool etc,
This explains several things: immutability of the strings, comparison == which works with Long objects from -127 to 128 but doesn’t work beyond that etc…
- There are several garbage collector. Choose the one that best suits the behaviour of your application.
Competitor Mark Sweep (CMS) Collector
G1 Garbage Collector
Z Garbage Collector
- When declaring an object, there are four types of references to choose from. However, the so-called “Strong References” type is always used.
Each type of reference interacts differently with the memory management of the garbage collector. There are therefore : Strong References, Weak References, Soft References, Phantom References
- When you’re starting out, you tend to pick up the Oracle Hotspot JVM. However, there are a multitude of JVMs: openJdk, graalVm, openJ9, amazon corretto etc. Each one has its specificities. To be chosen with full knowledge of the facts.
- The JVM optimises the code as it’s executed. It’s said to have a warm-up phase. It is admitted that it’s necessary to count approximately 10 000 executions for a maximum optimisation. Note: I don’t know if this is the case for all jvm, see the point above.
- It is possible to instrument the bytecode, i.e. modify the java program once it has been compiled in bytecode. This can be done using java agents. It can be useful for example to monitor the performance of your code.
- Primitive wrapper objects (Long, Integer, Short etc) manipulate the primitives internally. If you don’t need the objects’ methods, don’t be afraid to use the primitives directly, they are much faster to instantiate.
- Processor annotations are a very powerful tool. They allow you to generate java code at compile time (but not that, it can also be properties files etc.).
- An ArrayList has a default size of 10. Internally, java makes copies of arrays to increase the size of the list. To gain in performance, it is better to indicate the size of the list at its initialization (if you can have an idea of it, of course).
- Be careful, if you develop an application in a servlet container, the servlet container decides on its own concerning the number of class instantiations etc. The JEE spec strongly advises against instantiating threads in such an application.
- The comparison operator && will stop evaluating an expression when one of the members of the expression returns false.
The & operator will evaluate all members of the expression. Same for || and |
- Aspect oriented programming, annotations and api reflection are subjects to study to get the most out of java (frameworks such as Spring use a lot of them).
Here, I hope this “top” will change from what is usually found on the net and that it will be useful for you.
Don’t hesitate to tell us what you think about it as a comment.