Java model Overview

Source: Internet
Author: User

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

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.