Java Programming Common Technology (recommended) _java

Source: Internet
Author: User

One: Place the string string in the front

To prevent nullpointerexception exceptions, we typically compare string strings to the left side of the Equals method, which effectively avoids

Null-pointer exception occurred.

In the first case, if variable is NULL, a null-pointer exception occurs, and in the second case, null-pointer exceptions do not occur, and no data is lost, even if variable is null. So suggest

When you are programming, put the direct amount on the left.

Second: Do not trust the early JDK API

In early programming, the JDK API is not very mature, such as the following code block:

File File=new file ("..."), file encapsulates a directory or file, calls the list () method, returns an array of strings if it is a directory, or null if it is not;

So we can add some checksums in advance, as shown in the following code:

Three: Do not believe-1

Explicitly stated in Javadoc string.indexof () If the return value is-1, the character does not exist in the string, or when we read the file using IO stream, it is also defined as-1

Reading is complete. The following code:

It seems to work this way at the moment, but if Java changes the encoding and the definition returns-2 is a better way, it is recommended that the second type be used.

Four: Avoid accidental assignment

The following code:

The first case: the = =, will be an unexpected 5 assignment as a variable, the second case: the situation does not occur unexpected assignment;

The third case: = = = is the same as the exact symbol in JavaScript, the same type, and the value is equal.

Five: Check null and length

Anyway, as long as you have a collection or an array, first make sure it exists and is not empty.

The code is as follows:

The second way is better, avoiding errors in earlier versions of JDK APIs.

Six: All the methods are final.

Prevents subclasses from inheriting all methods of the parent class.

The above Java programming commonly used technology (recommended) is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.