JVM Tutorials - Herong's Tutorial Examples - v5.13, by Herong Yang
Array Class Introspection
This section provides a tutorial example on how to reflect arrays as classes and introspect its properties like superclass, element type, etc.
Arrays are represented as classes in JVM:
Here is tutorial example program showing you some array classes:
/* ClassArrayReflection.java * Copyright (c) HerongYang.com. All Rights Reserved. */ import java.lang.reflect.*; class ClassArrayReflection { static java.io.PrintStream out = System.out; public static void main(String[] a) { out.println(""); out.println(" byte[] class information..."); byte[] b = new byte[10]; arrayIntrospection(b.getClass()); out.println(""); out.println(" int[] class information..."); int[] i = new int[100]; arrayIntrospection(i.getClass()); out.println(""); out.println(" byte[][] class information..."); byte[][] bb = new byte[10][100]; arrayIntrospection(bb.getClass()); } public static void arrayIntrospection(Class cls) { out.println("Class name:"); out.println(" "+cls.getName()); Class p = cls.getSuperclass(); out.println("Parent class:"); out.println(" "+p); Class t = cls.getComponentType(); out.println("Element type:"); out.println(" "+t.getName()); Constructor[] c = cls.getDeclaredConstructors(); out.println("Constructors:"); for (int i = 0; i<c.length; i++) out.println(" "+c[i].toString()); Field[] f = cls.getDeclaredFields(); out.println("Fields:"); for (int i = 0; i<f.length; i++) out.println(" "+f[i].toString()); Method[] m = cls.getDeclaredMethods(); out.println("Methods:"); for (int i = 0; i<m.length; i++) out.println(" "+m[i].toString()); } }
When executed, I got this result:
herong>java ClassArrayReflection byte[] class information... Class name: [B Parent class: class java.lang.Object Element type: byte Constructors: Fields: Methods: int[] class information... Class name: [I Parent class: class java.lang.Object Element type: int Constructors: Fields: Methods: byte[][] class information... Class name: [[B Parent class: class java.lang.Object Element type: [B Constructors: Fields: Methods:
The result tells me that:
Table of Contents
JVM (Java Virtual Machine) Specification
Java HotSpot VM - JVM by Oracle/Sun
java.lang.Runtime Class - The JVM Instance
java.lang.System Class - The Operating System
ClassLoader Class - Class Loaders
►Class Class - Class Reflections
forName() Method - Loading Classes
Class Reflection and Introspection
Invoking Methods of Class Instances
JVM Stack, Frame and Stack Overflow
Thread Testing Program and Result
CPU Impact of Multi-Thread Applications
I/O Impact of Multi-Thread Applications
Micro Benchmark Runner and JVM Options
Micro Benchmark Tests on "int" Operations
Micro Benchmark Tests on "long" Operations
Micro Benchmark Tests in JIT Compilation Mode
Micro Benchmark Tests on "float" and "double" Operations