This article is a mirror article of machine translation, please click here to jump to the original article.

View: 13709|Reply: 0

[Source] 3 ways to get class names in Java!

[Copy link]
Posted on 4/2/2019 5:24:12 PM | | |
method to get the class name
There are three main ways to obtain class names in Java:

getName()
The return is the class name representation of the class in the virtual machine.

getCanonicalName()
What is returned is a more understandable class name representation.

getSimpleName()
Returned is the abbreviation of the class.

What is the difference?
Let's take an example to see the main differences.

The program outputs the following results.

Method Name Class Name
getName            com.test.TestClass
getCanonicalName   com.test.TestClass
getSimpleName      TestClass

getName            com.test.TestClass$TestInnerClass
getCanonicalName   com.test.TestClass.TestInnerClass
getSimpleName      TestInnerClass

getName            [Lcom.test.TestClass$TestInnerClass;
getCanonicalName   com.test.TestClass.TestInnerClass[]
getSimpleName      TestInnerClass[]
[Lcom.test.TestClass$TestInnerClass; It is worth explaining.

This is an encoding of the return values and parameters of a function called JNI Field Descriptors (JavaNative Interface Field Descriptors).

[ represents an array, one represents a one-dimensional array, such as [[ represents a two-dimensional array. After L stands for class descriptor, finally; Indicates the end of the class name.

conclusion

1. From the above results, it can be seen that getName() and getCanonicalName() are not different when obtaining ordinary class names, but there is a difference between obtaining internal classes and array classes.

2. getSimpleName() is no different when obtaining ordinary class and internal class names, but there is a difference when obtaining array classes.





Previous:In Spring boot, the JPA persistent listener intercepts additions, deletions, and modifications
Next:java gets the Object property type, property name, and property value
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com