Some people says that Java is the most convenience and easy to use programming language. It is also the most popular programming language for enterprise. Even so, Java has few ridiculous APIs that programmers can hardly figure out how to use it correctly. In order to avoid above situation. I would like to recommend you to use FindBugs while programming in Java.
Installation
FindBugs is a free software, distributed under the LGPL license. JRE (or JDK) 1.5.0 or later is required to run this program.
Here I would like to take the most well-known IDE - Eclipse as an example.
- Fire the Eclipse
- Click "Help" -> "Software Updates"
- In "Available Software" section, press "Add Site" button
- add the url "http://findbugs.cs.umd.edu/eclipse" and click "OK"
- Check and install the findbugs.
Testing
Before testing the findbugs, we need a simple program which can be compiled successfully but failed to run correctly. Below is the best example for this testing.
import java.util.Random;
public class RoundDemo {
public static void main(String args[]){
Random rnd = new Random();
int a = rnd.nextInt();
if(Math.round(a) != a){
System.out.println("Not Equal");
}else{
System.out.println("Equal");
}
}
}
This is a typical example that will sometimes output the unreasonable result - "Not Equal", but seems to be no error with programmers' intuition. Cause Java will cast the variable "a" to float and then passed to Math.round. So, how to identify this bug with findbugs?
- Right Click your source code
- Click "FindBugs" -> "FindBugs"

You can get more detail information about this bug just by double-click the bug message.
Conclusion
FindBugs is a great tool for the programmers who don't have enough Java programming experience.
0 意見:
Post a Comment