The old ape will also make mistakes and make mistakes.

Source: Internet
Author: User

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.

 

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.