MyEclipse write Java code hints dead The reason for the code

Source: Internet
Author: User

Programmers who frequently use the MyEclipse or Eclipse editor to write Java code may often experience a yellow line warning: Dead code; Ordinary programmers will ignore these problems, anyway, without affecting the execution of the program's compilation. Yes, this is not a bug, just a hint, for a programmer with obsessive-compulsive disorder, he has no problem with the code, including Yellow line warning to be eliminated, here a simple dead code that is dead, the reason for the role of code hints and solutions.

As the name suggests, dead code, that is, you write the line is invalid code, dispensable, is a line of nonsense; this is what you need to look at the processing logic of this line, which may be redundant or redundant, such as the following:

1. Situation one: the useless condition judgment, is you judge of this condition forever is true

if (True & True) {
System.out.println ("execute OK");
} else {
System.out.println ("execute fail");
}

Starting from else is no action.

Because the true&true is known as the result when it is being compiled, the else part is useless, and the compiler knows the code that will not be executed.

Into:

Boolean A = true;
Boolean B = true;
if (A & B) {
System.out.println ("execute OK");
} else {
System.out.println ("execute fail");
}

The problem does not occur because the compiler is unsure at compile time whether A & B is constant.

2. Situation two: superfluous judgment, that you judge of this object is never empty; Actually, it's similar to a situation.

Timelineeventmodel Datamodel = new Timelineeventmodel ();

if (Datamodel!=null) {

Perform some action ...

}

The judgment here is also superfluous, because you have new this object, then this object will not be empty


To be continued, there may be some other cases of dead code, wait until the code met to add it. At present, dead code hints generally appear in the condition of if or other judgments.



Reprint please indicate: Http://blog.csdn.net/yangkai_hudong

Related Article

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.