Generic Java security mechanism (JDK1.5) and java mechanism jdk1.5

Source: Internet
Author: User

Generic Java security mechanism (JDK1.5) and java mechanism jdk1.5

Generic, type security mechanism.

Benefits:

1. The ClassCastException during the running period is transferred to the compilation period, which facilitates solving the problem and reduces the problems during the running period and facilitates program robustness.

2. Avoiding the trouble of forced conversion

 

Generic Format:

ArrayList <String> al = new ArrayList <String> ();

If Integer data is added, an error is prompted during compilation.

 

When using the iterator value, you need to declare the type

 

Sample Code:

import java.util.*;class GenericDemo2 {    public static void main(String[] args)     {        TreeSet<String> ts = new TreeSet<String>(new LenComparator());        ts.add("abcdc");        ts.add("dbcdc");        ts.add("cd");        Iterator<String> it = ts.iterator();        while(it.hasNext()){            String s = it.next();            System.out.println(s);        }    }}class LenComparator implements Comparator<String>{    public int compare(String obj1, String obj2){        int num = new Integer(obj1.length()).compareTo(new Integer(obj2.length()));        if(num==0)            return obj2.compareTo(obj1);        return num;    }}

Question 1,

When can I use generics when using objects provided by java?

A:

It is usually common in the Collection framework. As long as you see <>, You need to define the generic

Interface Collection <E>: E represents a variable in the form of parameters.

In fact, <> is used to receive type.

When using a set, pass the data type to be stored in the set as a parameter to <>.

 

------------------------------------

// Generic class
Class Utils <T>
{
Private T t;
Public void setObject (T t ){
This. t = t;
}

Public T getObject (){
Return t;
}
}

 

Demo <String> d = new Demoe <String> ();

When the referenced data types to be operated in the class are uncertain, define the Object to complete expansion, and now define the generic type to complete expansion.

 

After the objects of a generic class define the specific type to be operated, the types to be operated by all methods are fixed.

 

------------------------------------------

To allow different methods to operate on different types, and the types are unknown, you can define generics on methods.

Class Demo {

public <T>void show(T t){

       

}

public <T>void print(T t){

     

}

}

 

Demo d = new Demo ();

-----------------------------------

Static methods cannot be generic defined on the category class.

If the referenced data type of the static method operation is uncertain, you can define the generic type on the method.

Public static <W> void method (W t );

 

--------------------------------------

Generic restrictions:

<? Extends Person>

? : Wildcard, which can be understood as a placeholder

----------------------------------

Limits on Generics:

For generic Extension

Upper Limit:

? Extends E: it can accept the E type or the E subclass type.

 

Lower limit:

? Super E: it can accept the E type or the parent type of E.

Only the parent class method can be used.


Is java generics very important? Even defining a linked list must be used. Otherwise, it is not a safe operation, so it is helpless.

Before Java SE 1.5, if there is no generics, the parameter "arbitrary" can be implemented by referencing the type Object ", the disadvantage of "arbitrary" is explicit forced type conversion, which requires developers to perform with predictable actual parameter types. If the forced type conversion is incorrect, the compiler may not prompt an error and the exception occurs during running. This is a security risk.
The advantage of generics is to check the type security during compilation, and all the forced conversions are both automatic and implicit, improving the code reuse rate.
Reference: baike.baidu.com/view/1436058.htm?sub1436058

In java, how does a generic type ensure type security?

To put it bluntly, it is to check the type that can only be run (Runtime) in the past, referring to compile (compile.
If the types do not match, the compilation will fail, so that the errors that can only be found during the previous running of the program can be found at the time of compilation, and the integrated IDE will tell you directly.

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.