Continue with the summary of code optimization.
7. Cycle optimization
List alist =getlist ();
for (int i=0;i<alist.size (); i++) {}
The code will always execute Alist.size ()
should be amended to read
for (int i=0,p=alist.size (); i<p;i++) {}
8. Objects that "eliminate" the non-visual stage
try{
Object Obj=new object ();
}catch (Exception e)
{
Obj=null; Add this code here
}
9. Create objects less with new
When you use the keyword new to create an instance of a class, all constructors in the constructor chain are called automatically, but if an object implements the Cloneable interface, we can call its clone () method.
public static Credit Getnewcredit () {
return new credit ();
}
Modified to:
private static Credit Basecredit = new Credits ();
public static Credit Getnewcredit () {
return (Credit) Basecredit.clone ();-
}
When new creates an object, try to create the object when it is used
NewObject obj=new NewObject ();
if (i>0) {
Value=object.getvalue ();
}
Modified to:
if (i>0) {
NewObject obj=new NewObject ();
Value=object.getvalue ();
}
10. Release unused objects in a timely manner
For example, when a is not empty,
A=new Object ();
Modified to:
A=null;
A=new Object ();
11. Use your own method
When working with strings, use as much of the methods as possible with objects such as String.IndexOf (), String.LastIndexOf (), and so on, than do the same thing in a Java loop 10~100 times.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Improve Code Performance Efficiency summary (b)--java