java--to develop good programming habits

Source: Internet
Author: User

Original address: HTTP://USER.QZONE.QQ.COM/1159340782/2

Sacrificing some performance slightly, for better readability and maintainability is a good choice. The following is not advocated and advocated by the wording for reference only.

The code does not advocate the wording

Serial number

The wording that is not advocated

Reason

1

Try to avoid random use of static variables

Static variables and class synchronization, the class does not unload, the object will reside in memory until the program terminates

2

Try to avoid too often creating Java objects

It takes time to create objects, and it is also a cost to reclaim objects, reuse objects as much as possible, and replace objects with basic data types or arrays.

3

Try to avoid using the Finalize method

resulting in greater GC burden and less program efficiency

4

Try to avoid repeated calculation of variables

such as traversing the length of the array, one calculation can

5

Try to avoid unnecessary creation

Creation of program logic control objects

6

Try to avoid using two-dimensional arrays

Two-dimensional arrays occupy much more space than one-dimensional arrays, about 10 times times

7

Try to avoid using split

Split because it supports regular expressions, is inefficient and consumes a lot of resources, it is recommended to use stringutils.split frequently split cache results under Apache

8

Try to avoid very large memory allocations

Continuous memory, wasting resources

9

Caution with exceptions

Create exception overhead, fortunately try-catch overhead

10

Minimizing IO operations

IO consumes system resources and uses cache as much as possible

11

Try to avoid unwanted instanceof operations

Always True

12

Try to avoid unwanted styling operations

--

13

Avoid unnecessary equations for the Boolean

Flag = = True

The wording advocated in the code

Serial number

The wording of the promotion

Reason

1

Try to use a single case on the right occasion

Reduce loading burden, shorten loading time and increase load efficiency

2

Use the final modifier as much as possible

Find inline all final methods with 50% better performance

3

Use local variables as much as possible

instance variables and static variables are built in the heap, slow, local variables are placed in the stack, fast

4

Handle the package type and basic type usage scenarios.

The wrapper type belongs to the object created in the heap, the base type is created in the stack, there are objects that need to handle the applicable wrapper type, others advocate the basic type

5

Using synchronized sparingly to minimize synchronized method

Implementation of synchronization system overhead, easy deadlock. Try to apply method synchronization instead of code block synchronization

6

Try to use StringBuffer and StringBuilder for string concatenation

Try to initialize StringBuffer and StringBuilder lengths, character arrays dynamically add strings

7

Try to replace objects with basic data types

such as String str = "Hello" string str = new string ("Hello"); The latter also creates an array of characters

8

Single thread try to use HashMap ArrayList

HashTable Vector uses a synchronization mechanism to reduce performance

9

Use HashMap as reasonably as possible

Using HashMap's construction method, initializing its size, scaling is a very expensive thing.

10

Try to free resources in the finally block

Avoid resource leaks

11

Try to replace A/B,A*B operations with bitwise operations

/is a very expensive operation

12

Release useless objects as early as possible

OBJ =null

13

Rational use of ArrayList and LinkedList

One is a linear table (query), one is a list (add and delete),

14

Try to copy an array using System.arraycope

is much faster than copying an array in a loop.

15

Try to cache frequently used objects

Hashmap,weekhashmap or third-party tools Ehcache Oscache Memcached

16

Large objects try to use Clone () without new

New object creation Cost large

17

Use the API provided by Java itself as much as you can

Don't explain

18

Using the fast-speed JDBC Driver

Database Connection Pool

19

Cache Datasorce

In order to re-invoke Jndi, you can cache the Datasorce that you want to use in the system

20

To make the Setget method of accessing the variables in the instance become final

can become inlined

21st

Charat instead of StartsWith

--

22

If there is only one character "instead" "

--

23

Constant string instead of StringBuffer

Reduce running overhead to improve performance

24

StringTokenizer instead of indexof substring

StringTokenizer parsing strings is easier and more efficient

25

Replace If-return else with a three mesh operation

--

java--to develop good programming habits

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.