Java-13.1 generics and containers (container evolution)

Source: Internet
Author: User

Java-13.1 generics and containers (container evolution)

In this section, we will discuss generics and containers. Specifically, they are caused by container requirements.

During programming, most of us use containers to load objects. Let's take a look at the evolution of containers.

1. Common container

 

package com.ray.ch11;public class Test {public static void main(String[] args) {Holder holder = new Holder(new RealObject());RealObject realObject = holder.getRealObject();}}class RealObject {static {System.out.println(loading object);}}class Holder {private RealObject realObject;public Holder(RealObject realObject) {this.realObject = realObject;}public RealObject getRealObject() {return realObject;}}

The code above demonstrates the container that can load objects of the RealObject type. However, it is less practical because it can only store a single type.

 

 

2. Universal container

 

package com.ray.ch11;public class Test {public static void main(String[] args) {Holder holder = new Holder(new RealObject());RealObject realObject = (RealObject) holder.getObject();Holder holder2 = new Holder(new RealObject2());RealObject2 realObject2 = (RealObject2) holder2.getObject();}}class RealObject {static {System.out.println(loading object);}}class RealObject2 {static {System.out.println(loading object2);}}class Holder {private Object object;public Holder(Object object) {this.object = object;}public Object getObject() {return object;}}

Let's modify the code above and change the type of the holder to Object. In this case, the Holder can load containers of any type objects.

 

Although the above containers are omnipotent, they generally do not use various types of objects during programming. Most of the time, they store a single type of objects, forced conversion is required in the process of use. This is a big problem because you do not necessarily know the type of the object to be converted. Therefore, you need to introduce generics.

 

3. Generic containers

 

package com.ray.ch11;public class Test {public static void main(String[] args) {Holder
 
   holder = new Holder
  
   (new RealObject());RealObject realObject = (RealObject) holder.getObject();Holder
   
     holder2 = new Holder
    
     (new RealObject2());RealObject2 realObject2 = (RealObject2) holder2.getObject();}}class RealObject {static {System.out.println(loading object);}}class RealObject2 {static {System.out.println(loading object2);}}class Holder
     
       {private T object;public Holder(T object) {this.object = object;}public Object getObject() {return object;}}
     
    
   
  
 

We modify the container again to introduce the generic feature. At this time, the container basically meets the requirements, and we can also perform type check during compilation to ensure the correctness of the type.

 

 

4. generic core concepts

That is, I tell the compiler what type of container I want to use, and then the compiler will handle some details as needed (for example, type detection)

 

Summary: This chapter describes the evolution of containers and the relationship between generics and containers.

 

 

 

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.