Java learning handwriting 1

xiaoxiao2021-03-06  75

Last Monday and Tuesday participated in the training of Best Practise for WAS, benefiting a lot, but the information in his hand was too messy. Now, the Java programming optimization precautions are now organized, and the full text is as follows:

1. Some optimization techniques in the process of objects:

Do not generate objects in the loop;

Try to avoid using the packaging class object with the original data type;

The reference address is empty after the object is used.

Try to avoid definition and use class variables, but should try to use local variables;

LazeObjectCreation, generated when the object needs to be used, and then assign the value null;

The inheritance chain of the class should not be too long, because the construction method of the parent class will be called when the class is constructed, and the variable of the parent class will be initialized. If the inheritance chain will increase the generated cost of the class.

2. String and StringBuffer selection strategy:

String (Immutable is not variable), StringBuffer (Mutable variable), so three String objects appear when String = "Append"; this code, first "append" is Litral [J1], so it It is a String object, and String is an Immutable type, so it must generate a new String object to include the old String content and "append" value; and StringBuffer's Append method does not need to generate a new StringBuffer object, direct Add new content after the original StringBuffer object;

String (Immutable) is CompileTimeResolution, and StringBuffer is RuntimeResolution, so string = "aa" "BB" "CC" "DD" assignment does not generate three new String objects to save the middle The result of the operation, it will be optimized by the Java compiler to string = "aabbccdd";

StringBuffer is best specified when creating, and if not specified, the new memory will be allocated after the current default capacity is full, and the old and new content is copied to the new memory, which will overhead.

3. Treatment of abnormal optimization:

The most basic error handling class in Java is: java.lang.throwable, its derived subclass is ERROR and Exception, and Error's processing is generally done by JVM, for example: Example of Error is outofmemoryError .;

Do not use an exception to do anything outside the program, for example, to execute program logic, because abnormal processing is very resource-consuming in Java, a big overhead;

Try to avoid using the "Exception E" to capture an abnormality, try to use the expected exception type as the type parameters captured by the expected exception type;

Place the operation of resource release in FINALLY, so that instant abnormal occurs can also avoid resource leakage and idling.

If there is no design consideration, try to put an exception handling operation in a class that discovery an abnormally, avoiding the exterior to throw, the cost of abnormal throwing processing is very high;

Do not use TRY-CATCH inside the loop, but you should loop in TRY;

4. Optimization of cyclic processing;

Use the local variable int type as a cycle of index variable, if the function is used as a judgment condition, because the function's overhead factor is very inappropriate, if you use a Byte or Short type, these two types will be implicitly converted to Int Type; Use System.ArrayCopy () to avoid using loops to complete the copy of the array;

Use local variables in the cycle instead of directly using array elements;

If there are multiple logical expressions as a cycle, it is placed on the left when && operation is considered at && operation.

[J1] JVM MAINTAINS AN INTERNAL LIST OF REFERENCES for interned Strings (pool of unique strings). When JVM LOADS STRING LITERAL FROM Class File and Executes, IT

CHECKS WHETER THAT STRING EXISTS in The Internal List or Not.

转载请注明原文地址:https://www.9cbs.com/read-90713.html

New Post(0)