Java-Paradigm Knowledge

Source: Internet
Author: User

I believe that a lot of people like me have not learned the concept of paradigm to start using paradigm examples, the most typical is the collection framework. To get a closer look at the paradigm, this time with a few simple examples to illustrate the paradigm's considerations.

A No paradigm of the world

All Java classes derive from Java.lang.Object, which means that all Java objects can be converted to object, which sounds wonderful, but that is not the case. Suppose, for example, that a group of people are needed to line up, asking only students to participate, but if there is no qualification for the team, it means that some of the groups we don't want will enter the army, which is not conducive to management. Again, the following paragraph does not use the generic code:

List string=new ArrayList ();

String.add ("I am the first element");

String.add ("I am the second element");

When you get a member out of it, it's an instance of Java.lang.Object, so if we want to use a string type of object, we have to make a strong transition, which undoubtedly increases the complexity of the code. But luckily, we have a paradigm.

Two Introduction to the type of paradigm

Like a method, a generic type can also accept parameters, declaring a generic type to enclose a list of type variables using angle brackets. For example, declare a list object:

List<e> MyList;

To instantiate a generic type, pass the same argument list when declaring it, for example, in order to create a ArrayList that uses string, the string is passed in a pair of angle brackets:

List<string> string=new arraylist<string> ();

However, after the JAVA7 version, you can explicitly pass parameters in the constructor of the parameterized class, and the preceding code can be changed to the following expression:

List<string> string=new arraylist< > ();

There is no doubt that a generic type can also specify an object type, but a generic type cannot be a direct or indirect subclass of java.lang.Throwable, because it throws an exception at run time, and therefore cannot see what exception is thrown at compile time.

The following code compares a list that uses a paradigm and an unused pattern:

Java code

Import java.util.ArrayList;  
Import java.util.List;  
      
public class Genericlisttest {public  
    static void Main (String args[]) {  
        //not using a generic queue  
        List stringlist=new ArrayList ();  
        Stringlist.add ("element 1");  
        Stringlist.add ("Element 2");  
        Requires strong transition  
        string s1= (String) stringlist.get (0);  
        System.out.println (S1);  
        Use the model queue  
        list<string> list=new arraylist<string> ();  
        List.add ("element 3");  
        List.add ("element 4");  
        No need for strong transition  
        String s2=list.get (0);  
        SYSTEM.OUT.PRINTLN (s2);  
    }  

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.