基本包装类和System类
生活随笔
收集整理的這篇文章主要介紹了
基本包装类和System类
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
基本包裝類
程序界面上用戶輸入的數據都是以字符串類型進行存儲的,要把字符串轉成基本數據類型操作
基本包裝類就是對8種基本數據類型進行包裝:Integer Character 其他的都是首字母大寫
1、將字符串轉換成基本數據類型
Integer.parseInt(字符串) ? Double.parseDouble(字符串) ? 其他類型也一樣
//parseXXX(String str)//傳入的字符串必須是對應的基本數據類型String str = "12";int i = Integer.parseInt(str);System.out.println(--i);String str2 = "2.5";double d = Double.parseDouble(str2);System.out.println(d);2、將基本數據類型轉換成字符串
用+號拼接一個空字符串""
用String類中的valueof()方法
用包裝類中的toString(參數)方法
public static void main(String[] args) {//基本數據類型+"" 加號的拼接int i = 9;String str = i+"";System.out.println(str+1);//調用String類的valueof()方法String str1 = String.valueOf(1);System.out.println(str1+9);//調用包裝類中的toString(參數)方法String str2 = Integer.toString(5);System.out.println(str2+5);}3、基本數據類型和包裝類的轉換
jdk1.5以前的方法
基本數據類型--包裝類
構造方法
valueof()方法
Integer in = new Integer(3);Integer in2 = new Integer("3");Integer in3 = Integer.valueOf(5);Integer in4 = Integer.valueOf("5");包裝類--基本數據類型
調用intValue()方法
int i = in.intValue();jdk1.5以后
自動裝箱和拆箱
自動裝箱:基本數據類型自動直接轉成對應的包裝類對象
自動拆箱:包裝類對象自動直接轉成對應的基本數據類型
public static void method1(){//自動裝箱Integer in = 5;//相當于Integer in = new Integer(3);//自動拆箱//int sum = in + 6;System.out.println(in+6);//System.out.println(sum); }在自動拆裝箱中,遇到byte類型的數值以內,先創建一個對象,后來的對象都指向第一個對象的地址
public static void method22(){//在自動拆裝箱中,如果是byte(128)數值以內,先創建一個對象,后來的對象都指向第一個對象的地址Integer in = 20;Integer in2 = 20;System.out.println(in==in2);//trueSystem.out.println(in.equals(in2));//true}?
轉載于:https://www.cnblogs.com/yelena-niu/p/9092490.html
總結
以上是生活随笔為你收集整理的基本包装类和System类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 吐槽net下没有靠谱的FastDFS的s
- 下一篇: python-configparser模