Type checking for Java generic containers

Source: Internet
Author: User

A generic container is a type that is guaranteed by the compiler to ensure the correctness of the object type by specifying the container that contains the object, and the type error can be checked at compile-time. Assign the list<long> object longlist as follows to a list<generictest> Object gtlist , a compilation error is reported.

Public class generictest

{

&NBSP;&NBSP;&NBSP; &NBSP; public &NBSP; static &NBSP; LIST<LONG> &NBSP; longlist &NBSP; = Arrays. aslist   (1L, 2L);

public static void main (String args []){

// The following statement will be compiled with an error message

list<generictest> gtlist= longlist  ;         

}

}

If this is just a compile check, can we bypass the build check? Let's first assign the list<long> object longlist to a raw List variable Rawlist, and then converts the rawlist coercion type to the list<generictest> type. The result is compiled and the runtime does not have an error. We succeeded in tricking the compiler.

Public class generictest

{

public static list<long> longlist = Arrays. Aslist (1L, 2L);

public static void main (String args []){

List rawlist= longlist;

        // This can be compiled through, successfully fooled the compiler

List<generictest> gtlist= rawlist;

}

}

   then, if we uselist<generictest>Types of variablesGtlist,What's going to happen. NotegtlistThe container inside the reference is actuallyLongThe type object. What happens will it refer to a faulty memory? As a result, we foundString result = Gtlist.get (0). stringvalue;statement is correctly thrown at run timejava.lang.ClassCastExceptionexception. AnalysisMainThe byte code of the function, A type conversion check byte code was inserted before the statement, which caused the exception to be thrown. SeemsJavaThe designer has already taken this into account by adding a type check before the generic container object is used to prevent such a situation.

Public class generictest

{

public static list<long> Longlist = Arrays. Aslist (1L, 2L);

public Stringstringvalue ="ss";

public static void main (String args []){

List rawlist= longlist;

        // This can be compiled through, successfully fooled the compiler

List<generictest> gtlist= rawlist;

        // will be thrown at run time java.lang.ClassCastException

String result =gtlist.get (0). StringValue ;

}

}

Byte code analysis is as follows:

0 getstatic #28 <variable/GenericTest.longList>// Get class variables longlist, and put it on top of the stack

3 astore_1// put the top of the stack reference into the first local variable rawlist

4 aload_1// the first local variable rawlist , put on top of the stack

5 astore_2// Place the top of the stack reference into the second local variable Tglist, Here we can see the assignment of rawlist to tglist, without any type detection, so run through

6 Aload_2

7 Iconst_0

8 Invokeinterface #43 <java/util/List.get> count 2

checkcast #1 <variable/GenericTest>// Detecting Type Conversions , When we use objects in the container, there is type detection, which causes the java.lang.ClassCastException to be thrown

GetField #37 <variable/GenericTest.stringValue>// get instance properties and put them on top of the stack

astore_3// Place the top of the stack reference into the third local variable result

Return

Type checking for Java generic 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.