Describes in detail the advanced Java optimization technology and the Liskov Substitution Principle)

Source: Internet
Author: User

I. Common Java advanced optimization techniques:

1. optimized the cycle. Re-organize repeated subexpressions to improve the running performance of the loop body.

2. Reduce the number of objects used to improve the running performance.

3. reduce network transmission data to shorten the wait time.

Others:

1. The object pool technology is used to improve the utilization efficiency of objects.

The loss of performance is mainly caused by the creation and release of objects. Therefore, the creation and release of objects must be avoided. The object pool technology is used to pre-define an object pool and create a group of objects to be used first:

Enemy [5] enemy = new Enemy [5];

Forint I = 0; I <5; I ++ ){

Enemy [I] = new Enemy );

}

Add a flag such as used and reset to identify the Enemy status. To create an object, obtain an unused object from the object pool and initialize it using the reset method. to release an object, you only need to modify the flag for the next use.
2. Try to replace the object with the basic data type

For example, a two-dimensional array is used to replace a simple object.

3. Optimization Algorithm

For example, scenarios and algorithms that do not require fine-grained writing are simulated using simple algorithms.

4. Other Optimizations

A. When extracting a string, try to return the substring instead of creating a copy.

B. Try to create as few temporary objects as possible.

C. If you can use database functions, it is optimized not to create database functions by yourself)

D. Map map = new HashMap );

HashMap map = new HashMap); // This has higher performance and code Reconstruction

E. When using the enhanced for loop and Iterable, you need to create an object with caution.

F. Avoid the enum type.

G. Pay attention to floating point usage during embedded development. Whether the processor supports floating point)

H. Compression of image resources. The size of one image in multiple image sets is much smaller than that of the other images. This saves data blocks such as each header file and end file, and merges the color palette)

Ii. liskey replacement Principle Liskov Substitution Principle), LSP for short

Definition:

Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it.

All references to the base class must be transparent to use its subclass objects.

That is to say, as long as the parent class appears, the Child class can appear, and replacing it with the Child class will not produce any errors or exceptions. However, if the child class is replaced with the parent class, a problem may occur.

This principle defines a standard for good inheritance. In simple terms, it has four meanings:

1. Subclass must fully implement the parent class Method

Define an abstract class

Public abstract class ViewPoint {// public abstract void where for traveling to Lijiang);} The following two classes implement this abstract class

Public class Lijiang extends ViewPoint {

@ Override public void where) {System. out. println "Welcome to Lijiang ...... ");}

} Public class Zhangjiajie extends ViewPoint {

@ Override public void where) {System. out. println "Welcome to Zhangjiajie ...... ");}

} The character is painted, and the class type is set in it to pass the parameter. At this time, the scenic spots to be painted are still Abstract

Public class Tutu {// define the private ViewPoint viewpoint of the Scenic Spot to be visited; // tu the public void setViewPointViewPoint viewpoint of the Scenic Spot to be visited)

{This. viewpoint = viewpoint ;}

Public void travelTo)

{System. out. println "painted to travel"); viewpoint. where);} scenario class. Set specific scenic spots to go

Public class Sence {public static void mainString args [])

{Tutu tutu = new Tutu); // set the destination tourist attraction tutu. setViewPointnew Lijiang); tutu. travelTo);} running result:

Tu is going to travel

Welcome to Lijiang ......

Ii. Subclass can have its own features

That is to say, other methods or attributes can be defined on the subclass of the class.

3. input parameters can be amplified when overwriting or implementing the parent class Method

Where the parent class can exist, the Child class can exist without changing the running result. Otherwise, no.

The parameters in the parent class, say) are of the HashMap type and are child types of the Map type. Because the subclass range should be larger than the parent class)

Import java. util. Collection; import java. util. HashMap;

Public class Father {public Collection sayHashMap map)

{System. out. println "parent class is executed ...... "); Return map. values);} subclass, say) the parameters in the parameter change to the Map type. The Map range is larger than the HashMap type and complies with the LSP principle. Note that the statement here is not a statement that overwrites the parent class, because the parameter types are different. It is heavy load.

Import java. util. Collection; import java. util. Map;

/** Subclass inherits all attributes of the parent class */public class Son extends Father {// method input parameter type public Collection sayMap map)

{System. out. println "subclass is executed ...... "); Return map. values);} scenario class

Import java. util. HashMap;

Public class Home {public static void mainString args [])

{Invoke );}

Public static void invoke)

{// Where the parent class exists, the subclass should be able to exist // Father f = new Father); Son s = new Son); HashMap map = new HashMap ); // f. saymap); s. saymap);} whether it is using the parent class or subclass to call the say method, the result is

The parent class is executed ......

However, if you change the say parameter in Father to Map and the say parameter in Son sub-class to HashMap, the result is changed

F. saymap) result: the parent class is executed ......

S. saymap) Result: The subclass is executed ......

This will cause logical confusion. Therefore, the preconditions of the method in the subclass must be the same or wider than that of the override in the parent class.

4. output results can be reduced when overwriting or implementing the parent class Method

In fact, similar to the above, that is, the Child class where the parent class can appear, and replacing it with a Child class will not produce any errors or exceptions, and the user does not need to know whether it is a parent class or a child class. But in turn, it will not work. Where a subclass appears, the parent class may not be able to adapt. After all, the subclass range must be greater than or equal to the parent class range)


 

This article is from the "Network Knight" blog and will not be reposted!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.