Is it possible that Class.forName
can return an array type? Now when I use Class.forName("byte[]")
it throws NoClassFound
exception.
Or generally, how to get Type[].class
from Type.class
?
forName. Returns the Class object associated with the class or interface with the given string name, using the given class loader. Given the fully qualified name for a class or interface (in the same format returned by getName ) this method attempts to locate, load, and link the class or interface.
An Array of Objects is created using the Object class, and we know Object class is the root class of all Classes. We use the Class_Name followed by a square bracket [] then object reference name to create an Array of Objects.
Class Array. The Array class provides static methods to dynamically create and access Java arrays. Array permits widening conversions to occur during a get or set operation, but throws an IllegalArgumentException if a narrowing conversion would occur.
public static void main(String[] args) {
System.out.println(byte[].class.getName());
try {
Class clazz = Class.forName("[B");
System.out.println(byte[].class==clazz);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
byte[].class's name is "[B";
byte[].class==clazz is true
[Ljava.lang.String; for String[]
[Lpacket.to.YourClass; for YourClass[]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With