Java coding considerations

Source: Internet
Author: User

1. Compare whether two floating point types are equal, including float and double. Do not use equal signs "=" for comparison, because floating point calculation may involve rounding calculation. If you need a relatively accurate comparison of values, such as the currency amount, you can consider using a fixed precision type, such as bigdecimal. For values that do not need to be accurate, consider equality within a certain range, for example, if (math. Abs (x-y) <. 0000001 ). For example:

 
If (x = y ){}

Can be modified

 
If (math. Abs (x-y) <. 0000001 ){}

2. Date formatting in multi-threaded concurrent applications. Note that simpledateformat is NOT thread-safe.

Simpledateformat internally uses class member variables to record values to ensure efficiency, so it is not thread-safe. Our applicationProgramDo not use simpledateformat as a class member variable. Otherwise, thread security issues may occur. The solutions are as follows:

1) Use simpledateformat as the method variable and define it directly inside the method. Although there is a garbage collection problem, it solves the thread security problem;

2) use fastdateformat in Apache commons-Lang, Which is thread-safe. However, it only provides the format date method, but does not provide the method to convert string to date, this is an deficiency;

3) If you do have a special liking for simpledateformat, you can use threadlocal to wrap the current one, or ensure thread security. For example:

Threadlocal <simpledateformat> df = new threadlocal <simpledateformat> () {public simpledateformat get () {simpledateformat SDF = new simpledateformat ("yyyy-mm-dd "); return SDF ;}}; public date dateformat (string datestring) throws parseexception {return DF. get (). parse (datestring );}

3. Try not to capture all exceptions through throwable in the cache

It can be seen from its javadoc that throwable is a superclass of all classes that can be thrown by the Java Virtual Machine, including errors and exceptions. error cannot be restored, and exception is acceptable.

4. concatenate strings and try not to conform to "+"

In small applications, especially client applications, this is not much different, but if you have experience in large-scale Internet application development, under the pressure of high concurrency, the excessive use of "+" to splice strings may have a significant impact on your application, or even the most serious case is to make the server unable to respond. Therefore, we recommend that you use stringbuffer to concatenate strings. In the case of a single thread, you can use stringbuilder to obtain better performance. This blog shows the performance of String concatenation by a blogger: http://blog.csdn.net/yirentianran/article/details/2871417.

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.