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) 2010, 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 on my Windows 7 system with JDK 1.7.0, I got this result:

C:\herong\jvm\cod>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:

When tested with program with JDK 1.6.0 in 2010, I got the same results.

Last update: 2014.

Table of Contents

 About This Book

 Downloading and Installing JDK 1.8.0 on Windows

 Downloading and Installing JDK 1.7.0 on Windows

 java.lang.Runtime Class - The JVM Instance

 java.lang.System Class - The Operating System

 ClassLoader Class - Class Loaders

Class Class - Class Reflections

 What Is java.lang.Class Class?

 forName() Method - Loading Classes

 Class Reflection and Introspection

Array Class Introspection

 Invoking Methods of Class Instances

 Sun's JVM - Java HotSpot VM

 JRockit JVM 28.2.7 by Oracle Corporation

 JVM Runtime Data Areas

 Memory Management and Garbage Collectors

 Garbage Collection Tests

 JVM Stack, Frame and Stack Overflow

 Thread Testing Program and Result

 CPU Impact of Multi-Thread Applications

 I/O Impact of Multi-Thread Applications

 CDS (Class Data Sharing)

 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

 Outdated Tutorials

 References

 PDF Printing Version