• java获取类型名字的不同方法


    java的Class<?>类型提供了多种方法获取类型的名字。这些方法的返回值区别如下:

    测试代码:

     1 import java.math.BigDecimal;
     2 
     3 public class TestClassName {
     4     public static void main(
     5             String[] args
     6     ) {
     7         char v1 = 'C';
     8         byte v2 = 2;
     9         short v3 = 3;
    10         int v4 = 4;
    11         long v5 = 5;
    12         float v6 = 6;
    13         double v7 = 7;
    14         BigDecimal v8 = BigDecimal.valueOf(8);
    15         String v9 = "9";
    16         TestBean v10 = new TestBean();
    17 
    18         char[] a1 = {'A'};
    19         byte[] a2 = {2};
    20         short[] a3 = {3};
    21         int[] a4 = {4};
    22         long[] a5 = {5};
    23         float[] a6 = {6};
    24         double[] a7 = {7};
    25         BigDecimal[] a8 = {BigDecimal.valueOf(8)};
    26         String[] a9 = {"9", "10"};
    27         TestBean[] a10 = {new TestBean(), new TestBean()};
    28 
    29         dumpAllNames("char", char.class);
    30         dumpAllNames("byte", byte.class);
    31         dumpAllNames("short", short.class);
    32         dumpAllNames("int", int.class);
    33         dumpAllNames("long", long.class);
    34         dumpAllNames("float", float.class);
    35         dumpAllNames("double", double.class);
    36         dumpAllNames("BigDecimal", BigDecimal.class);
    37         dumpAllNames("String", String.class);
    38         dumpAllNames("TestBean", TestBean.class);
    39 
    40         dumpAllNames("char[]", char[].class);
    41         dumpAllNames("byte[]", byte[].class);
    42         dumpAllNames("short[]", short[].class);
    43         dumpAllNames("int[]", int[].class);
    44         dumpAllNames("long[]", long[].class);
    45         dumpAllNames("float[]", float[].class);
    46         dumpAllNames("double[]", double[].class);
    47         dumpAllNames("BigDecimal[]", BigDecimal[].class);
    48         dumpAllNames("String[]", String[].class);
    49         dumpAllNames("TestBean[]", TestBean[].class);
    50     }
    51 
    52     private static void dumpAllNames(
    53             String name,
    54             Class<?> clazz
    55     ) {
    56         System.out.println(name + ".name          = " + clazz.getName());
    57         System.out.println(name + ".simpleName    = " + clazz.getSimpleName());
    58         System.out.println(name + ".canonicalName = " + clazz.getCanonicalName());
    59         System.out.println(name + ".typeName      = " + clazz.getTypeName());
    60     }
    61 
    62     private static class TestBean {
    63     }
    64 }

    运行结果:

    char.name          = char
    char.simpleName    = char
    char.canonicalName = char
    char.typeName      = char
    byte.name          = byte
    byte.simpleName    = byte
    byte.canonicalName = byte
    byte.typeName      = byte
    short.name          = short
    short.simpleName    = short
    short.canonicalName = short
    short.typeName      = short
    int.name          = int
    int.simpleName    = int
    int.canonicalName = int
    int.typeName      = int
    long.name          = long
    long.simpleName    = long
    long.canonicalName = long
    long.typeName      = long
    float.name          = float
    float.simpleName    = float
    float.canonicalName = float
    float.typeName      = float
    double.name          = double
    double.simpleName    = double
    double.canonicalName = double
    double.typeName      = double
    BigDecimal.name          = java.math.BigDecimal
    BigDecimal.simpleName    = BigDecimal
    BigDecimal.canonicalName = java.math.BigDecimal
    BigDecimal.typeName      = java.math.BigDecimal
    String.name          = java.lang.String
    String.simpleName    = String
    String.canonicalName = java.lang.String
    String.typeName      = java.lang.String
    TestBean.name          = TestClassName$TestBean
    TestBean.simpleName    = TestBean
    TestBean.canonicalName = TestClassName.TestBean
    TestBean.typeName      = TestClassName$TestBean
    char[].name          = [C
    char[].simpleName    = char[]
    char[].canonicalName = char[]
    char[].typeName      = char[]
    byte[].name          = [B
    byte[].simpleName    = byte[]
    byte[].canonicalName = byte[]
    byte[].typeName      = byte[]
    short[].name          = [S
    short[].simpleName    = short[]
    short[].canonicalName = short[]
    short[].typeName      = short[]
    int[].name          = [I
    int[].simpleName    = int[]
    int[].canonicalName = int[]
    int[].typeName      = int[]
    long[].name          = [J
    long[].simpleName    = long[]
    long[].canonicalName = long[]
    long[].typeName      = long[]
    float[].name          = [F
    float[].simpleName    = float[]
    float[].canonicalName = float[]
    float[].typeName      = float[]
    double[].name          = [D
    double[].simpleName    = double[]
    double[].canonicalName = double[]
    double[].typeName      = double[]
    BigDecimal[].name          = [Ljava.math.BigDecimal;
    BigDecimal[].simpleName    = BigDecimal[]
    BigDecimal[].canonicalName = java.math.BigDecimal[]
    BigDecimal[].typeName      = java.math.BigDecimal[]
    String[].name          = [Ljava.lang.String;
    String[].simpleName    = String[]
    String[].canonicalName = java.lang.String[]
    String[].typeName      = java.lang.String[]
    TestBean[].name          = [LTestClassName$TestBean;
    TestBean[].simpleName    = TestBean[]
    TestBean[].canonicalName = TestClassName.TestBean[]
    TestBean[].typeName      = TestClassName$TestBean[]
  • 相关阅读:
    MacOS中Elasticsearch的安装「借助Homebrew」
    全文搜索Lucene之倒排索引
    MybatisCodeHelperNew-2.8.1-191-201插件使用
    支付宝支付-当面付之扫码支付「扫码支付」
    ConcurrentHashMap底层原理?
    支付宝手机h5网页支付不再提供「继续浏览器付款」按钮了吗
    小程序json字符串取值问题,怎么取出来的是undefined,eval函数不能用?
    @SpringBootApplication(exclude={DataSourceAutoConfiguration.class})注解作用
    CAS无锁机制
    数组追加数组,小程序数组里面追加数组如何操作?
  • 原文地址:https://www.cnblogs.com/mopno1/p/9353319.html
Copyright © 2020-2023  润新知