e078. converting a primitive type value to a string.txt

来自「这里面包含了一百多个JAVA源文件」· 文本 代码 · 共 24 行

TXT
24
字号
There are two ways to convert a primitive type value into a string. The explicit way is to call String.valueOf(). The implicit way is to use the string concatenation operator `+'. 
    // Use String.valueOf()
    String s = String.valueOf(true);     // true
    s = String.valueOf((byte)0x12);      // 18
    s = String.valueOf((byte)0xFF);      // -1
    s = String.valueOf('a');             // a
    s = String.valueOf((short)123);      // 123
    s = String.valueOf(123);             // 123
    s = String.valueOf(123L);            // 123
    s = String.valueOf(1.23F);           // 1.23
    s = String.valueOf(1.23D);           // 1.23
    
    // Use +
    s = ""+true;                         // true
    s = ""+((byte)0x12);                 // 18
    s = ""+((byte)0xFF);                 // -1
    s = ""+'a';                          // a
    s = ""+((short)123);                 // 123
    s = ""+123;                          // 123
    s = ""+123L;                         // 123
    s = ""+1.23F;                        // 1.23
    s = ""+1.23D;                        // 1.23

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?