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.