转发:

请先了解这个程序:

class ClassNameTest {public static void main(final String... arguments) {printNamesForClass(int.class,"int.class (primitive)");printNamesForClass(String.class,"String.class (ordinary class)");printNamesForClass(java.util.HashMap.SimpleEntry.class,"java.util.HashMap.SimpleEntry.class (nested class)");printNamesForClass(new java.io.Serializable(){}.getClass(),"new java.io.Serializable(){}.getClass() (anonymous inner class)");}private static void printNamesForClass(final Class<?> clazz, final String label) {System.out.println(label + ":");System.out.println("    getName():          " + clazz.getName());System.out.println("    getCanonicalName(): " + clazz.getCanonicalName());System.out.println("    getSimpleName():    " + clazz.getSimpleName());System.out.println("    getTypeName():      " + clazz.getTypeName()); // added in Java 8System.out.println();}
}

Output:

int.class (primitive):getName():          intgetCanonicalName(): intgetSimpleName():    intgetTypeName():      intString.class (ordinary class):getName():          java.lang.StringgetCanonicalName(): java.lang.StringgetSimpleName():    StringgetTypeName():      java.lang.Stringjava.util.HashMap.SimpleEntry.class (nested class):getName():          java.util.AbstractMap$SimpleEntrygetCanonicalName(): java.util.AbstractMap.SimpleEntrygetSimpleName():    SimpleEntrygetTypeName():      java.util.AbstractMap$SimpleEntrynew java.io.Serializable(){}.getClass() (anonymous inner class):getName():          ClassNameTest$1getCanonicalName(): nullgetSimpleName():    getTypeName():      ClassNameTest$1

There's an empty entry in the last block where getSimpleName returns an empty string.

The upshot looking at this is:

  • the name is the name that you'd use to dynamically load the class with, for example, a call to Class.forName with the default ClassLoader. Within the scope of a certain ClassLoader, all classes have unique names.
  • the canonical name is the name that would be used in an import statement. It might be useful during toString or logging operations. When the javac compiler has complete view of a classpath, it enforces uniqueness of canonical names within it by clashing fully qualified class and package names at compile time. However JVMs must accept such name clashes, and thus canonical names do not uniquely identify classes within a ClassLoader. (In hindsight, a better name for this getter would have been getJavaName; but this method dates from a time when the JVM was used solely to run Java programs.)
  • the simple name loosely identifies the class, again might be useful during toString or logging operations but is not guaranteed to be unique.
  • the type name returns "an informative string for the name of this type", "It's like toString: it's purely informative and has no contract value". (as written by sir4ur0n)

Also you can commonly reference the Java Language Specification documentation for these types technical Java API details:

  • Here's the Java 11 Specification on this subject matter: Chapter 6. Names

Example 6.7-2. and Example 6.7-2. goes over Fully Qualified Names and Fully Qualified Names v. Canonical Name respectively

Java 中canonical name, simple name and class name 的区别相关推荐

  1. Java中的string定义的两种方法和区别

    java中的String定义的两种方法和区别 第一种:new方式 String s1 = new String("hello world"); String s2 = new St ...

  2. java getabsolutepath,详谈java中File类getPath()、getAbsolutePath()、getCanonical的区别

    简单看一下描述,例子最重要. 1.getPath(): 返回定义时的路径,(就是你写什么路径,他就返回什么路径) 返回绝对路径,但不会处理"."和".."的情况 ...

  3. java中的path类_详谈java中File类getPath()、getAbsolutePath()、getCanonical的区别

    简单看一下描述,例子最重要. 1.getPath(): 返回定义时的路径,(就是你写什么路径,他就返回什么路径) 2.getAbsolutePath(): 返回绝对路径,但不会处理".&qu ...

  4. java中static、final 和 static final之间的区别

    java中static.final 和 static final之间的区别 static可以修饰:全局属性,方法,代码块,内部类(静态内部类或嵌套内部类) 由于静态属性和方法是属于该类的所有对象的,所 ...

  5. java 里面write,java 中 System.out.println()和System.out.write()的区别

    java 中 System.out.println()和System.out.write()的区别. 这两个函数一个是System.out.write()输出字符流,System.out.printl ...

  6. Java:中的String,StringBuilder,StringBuffer三者的区别

    Java中的String,StringBuilder,StringBuffer三者的区别 最近在学习Java的时候,遇到了这样一个问题,就是String,StringBuilder以及StringBu ...

  7. 2.Java中String,StringBuilder以及StringBuffer的关系与区别

    String     StringBuffer     StringBuilder String的值是不可变的,这就导致每次对String的操作都会生成新的String对象,不仅效率低下,而且浪费大量 ...

  8. Java中的String,StringBuilder,StringBuffer三者的区别

    最近在学习Java的时候,遇到了这样一个问题,就是String,StringBuilder以及StringBuffer这三个类之间有什么区别呢,自己从网上搜索了一些资料,有所了解了之后在这里整理一下, ...

  9. java中synchronized修饰静态方法和非静态方法有什么区别?

    Synchronized修饰非静态方法 Synchronized修饰非静态方法,实际上是对调用该方法的对象加锁,俗称"对象锁". Java中每个对象都有一个锁,并且是唯一的.假设分 ...

最新文章

  1. 只因接了一个电话,程序员被骗 30 万!
  2. vs2012 error c4996: 'fopen': This function or variable may be unsafe
  3. Pytorch学习-tensorboard的使用
  4. LOL快要凉?腾讯电竞应如何破除游戏生命周期诅咒
  5. linux安装x11鼠标主题
  6. CRM WebClient UI Relationship drop down list render logic
  7. Silverlight 5 Features
  8. java如何实现游戏暂停和恢复_Android:游戏循环暂停/恢复问题
  9. 三种方式解决vue中v-html元素中标签样式
  10. 服务器网卡,10GE设备相关笔记
  11. QGraphicsItem的使用
  12. 1、spss中做相关分析
  13. 使用WPF与C#实现鼠标画线
  14. 第一行代码第三版笔记
  15. 一文让你吃透!图解 pandas 透视表、交叉表!
  16. 计算机电池的性能参数,【戴尔灵越3567笔记本电脑使用总结】体积|电池|性能_摘要频道_什么值得买...
  17. 一方包、二方包和三方包的概念
  18. dnf mysql_CentOS7使用dnf安装mysql的方法
  19. python使用for循环输出0~10之间的整数_用Python编写一个程序,使用for循环输出0~10之间的整数...
  20. python实现一个简单的项目建议书范文_建议收藏,22个Python迷你项目(附源码)

热门文章

  1. 数商云供应链集采管理系统解决方案:集采系统管理模式,数字化管控企业物资
  2. Typecho上传附件失败最佳解决方法
  3. 【算力理解】MIPS/DMIPS/MFLOPS 理解
  4. apk里的html文件怎么打开,apk文件怎么打开
  5. 安徽教师计算机信息技术,安徽省中小学教师信息技术教育现状研究
  6. 安卓zip解压软件_[Android]压缩解压工具ZArchiver Pro高级直装版
  7. 《图解深度学习》学习笔记(六)
  8. 流量分析软件实时分析php mysql_石头科技 - 舆情分析小助手(php+mysql) v20200513
  9. Stata学习笔记|外部命令安装
  10. Photoshop 入门教程 如何更改图像分辨率?