My Java Learning Note (14) About reflection (Part 3)

Source: Internet
Author: User

The array class in the 1.java.lang.reflect package allows you to create arrays dynamically.

2. In the call to the static method in the array class newinstance is required to provide two parameters, one is the element type of the array, and the other is the length of the array.

The Getcomponenttype method of the 3.Class class can determine the corresponding type of the array.

4. Integer array type int[] can be converted to object, but cannot be converted to an array of objects.


Related instance Code

Import Java.lang.reflect.*;import java.util.*;p ublic class test{public static void Main (string[] args) {int[] a = {}; A = (int[]) goodcopyof (a,10); System.out.println (Arrays.tostring (a)); String[] B = {"One", "one", "one", "three"};b = (string[]) goodcopyof (b,10); System.out.println (arrays.tostring (b)); System.out.println ("This method will produce an exception:"); b = (string[]) badcopyof (b,10);} public static object[] badcopyof (object[] a,int newlength) {object[] NewArray = new Object[newlength]; System.arraycopy (A, 0, NewArray, 0, Math.min (a.length, newlength)); return NewArray;} public static object Goodcopyof (Object A,int newlength) {Class C1 = A.getclass (); if (!c1.isarray ()) return null; Class ComponentType = C1.getcomponenttype (); int length = Array.getlength (a); Object NewArray = Array.newinstance ( Componenttype,newlength); System.arraycopy (a,0,newarray,0,math.min (length, newlength)); return newarray;}}

Output Results



The Invoke method in the 5.Method class can invoke the method wrapped in the current method object.

6. If the return value is a base type, the Invoke method returns its wrapper type.


Related instance Code

Import java.lang.reflect.*;p ublic class Test {public static void main (string[] args) throws Exception{method squ = test.cl Ass.getmethod ("Square", double.class); Method Sqr = Math.class.getMethod ("sqrt", Double.class);p rinttable (1,10,10,squ);p rinttable (1,10,10,SQR);} public static double square (double x) {return x*x;} public static void PrintTable (double begin,double end,int n,method f) {Double dx = (end-begin)/(n-1); for (double x = Beg In X <= end;x + = dx) {try{double y = (double) f.invoke (null, x); System.out.printf ("%10.4f | %10.4f%n ", x, y);} catch (Exception e) {e.printstacktrace ();}}}}

Output Results



My Java Learning Note (14) About reflection (Part 3)

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.