1. 程式人生 > 其它 >Java學習筆記111——包裝類

Java學習筆記111——包裝類

包裝類

需求1:將100這個資料,計算出它的二進位制,八進位制,十六進位制

需求2:如何用程式碼求出int型別的資料範圍

為了對基本資料型別進行更多的操作,更方便的操作,Java就針對每一個基本資料型別 都提供了對應的類型別。我們叫做包裝類型別。

包裝類型別: byte Byte short Short int Integer long Long float Float double Double char Character boolean Boolean

包裝類常見的使用場景: 1、集合中的泛型 2、用於基本資料型別與字串之間做轉換使用

public class PackingDemo1 {
  public static void main(String[] args) {
    //public static String toBinaryString(int i) 求int型別資料的二進位制
    String s = Integer.toBinaryString(100);
    System.out.println("100的二進位制為:" + s);
​
    //public static String toOctalString(int i) 求出int型別資料的八進位制
    String s1 = Integer.toOctalString(100);
    System.out.println("100的八進位制為:" + s1);
​
    //public static String toHexString(int i) 求出int型別資料的十六進位制
    String s2 = Integer.toHexString(100);
    System.out.println("100的十六進位制為:" + s2);
​
    //求出int型別資料的最小值
    //public static final int MIN_VALUE
    System.out.println("int型別資料的最小值為:" + Integer.MIN_VALUE);
​
​
    //求出int型別資料的最大值
    //public static final int MAX_VALUE
    System.out.println("int型別資料的最大值為:" + Integer.MAX_VALUE);
   }
}