For many students who have just started learning Java, how to better learn the relevant theoretical knowledge of Java is very important. In addition to writing more code, learning some simple and effective Java performance optimization tips will not only help you use the Java programming language better, but also improve your application performance! Next, I will share with you the knowledge of Java performance optimization!
1. Avoid regular expressions
Regular expressions give the impression of being quick and easy. But using regular expressions in the N.O.P.E branch is the worst decision you can make. If you must use regular expressions in computationally intensive code, at least cache Pattern to avoid repeating Pattern compilations.
Regular expressions are useful, but they come at a cost. Especially deep in the N.O.P.E branch, avoid regular expressions in your code at all costs. Also be wary of the various JDK string methods that use regular expressions
2. Do not use the iterator() method
This advice does not apply to general situations, but only to scenes deep in the N.O.P.E branch. Still, it’s worth knowing. Iterators, Iterable interfaces, and foreach loops are useful both from a readability perspective and from an API design perspective. The trade-off is that using them creates an additional object on the heap for each child of the loop.
3. Don’t call expensive methods
Some methods are expensive. In the case of the N.O.P.E branch, we did not mention the method for leaves, but this could be done. Suppose our JDBC driver needs to calculate the return value of the resultSet.wasNULL () method against all the difficulties.
Caching method calls instead of expensive methods on leaf nodes, or avoiding expensive method calls if method conventions allow.
4. Use primitive types and stacks
The example from jOOQ described above uses a lot of generics, resulting in wrapper classes for Byte, short, int, and Long. But at least generics should not be a limitation of code until they are specialized in Java 10 or Valhalla projects.
So an effective optimization method is to use primitive data types, fixed-length arrays, and a series of split variables to identify the position of the object in the array.