int Integer String 相互轉換

三種類型的相互轉換是非常重要的知識點
接下來直接展示代碼:

package cn.geezer.usually;

public class IntToIntegerToString {
    public static void main(String[] args) {
        //int -> String
        int i = 10;
        String str = i + "";//直接使用字符串拼接的技巧,將int轉換爲String
        str = String.valueOf(i);//使用String的valueOf函數

        //int -> Integer
        Integer I = new Integer(i);//實例化對象時,將int型轉換爲Integer
        Integer I1 = Integer.valueOf(i);//使用方法
        Integer I2 = i;//自動裝箱

        //Integer -> int
        int i1 = I.intValue();//使用方法得到int值
        int i2 = I1;//自動拆箱

        //Integer -> String
        String str1 = I.toString();
        String str2 = String.valueOf(I);

        //String -> int
        int i3 = Integer.parseInt("10");
        int i4 = Integer.valueOf("10");

        //String -> Integer
        Integer I3 = Integer.parseInt("11");
        Integer I4 = new Integer("666");

    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章