Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Class.forName return array type? [duplicate]

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?

like image 586
imgen Avatar asked Mar 16 '13 14:03

imgen


People also ask

What is return type of class forName ()?

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.

How do you make an array from a class?

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.

Do we have a array class in Java?

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.


1 Answers

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[]

like image 164
BlackJoker Avatar answered Sep 21 '22 01:09

BlackJoker