In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "what are the practical code optimization skills in Java work". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Minimize the visibility of class members and methods
For example: if it is a private method, delete it if you want.
If a service method of public, or a member variable of public, is deleted, you don't have to think a lot.
two。 Use displacement operation instead of multiplication and division
The computer is represented in binary, and the displacement operation will greatly improve performance.
Moving to the right is equivalent to dividing by 2
>
A = val > 1 * * 3. Minimize the double calculation of variables
We know that the call to the method is costly, including creating stack frames, protecting the scene when calling the method, restoring the scene, and so on.
/ / counterexample for (int I = 0; I
< list.size(); i++) { System.out.println("result");}//正例for (int i = 0, length = list.size(); i < length; i++) { System.out.println("result");} 在list.size()很大的时候,就减少了很多的消耗。 4.不要捕捉RuntimeException RuntimeException 不应该通过 catch 语句去捕捉,而应该使用编码手段进行规避。 如下面的代码,list 可能会出现数组越界异常。 是否越界是可以通过代码提前判断的,而不是等到发生异常时去捕捉。 提前判断这种方式,代码会更优雅,效率也更高。 public String test1(List list, int index) { try { return list.get(index); } catch (IndexOutOfBoundsException ex) { return null; }}//正例public String test2(List list, int index) { if (index >= list.size () | | index < 0) {return null;} return list.get (index);} 5. Use local variables to avoid allocation on the heap
Because heap resources are shared by multi-threads and are the main working area of garbage collector, too many objects will cause GC pressure, so variables can be allocated on the stack by means of local variables. In this way, variables are destroyed after the method is executed, which reduces the pressure on GC.
6. Reduce the scope of a variable
Pay attention to the scope of variables and minimize the creation of objects.
As in the following code, the variable s is created each time it enters the method, and can be moved inside the if statement.
Public void test (String str) {final int s = 100; if (! StringUtils.isEmpty (str)) {int result = s * s;}} 7. Lazy loading strategy
Try to use the strategy of lazy loading and create it when needed.
String str = "Moon flying fish"; if (name = = "official account") {list.add (str);} if (name = "official account") {String str = "moon flying fish"; list.add (str);} 8. Access static variables directly using class names
Use objects to access static variables, which requires one more step of addressing. You need to find the class corresponding to the variable first, and then find the variable corresponding to the class.
/ / counterexample int I = objectA.staticMethod (); / / positive example int I = ClassA.staticMethod (); 9. String concatenation using StringBuilder
String concatenation, use StringBuilder or StringBuffer, do not use the + sign.
/ / counterexample public class StringTest {@ Test public void testStringPlus () {String str = "111"; str + =" 222"; str + = "333"; System.out.println (str);}} / / positive example public class TestMain {public static void main (String [] args) {StringBuilder sb = new StringBuilder ("111"); sb.append (" 222"); sb.append (333) System.out.println (sb.toString ());} 10. Override the HashCode of an object
Override the HashCode of an object and do not simply return a fixed value
When developing rewriting HashCode and Equals methods, some students will return the value of HashCode to a fixed 0, which is not appropriate.
When these objects are stored in HashMap, the performance will be very low, because the HashMap is located to the Hash slot through HashCode, and when there is a conflict, the nodes will be organized using linked lists or red-black trees, and a fixed return of 0 is equivalent to invalidating the Hash addressing function.
11.HashMap and other collection initialization
Specify the size of the initial value when initializing collections such as HashMap
There are many such objects, such as ArrayList,StringBuilder. The performance loss caused by capacity expansion can be reduced by specifying the initial value.
Initial value size calculation:
twelve。 Create an object reference within a loop
Don't keep creating object references within the loop
/ / counterexample for (int I = 1; I
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.