Java model Overview
1. A brief understanding of Java paradigm
I often hear people say "paradigm". I have never been too explicit about "paradigm". I read some articles today. The first thing I feel is that I use paradigm in Java, this is to expose some errors in the compilation phase, rather than throwing exceptions in the running phase. The following is a simple example.
/***//**
* Examples that do not use a pattern
*/
Public void example1 ()...{
Arraylist array = new arraylist ();
Array. Add ("this is a string ");
Array. Add (New INTEGER (3); // You can correctly add
Iterator = array. iterator ();
While (iterator. hasnext ())...{
String STR = (string) iterator. Next (); // It is correct during compilation, but the classcastexception will be thrown during runtime.
System. Out. println (STR );
}
} Running the above program will throw Java. lang. classcastexception, Which is discovered only when the program is running. If we use the paradigm, exceptions will be found during the compilation phase to ensure the security of type conversion. As shown in the following program:
Public void example2 ()...{
Arraylist <string> array = new arraylist <string> ();
Array. Add ("this is a string ");
// Array. add (New INTEGER (3); // an exception is reported during compilation: The method add (string) in the type arraylist <string> is not applicable for the arguments (integer)
Iterator <string> iterator = array. iterator ();
While (iterator. hasnext ())...{
String STR = iterator. Next (); // The type conversion is not required here.
System. Out. println (STR );
}
}
In this way, we can capture potential risks in the compilation phase.
Through the above simple example, we can see that the advantages of using Java paradigm are:
Internal type conversion is better than external manual conversion
The type matching problem can be found in the compilation stage, instead of in the running stage.
2. Create your own Model
Any class, interface, exception, and method can use the model. The following is a simple example. The model is used to compare the size of two objects. Both objects must implement the comparable interface.
Public <t extends comparable> T max (T T1, t T2 )...{
If (t1.compareto (T2) <= 0 )...{
Return T2;
} Else ...{
Return T1;
}
}
Iii. References
This article from csdn blog http://blog.csdn.net/hbcui1984/archive/2006/10/21/1344522.aspx