Skip to content

Instantly share code, notes, and snippets.

@bingoohuang
Last active December 19, 2015 07:09
Show Gist options
  • Save bingoohuang/5916670 to your computer and use it in GitHub Desktop.
Save bingoohuang/5916670 to your computer and use it in GitHub Desktop.
In Java: What is the difference between: Object o1= .... o1.getClass().getSimpleName(); o1.getClass().getName(); o1.getClass().getCanonicalName();
/**
In Java: What is the difference between:
Object o1= ....
o1.getClass().getSimpleName();
o1.getClass().getName();
o1.getClass().getCanonicalName();
**/
@Test
public void testNames() throws Exception {
//primative
System.out.println(int.class.getName()); // int
System.out.println(int.class.getCanonicalName()); // int
System.out.println(int.class.getSimpleName()); // int
System.out.println();
//class
System.out.println(String.class.getName()); // java.lang.String
System.out.println(String.class.getCanonicalName()); // java.lang.String
System.out.println(String.class.getSimpleName()); // String
System.out.println();
//inner class
System.out.println(HashMap.SimpleEntry.class.getName()); // java.util.AbstractMap$SimpleEntry
System.out.println(HashMap.SimpleEntry.class.getCanonicalName()); // java.util.AbstractMap.SimpleEntry
System.out.println(HashMap.SimpleEntry.class.getSimpleName()); // SimpleEntry
System.out.println();
//anonymous inner class
System.out.println(new Serializable(){}.getClass().getName()); // com.ailk.ecs.esf.base.cache.ConcurrCacheITest$1
System.out.println(new Serializable(){}.getClass().getCanonicalName()); // null
System.out.println(new Serializable(){}.getClass().getSimpleName()); // (blank string)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment