The old ape will also make mistakes and make mistakes.
Recently, many low-level errors have been found in the Process of continuously testing and completing functions under a project. These problems are basically discovered through breakpoint debugging, I have read the problematic code several times and I am also drunk ............ To remind yourself not to make the same mistake in the future, let's summarize the process ~~~
1. When I judge whether two String types of data are equal in java, I often use the equals method instead of "= ", because "=" is used to determine whether the references of two strings are the same, while the equals method is used to determine whether their values are equal. The first problem occurs here. if an operation in the project does not get the expected result, the problem is caused by another if condition, previously, programmers used "=" to make the if condition not true;
Map B = new HashMap ();
(1) B. put ("key", "1 ");
(2) B. put ("key", 1 );
String a = B. get ("key"). toString ();
If ("1" = ){
System. out. println (123456 );
} Else {
System. out. println (654321 );
}
The results of the above Code (1) and (2) are completely different.
2. When determining whether a String variable is null, You need to determine whether it is null because variable a is obtained from B of Map type. In jdk1.7, a method ".IsEmpty ()"This method is also used in the project, but the null pointer exception is always reported in the actual process. Because a is null rather than null in the project, a null pointer exception is reported. Therefore, when using this method, you must first judge whether the variable is null. The sample code is as follows:
Map B = new HashMap ();
/* B. put ("key ","");*/
String a = (String) B. get ("key ");
If (a. isEmpty ()){
System. out. println (123456 );
} Else {
System. out. println (654321 );
}
When you run the above Code, you will find that a null pointer exception is reported because a is null. However, if you cancel the annotation of the green part, this code runs normally, because a is null.