Do you really know Java class name?

In the object-oriented world, class is the foundation of Java. java. Lang. class actually inherits from Java lang.Object。

Class has a method called getname, which returns the class name of (class, interface, array, class, primitive type, or void).

If you often debug the JVM, you will see the following strange content:

jcmd 1234 GC.class_histogram

These strange contents are class name. Let's see what they mean.

Class name is actually divided into three categories.

Primitive type or void

If the class object is of primitive type or void, their class name is the corresponding keyword or void.

        //primary class
        log.info(int.class.getName());
        log.info(short.class.getName());
        log.info(float.class.getName());
        log.info(double.class.getName());
        log.info(long.class.getName());
        log.info(byte.class.getName());
        log.info(char.class.getName());
        log.info(boolean.class.getName());
        //void
        log.info(void.class.getName());

Output results:

[main] INFO com.flydean.classname.ClassNameUsage - int
[main] INFO com.flydean.classname.ClassNameUsage - short
[main] INFO com.flydean.classname.ClassNameUsage - float
[main] INFO com.flydean.classname.ClassNameUsage - double
[main] INFO com.flydean.classname.ClassNameUsage - long
[main] INFO com.flydean.classname.ClassNameUsage - byte
[main] INFO com.flydean.classname.ClassNameUsage - char
[main] INFO com.flydean.classname.ClassNameUsage - boolean
[main] INFO com.flydean.classname.ClassNameUsage - void

reference type

If it is a reference type, the class name of the class will be returned:

//object class
        log.info(Object.class.getName());

Output results:

[main] INFO com.flydean.classname.ClassNameUsage - java.lang.Object

Array type

The array type is a little complex. According to the hierarchical relationship of array, [, will be added before class name, and as many [, as many as there are levels of array.

At the same time, the corresponding type will be converted to the corresponding code:

Let's take an example:

//Array
        log.info(int[].class.getName());
        log.info(short[].class.getName());
        log.info(float[].class.getName());
        log.info(double[].class.getName());
        log.info(long[].class.getName());
        log.info(byte[].class.getName());
        log.info(char[].class.getName());
        log.info(boolean[].class.getName());
        log.info(Object[].class.getName());

        //multiple arrays
        log.info(int[][][].class.getName());

Output results:

[main] INFO com.flydean.classname.ClassNameUsage - [I
[main] INFO com.flydean.classname.ClassNameUsage - [S
[main] INFO com.flydean.classname.ClassNameUsage - [F
[main] INFO com.flydean.classname.ClassNameUsage - [D
[main] INFO com.flydean.classname.ClassNameUsage - [J
[main] INFO com.flydean.classname.ClassNameUsage - [B
[main] INFO com.flydean.classname.ClassNameUsage - [C
[main] INFO com.flydean.classname.ClassNameUsage - [Z
[main] INFO com.flydean.classname.ClassNameUsage - [Ljava.lang.Object;
[main] INFO com.flydean.classname.ClassNameUsage - [[[I
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>