About the strong conversion of object array into integer array: ljava lang.Object; cannot be cast to [Ljava.lang.Integer;

The code is as follows:

reason:

The root cause is the inheritance relationship of types. Integer [] is not a subclass of object []. Although integer inherits from object, the direct parent of integer [] is object. That is, the direct parent class of all array types is object, which can be verified by reflection. Array types are written in the JVM, just like eight basic types. We can't find this class in the Java standard library.

Output results:

Solution: each member of the array can be converted separately:

When a is inherited by B, the reference of type a [] can point to an instance of type B []

That is: a [] a = New B [3] holds.

Console:

This situation is allowed. It is a syntax feature of Java. This syntax feature exists for the convenience of parameter passing in the method.

When passing parameters to the database statement, multiple parameters are passed to each placeholder in the SQL statement, which can be received with parameters of object [] type. Then, the array elements are traversed in the method and converted into corresponding type data according to different types.

Without this feature:

1. Define parameters of object type in the method to receive array parameters. If type conversion is carried out inside the code, there is a risk of abnormal type conversion, because object is the top-level parent class, and the parameters passed can not be an array

2. Using a specific data type array, string [], integer [], will make the type restrictions too fixed and inflexible. Each type should be replaced by a new method.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>