Java1.5 Model

Source: Internet
Author: User

This article introduces three important features of j2se 5.0: Enumeration type, annotation type, and paradigm. On this basis, it introduces how to develop enumeration types in the eclipse 3.1 development environment, annotation type and fan type applications.

The release of j2se 5.0 (TIGER) is an important milestone in the development history of Java language and the greatest progress made so far in Java programming.

J2se 5.0 provides many exciting features. These features include support for general, enumeration, metadata, unboxing, and autoboxing ), varargs, static imports, and thread framework ).

With the release of j2se 5.0, more and more integrated development environments (IDES) Support j2se 5.0 development. The famous open-source Java ide eclipse has supported j2se 5.0 development since 3.1m4. The latest version is 3.1rc4.

This series will introduce three important features of j2se 5.0: Enumeration type, annotation type, and fan type. On this basis, we will introduce how to develop enumeration types in the eclipse 3.1 development environment, annotation type and fan type applications. This article introduces the model.

3. Fan type

Introduction to the 3.1 model (generics)
One of the most significant changes in j2se 5.0 is to add support for generic types. in j2se 1.4 and earlier versions, Java programs are not of type security. for example, the list, map, and other container classes defined in the Collection framework are all object types, that is, the elements contained in this class are object objects. the list implemented in this way can be used to operate integers, real numbers, strings, or any object type. for example

List 3.1.1 examples of Insecure code

List stringList = new ArrayList();stringList.add("abcde");String str = (String)stringList.get(0);

The list implemented in this method requires forced type conversion (also known as display Styling), so it is not type-safe. in the above Code, although the variable name is stringlist, we can still add an integer object to this queue. For example,

List 3.1.2 example of Insecure code

stringList.add(new Integer(5));

In this case, when an object is obtained from the character list, forced type conversion will cause an exception during running.

List 3.1.3 examples of Insecure code

String str = (String)stringList.get(1); //runtime exception

Pattern is an important step for Java towards type security. You can use the pattern to construct type-safe code.

3.2 declarative paradigm
The parameter type refers to parameterized types ). java is a strongly typed language. In j2se 1.4 and earlier versions, we must specify the variable type when defining a Java class, interface, or method. when declaring a fan type class, interface, or function, a type parameter is used instead of a specific type of some variables when defining a variable. when using this class, interface, or method, this type parameter is replaced by a specific type.

3.2.1 fan type

The following example describes how to create a simple fan class.

List 3.2.1, the simplest model class

public class GenSample<T> {}

<T> after the class name indicates that this class is a fan class, where T is a type parameter. When the fan type is used, the type parameter can be replaced with any class type, but cannot be the original type (primitive type), such as Int or double.

The following example uses a list to describe the usage of declared fan classes and type parameters.

List 3.2.2, Model List

public class GenList <T>{private T[] elements;private int size = 0;private int length = 0;public GenList(int size) {elements = (T[])new Object[size];this.size = size;}public T get(int i) {if (i < length) {return elements[i];}return null;}public void add(T e) {if (length < size - 1)elements[length++] = e;}}

In the list example, the type parameter T is used to represent the type of the element in the list, that is, the element in this list is of the T type.

When using this list, this type parameter T will be replaced by a specific type.

Note: Because t is not a specific class, you cannot use the new operator to create T objects, such as new T (), or, new T [10].

3.2.2 fan Interface

In j2se 5.0, you can declare not only the fan class, but also the fan interface. The syntax for declaring the fan interface is similar to that for declaring the fan class, add <t>. for example,

List 3.2.3, Fan Interface

public interface GenInterface<T> {    void func(T t);}

3.2.3 specify the model class or interface for declaring multiple type parameters

When declaring a fan class, multiple type parameters are used. Multiple type parameters are separated by commas (,). For example,

Listing 3.2.4: Fan classes of multiple type parameters

public class GenMap<T, V> {}

The eclipse 3.1 class creation wizard supports the creation of Fan classes, as shown in,

Figure 3.2.1 create a fan class using the Class Wizard

Use the eclipse interface Wizard to create a fan Interface

Figure 3.2.2 create a fan interface using the interface wizard

3.2.4 Model Method

