Introduction to Apache Jakarta common Lang
No matter which Java application you are developing, you will inevitably need to write many tool classes/tool functions. You know, there are a lot of ready-made tool classes available, and the code quality is very good, you don't need to write, you don't need to debug, as long as you find out.
In Apache Jakarta common, the Lang Java toolkit is the most widely used among all Apache Jakarta common projects. It is useful in almost the well-known software you know, including tomcat, WebLogic, websphere, and eclipse. We will start from this package to introduce the entire common project.
Stringutils is the largest and most commonly used class in Lang, and isempty is the most commonly used function in stringutils. Here is an example to familiarize you:
String STR = NULL;
If (stringutils. isempty (STR )){
}
STR = stringutils. lowercase (STR );
STR = stringutils. capitalize (STR );
STR = stringutils. swapcase (STR );
Stringutils. isnumeric (STR );
Stringutils. isasciiprintable (STR );
Stringutils. inclusignorecase (STR ,"");
Where,
If (stringutils. isempty (STR )){
}
Equivalent
If (STR = NULL | Str. Length () = 0 ){
}
The code above shows the features of this tool class:
First, these functions are static functions and seldom throw exceptions or empty pointer exceptions.
Second, most of the functions of these functions are enhanced by the java. Lang. string class functions in the standard Java.
In fact, the functions of the entire lang package are basically enhancing the functions of the Java. lang package. An interesting design is that although all the functions in stringutils are static functions, the constructor will be designed to be private according to the common practice, but not! The stringutils constructor is public. The comments in the constructor are "no init ". This is interesting. You can create a new stringutils. Although it has no special advantages, it does not have any special disadvantages. You can write a subclass of stringutils, although this is not very common. The idea here seems to be that software developers who use this Toolkit can use it whenever they want, and there is no need to impose too many restrictions on tool classes.
As for not throwing an exception, many people have realized that exceptions in Java are not a good thing. If they do not need exceptions, they can write programs. Why should they use exceptions? When an exception is used, you need to write a lot of try catch. In many cases, such as JDBC driver, this has proved that the amount of code will increase and the code readability will deteriorate.
Will the idea affect the habits of writing programs?
There are many stringutils functions. Arrayutils is an important class in Lang. Its main functions involve operations such as array comparison, conversion, splitting, merging, searching, adding, removing, and reversing. In JDK, there are no ready-made functions for such operations.
There are many tool classes in Lang. Here we will introduce several main types:
Classutils getaskclassname. This function should be available in the Java. Lang. Class class. I see many people write this function by themselves. Getallinterfaces, convertclassnamestoclasses, isassignable, primitivestowrappers, and isinnerclass.
Numberutils numeric and string conversion classes such as stringtoint, todouble, createnumber, isallzeros, int compare (float LHS, float RHs), isnumber (string Str ), double min (double [] array ).
Randomutils is used to generate random numbers.
Dateformatutils date and time format conversion, and local time and UTC time conversion.
Dateutils date tool class. Issameday, truncate, round, modify.
Reflection-based classes:
Comparetobuilder is used for algorithm, sorting, and comparison. Reflectioncompare, append.
Equalsbuilder compares reflection mechanisms. Reflectionequals is used in many projects.
Hashcodebuilder can generate hash code through reflection. Many algorithms involve hash code, but not everyone knows how to generate hash code.
Tostringbuilder can be used when you need to reload the tostring function and do not want to write code to list all the member information of the current class.
I use less of the other classes:
Serialization in serializationutils Java is mysterious and error-prone.
Systemutils can be used to read JDK information and operating system information.