Java: implicit conversion of object at compile time does not work, but reflection can be used

The following codes are given:

import java.lang.reflect.Array;
import java.lang.reflect.Method;

public class Test {

  public static void test_function(int[] values) {
    System.out.println("Class of values : " + values.getClass());
    System.out.println(values[0]);
  }

  public static void main(String[] args) {

    Object a = Array.newInstance(int.class,3);
    Array.set(a,5);

    Class a_class         = a.getClass();
    Class int_array_class = int[].class;

    System.out.println("Class of int[]  : " + int[].class); 
    System.out.println("Class of a      : " + a.getClass());
    System.out.println("int_array_class : " + int_array_class);
    System.out.println("a_class         : " + a_class);

    // These instructions will provoke a compile-time error
    // test_function(a);
    // test_function(a_class.cast(a));
    // test_function(int_array_class.cast(a));

    // The following call won't cause any problem
    test_function((int[]) a);

    Method testf = null;
    try { 
      testf = Test.class.getmethod("test_function",int[].class);
      testf.invoke(null,a); // Does not provoke exception at run-time either
      testf.invoke(null,a_class.cast(a));
      testf.invoke(null,int_array_class.cast(a));
    }
    catch(Exception e) { 
      e.printStackTrace();
    }

  }

}

I've noticed that explicitly passing an object to a method doesn't work If you uncomment, the three comment descriptions raise the following error:

Test.java:25: error: method test_function in class Test cannot be applied to given types;
    test_function(a);
    ^
  required: int[]
  found: Object
  reason: actual argument Object cannot be converted to int[] by method invocation conversion
Test.java:26: error: method test_function in class Test cannot be applied to given types;
    test_function(a_class.cast(a));
    ^
  required: int[]
  found: Object
  reason: actual argument Object cannot be converted to int[] by method invocation conversion
Test.java:27: error: method test_function in class Test cannot be applied to given types;
    test_function(int_array_class.cast(a));
    ^
  required: int[]
  found: Object
  reason: actual argument Object cannot be converted to int[] by method invocation conversion
3 errors

However, passing objects through reflection works and produces the desired results

Class of int[]  : class [I
Class of a      : class [I
int_array_class : class [I
a_class         : class [I
Class of values : class [I
5
Class of values : class [I
5
Class of values : class [I
5
Class of values : class [I
5

So the question is: why does the compiler refuse to compile three command instructions that work normally at run time?

Solution

Reflective access can determine the actual type of object at run time On the other hand, the compiler only knows the declared type It has no other possibility of checking actual type parameters

Your variable a is declared as object therefore

test_function(a);

You tried to use the signature void test_ Function (object) calls a method that does not exist at all A simple actor – just as you know the right type as a programmer – will help here:

test_function((int[]) a); // works fine (even at runtime)

The other two method calls are the same, but somewhat different You declared a with type class_ Class and int_ array_ class. This is a primitive type, so the compiler does not know which type these variables can reference The compiler must infer the type object This leads to the same problems (and solutions) as before

However, class is a generic type and can be parameterized The last method call does not require an explicit conversion with the following changes:

Class<int[]> int_array_class = int[].class;
test_function(int_array_class.cast(a));
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
分享
二维码
< <上一篇
下一篇>>