Type parameter can be used not only to declare the fan class or fan interface, but also to declare the fan method, in addition, the declared model method can be used in a non-model class. The general format of the declared model method is

List 3.2.5 general form of the Model Method

<type-list> return-type method-name(parameter-list) {}

List 3.2.6 Sample Method

public <T> String getString(T obj) {return obj.toString();}

3.2.5 restricted fan type

A restricted parameter indicates that the value range of A type parameter is limited. the extends keyword can be used not only to declare the inheritance relationship of a class, but also to declare the restricted relationship of a type parameter. for example, we only need a list of numbers, including integers (long, integer, short) and real numbers (double, float). It cannot be used to store other types, such as strings ), that is to say, the value type T of the type parameter must be limited to the number extremely subclass. in this case, we can use the extends keyword to limit the type parameter to a number,

Listing 3.2.7 restricted examples

Public class limited <t extends number> {public static void main (string [] ARGs) {limited <integer> Number; // correct limited <string> STR; // compilation error }}

In eclipse 3.1, the compilation error information in the previous example is shown in

Figure 3.2.3 compilation errors caused by restricted dimensions

3.3 use the model in the program
3.3.1 Use a model class in a program

When creating a model class object, it is basically similar to creating a common object, the specific class type must be provided to replace the type parameter T (j2se 5.0 currently does not support the original type as the type parameter )).

List 3.3.1. Use the model class

// If you need an integer list genlist <integer> integerlist = new genlist <integer> (); // if you need to list the struct type genlist <string> strlist = new genlist <string> (); // you cannot use the original genlist type <int> NLIST = new genlist <int> (); // compilation Error

3.3.2 use the model to solve type security problems
The list implemented by the model is type-safe. The following statements that destroy the type security are checked during compilation. Place the mouse over the error mark, and the error prompt in eclipse 3.1 will be displayed, as shown in:

Figure 3.3.2 errors caused by damage type security

3.3.3 ambiguity Error

Genmap uses two type parameters T and V in the declaration. Therefore, when creating a genmap object, you must provide two specific class types to replace these two type parameters, for example,

Listing 9: Fan classes with multiple parameters

GenMap<Integer, String> gm = new GenMap<Integer, String>();GenMap<String, String> gm2 = new GenMap<String, String>();

In the above example, although T and V seem to be two different type parameters, T and V are likely to be replaced with the same type when this model class is used. therefore, when declaring fan classes of multiple type parameters, you must avoid this type of ambiguity error. For example,

Listing 10, ambiguity Error

Public class genmap <t, V >{// compilation error, ambiguity error public void set (t) {} public void set (V v ){}}

In the above Code, if T and V are replaced with the same type, the signature of the Set function is exactly the same, so the compiler reports a binary error. the correct method is to declare two methods with different names, for example,

Listing 10, ambiguity Error

public class GenMap<T, V> {    public void setKey(T t){}    public void setValue(V v){}}

Figure 3.3.3 ambiguity Error

3.3.4 use wildcards

We have created a fan type list. If I need a method to process the fan type list, for example, we want to print every element in the list, but the type parameter) it can only be used when a fan class is declared. If the type parameter is used in the function definition, compilation errors may occur.


Public static void print (genlist <t> List) {}// compilation Error

In this case, we need to use another method to represent a paradigm class. Otherwise, we may need to write multiple print functions.


public static void print(GenList<Integer> list){}public static void print(GenList<Double> list){}…public static void print(GenList<String> list){}

J2se 5.0 provides the wildcard "? ","? "Can be used to replace any type, for example, using wildcards to implement the print method


public static void print(GenList<?> list) {}

3.4. Limitations of the Model
(1) type parameters cannot be instantiated, for example,


T = new T (); // compilation Error

(2) arrays of type parameters cannot be instantiated.


T [] Ts = new T [10]; // compilation Error

(3) static variables of the class cannot be declared as type parameters.


Public class genclass <t> {Private Static t; // compilation error}

(4) The fan class cannot inherit from throwable and its subclass.


Public genexpection <t> extends exception {} // compilation Error

3. Summary
The fan type is a powerful function provided by j2se 5.0. You can use the fan type to create type-safe and reusable code, although the Java model cannot be compared with the c ++ model, with the evolution of Java language skills, the model will play a greater role in the Java language.

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.