About the cast of the list generics

Source: Internet
Author: User

When we query some data from the database, sometimes the result of the return may be the list<object> type, and we know clearly that its exact type is list<user>, and maybe we want to go directly to the type conversion, which you might write:

Dbutils.list ("from User") return is list<object> type

List<user> listuser= (list<user>) dbutils.list ("from User");

However, the editor hints are not convertible, which is why AH? Our project development often like the following to use the strong turn can be AH:

Object o= new Object ();

User u= new User ();

Down transformation

u= (User) o;

o= u;

Look at the above code, is a very normal conversion ah, even if the parent class down to subclass is no problem although there is a type safety hazard, the following is my projection, because object is the parent of all types, so any object can be converted to object, because with this inherited attribute, It is possible that object points to a user, and we need to use this user method, so it supports a forced downward transformation of the security implications, although there is a security risk, but as long as we encode a little attention to avoid this vulnerability, because we know that this object is pointing to a user, Therefore, a downward-type conversion with inheritance is an avoidable security risk.

But why did list<user> and list<object> turn?

We can think of list<user> and list<object> as two separate types (I don't know how to explain this at the moment, because I don't know about Java's underlying generic processing, which I understand for the time being), such as:

String s= "a";

Integer i= 12;

I= (Integer) s;

You will find that the string does not strongly go to shaping, the compiler prohibits this is to make the type more secure, if any type can be converted to each other, it will lead to the type of insecurity, because there is no inheritance between them, if the hasty conversion will occur inevitably type of security risks.

Since we cannot convert directly, but do not want an element of the type of the transition, then we will indirectly to convert it, the following is the indirect conversion of several ways:

1, this method is not feasible, although the compile period does not error, but the operation of the conversion failed, the reason for the failure of the array conversion and the generic strong failure should be the same, are independent types, and other types have no connection, the strong turn of the array in the compile period check out, use to pay attention to this.

List<user> listuser= arrays.aslist ((user[]) dbutils.list ("from User"). ToArray ());

2

List<user> listuser= arrays.aslist (Dbutils.list ("from User"). ToArray (new user[0]);

3, this method is not feasible, although the compile period does not error, but the operation of the conversion failed, the reason for the failure of the array conversion and the generic strong failure should be the same, are independent types, and other types have no connection, the strong turn of the array in the compile period check out, use to pay attention to this.

list<user> listuser= new linkedlist<user> ();
Collections.addall (Listuser, (user[]) dbutils.list ("from User"). ToArray ());

4

List<user> listuser= (list<user>) (Object) dbutils.list ("from User");

5

Object object= dbutils.list ("from User");
List<user> listcart= (list<user>) object;

Note: The strong turn of the basic type and the reference type are different, but the distinction is very good, here is a small example:

1

int i= 1;

Long l= 10;

Feasible

I= (i) L;

2

Integer i= new Integer (1);

Long l= New Long (10);

Not feasible

I= (Integer) L;

About the cast of the list generics

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.