Java Specification Summary

Source: Internet
Author: User

1. The comparison of values between all the same types of wrapper class objects is all compared using the Equals method.

Description: for Integer var =? In the range of 128 to 127, the Integer object is generated in Integercache.cache,
The existing object will be reused, and the Integer value within this interval can be judged directly using = =, but all data outside this interval
will be generated on the heap, and will not reuse the existing objects, this is a big pit, it is recommended to use the Equals method to judge.

The Equals method of 2.Object is prone to throwing null pointer exceptions, and you should call equals using constants or determining which objects have values.

such as: "Test". Equals (object);

3. When using an index to access an array obtained using the split method of String, there is no content check after the last delimiter, otherwise there is a risk of throwing indexoutofboundsexception.

Such as:
String str = "a,b,c,,";
string[] ary = Str.split (",");
Expected to be greater than 3, the result is 3
System.out.println (ary.length);

4. In-class method definition order is: Public method or Protection method > Private Method > Getter/setter method.

Description: The public method is the caller of the class and the maintainer of the most concerned about the method, the first screen display the best; the protection method is only a subclass of care,
It may also be the core method under the template design pattern, while the private method does not need special care outside, it is a black box implementation;
Because the value of the method information is low, all Service and DAO getter/setter methods are placed at the end of the class body.

5. Do not perform the remove/add operation of the element in the Foreach loop. The remove element uses the Iterator method, which requires a lock on the Iterator object if the concurrency operation.

6. In JDK7 version and above, Comparator to meet the following three conditions, otherwise arrays.sort,collections.sort will report illegalargumentexception exception.

Description
1) The comparison between x, Y and y,x results in the opposite direction.
2) X>y,y>z, then x>z.
3) X=y, the x,z comparison result is the same as the y,z comparison.

7. With the unique features of the set element, it is possible to quickly redo a collection and avoid using the contains method of List to traverse, contrast, and redo operations.

8. If it is an application of JDK8, you can use Instant instead of date,localdatetime instead of Calendar,datetimeformatter instead of Simpledateformatter,
The official explanation: simple beautiful strong immutable thread-safe.

9. When expressing an abnormal branch, use less if-else, which can be modified as follows:
if (condition) {
...
return obj;
}

10. In addition to the usual methods (such as getxxx/isxxx), do not execute other complex statements in the conditional judgment, assign the result of complex logic judgment to a meaningful Boolean variable name, to improve readability.
such as: final Boolean existed = (File.Open (fileName, "w")! = null) && (...) | | (...) {
...
};
if (existed) {
...
}

11. The variables that are sent to the page in the background must be added $! {var}--an exclamation point in the middle.}
Description: If Var=null is not present, then ${var} will be displayed directly on the page.

    

Java Specification Summary

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.