Class Name Forms For Diagnostics
Compare getName, getSimpleName and getCanonicalName across a plain class, a nested interface, an array and an anonymous class whose canonical name is null.
Code
import java.util.ArrayList;
import java.util.Map;
public class ClassNameForms {
public static void main(String[] args) {
Class<?> plain = ArrayList.class;
Class<?> nested = Map.Entry.class;
Class<?> array = String[].class;
Class<?> anon = new Runnable() {
@Override
public void run() {
}
}.getClass();
// getName 是 JVM 名(内部 $ 记法),simple 是短名,canonical 是源码形态
System.out.println("ArrayList: name=" + plain.getName()
+ " simple=" + plain.getSimpleName() + " canonical=" + plain.getCanonicalName());
System.out.println("Map.Entry: name=" + nested.getName()
+ " simple=" + nested.getSimpleName() + " canonical=" + nested.getCanonicalName());
System.out.println("String[]: name=" + array.getName()
+ " simple=" + array.getSimpleName() + " canonical=" + array.getCanonicalName());
System.out.println("anonymous: name=" + anon.getName()
+ " simple=" + anon.getSimpleName() + " canonical=" + anon.getCanonicalName());
}
}How it works
Class Name Forms For Diagnostics comes down to the class ClassNameForms and the functions main and run.
Caveats
- No dependencies section needed; the standard library is the whole story.
Using it
The primary signature is public static void main(String[] args)
Look for the ArrayList: name=, Map.Entry: name=, String[]: name= prefixes when the file runs; they tie each line to its step.
main sequences run; executing the file runs that flow once.
Key statements worth quoting
Class<?> nested = Map.Entry.class;
Class<?> plain = ArrayList.class;
Class<?> array = String[].class;
Interactive Tools for This Topic
CSS Color Name LookupTranslate between CSS keyword color names and hex values with RGB and HSL conversions, exact name matching, and nearest-color suggestions.Git Conventional Commit BuilderBuild and validate Conventional Commits v1.0.0 messages with type, scope, breaking markers, 72-char subject counter, body, footers, and git CLI command.Glob Pattern TesterCompile glob patterns with *, **, ?, character classes, and braces into regex and test them against a list of candidate file paths.Tar Archive Command BuilderBuild safe tar create, extract, and list commands for gzip, xz, zstd, and bzip2 archives with excludes, target directory (-C), and flag explanations.
FAQ
- What does Class Name Forms For Diagnostics print when run?
- Execute the whole file; each printed line is prefixed with a label like `ArrayList: name=`, `Map.Entry: name=`, `String[]: name=`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.