In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
The content of this article mainly focuses on learning code optimization skills from the rule detection rules of the PMD tool. The content of the article is clear and clear. It is very suitable for beginners to learn and is worth reading. Interested friends can follow the editor to read together. I hope you can get something through this article!
Speaking of static code analysis tools, it is estimated that everyone is familiar with several, such as PMD, Checkstyle, Findbug and so on. Yes, these are good tools for us to improve the quality of our code when we code every day. Starting from the rules implemented by the PMD tool, we will learn the skills of code optimization through the definition of these rules. I hope that the introduction of these skills can help you improve the quality of your code writing in the coding process. At the same time, it is also a good way to learn how to improve the quality of code writing.
Here are some of the code optimization recommendations summarized from the PMD rules:
1. Local variables are assigned only once and can be declared as final
Note: after using the final logo, Java will automatically replace the changes with constants when compiling, so that the running efficiency of the program will be improved.
two。 If you pass in a literal parameter of length 1, this call to String.startsWith can save running time using the String.charAt (0) method
Description: on this point is easy to understand, String object itself is a char array, String.startsWith method in the internal processing is more complex, but also a circular comparison operation, so the efficiency is far less than the charAt operation.
"Abc" .startswith ("A") / / bad
"Abc" .charat (0) = ='A' / / good
3. String concatenation, using StringBuffer instead of using the "+" operator directly
Explanation: everyone knows about this. Because of the direct splicing of String objects, new String objects are generated for storage. StringBuffer will apply for a larger memory space (char array). For splicing, if it does not exceed the size of the char array, it will be appended directly to the end of the array. Note that the default size of StringBuffer is 16 characters, so it is recommended that you specify the size of the array you create when using it.
The indexOf method of 4.String, which uses char if the argument is a single letter
Abc ".indexOf (" A ") / / bad
Abc ".indexOf ('A') / / good
Description: String indexOf method, the search for String and char is using two methods, interested students can take a look at the original code of String, the method of finding String is more complex than finding char method, relatively time-consuming
5. It is not recommended to use a StringBuffer constructor with no arguments (the initial length is 16 characters)
Note: you may have some misunderstandings about StringBuffer, thinking that the initial space of StringBuffer is relatively large, but in fact, the default size of StringBuffer is only an array of 16 characters. Once it is exceeded, you need the newly applied array space, which will incur additional overhead.
6. Non-thread-safe singleton implementation of NonThreadSafeSingleton
The following code is a common one in a singleton implementation
But one of the problems introduced by this code is that running in multithreaded mode may lead to the problem of recreating objects. The solution is to add the synchronized keyword to the getFoo method
7. Both hashCode and equals methods need to be overridden
Explanation: this is also a problem that everyone occasionally ignores. The main uses of hashCode and equlas methods are in the collection classes of basic Hash algorithms, such as HashMap, and so on.
In the HashMap implementation, the logic for judging whether an opponent is equal is as follows:
That is, in HashMap, if two objects are equal, they must be hashCode equal and the equals method must return equality.
If in an object, only the equals is overridden, but the hashCode method is not overridden, the "duplicate" object will also occur in the HashSet.
Here is a sample code with a problem:
Use the String parameter for floating-point data values when the 8.BigDecimal object is initialized (for precision issues)
Note: for example, the real value of the new BigDecimal (0.1) value may be .10000000000000055511151231257827021181583404541015625
Thank you for your reading. I believe you have a certain understanding of "what are the code optimization skills of learning code optimization skills from the rule detection rules of the PMD tool?" go ahead and practice it. If you want to know more about it, you can follow the website! The editor will continue to bring you better articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.