java基礎專欄—CommonApi

Java經常使用Api

將基本類型也轉換成了對象以對其功能擴展java

javaWeb接受到的數據都是以String類型的,怎麼把String轉換爲int數組

|字節 | 整型 | 字符 | 布爾|dom

| ---- | ------- | ------ | ------- |ide

| Byte | Integer | String | Boolean |函數

Integer應用舉例

數字格式的字符串工具

  • *Integer.parseInt("12") NumberFormatException操作系統

  • 任何類型轉換爲String ,只須要鏈接空串" "code

  • 使用Integer中的靜態方法toString()orm

    • 只有引用類型纔有toString(),基礎類型是沒有toString()

利用Integer的構造方法將String轉換爲int類型對象

Integer in = new Integer("100");
int in = in.valueOf();
  • Integer的靜態成員變量
    • MAX_VALUE
    • MIN_VALUE
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE)
  • Integer的靜態方法
    • 十進制轉二進制 toBinarString(int value)
    • 十進制轉八進制 toOctalString(int value)
    • 十進制轉十六進制 toHexString(int value)
    • 轉換完之後是String轉換的

自動裝箱,自動拆箱

  • 自動裝箱:將基本數據類型直接轉換爲對象
  • 自動拆箱:將對象數據轉換爲基本數據類型
Integer i = 1;
//不用new Integer()
System.out.println(i)
//打印應用類型會調用toString(),說明咱們重寫過toString()
  
ArrayList<int> ar = new ArrayList<int>
//會報錯,由於int是基本數據類型不是對象
ArrayList<Integer> ar = new ArrayList<Integer>
ar.add(1);
//1是基本數據,可是自動轉換爲對象了

Integer in = null;
in = in + 1;
//弊端,在爲空的時候是不會自動裝箱的
//在運算的時候就會NullPointException
  • 基本類型能夠直接和引用類型作運算
  • 簡化代碼
Integer i = 127;
Integer j = 127;
System.out.println(i==j);//Ture
//數據在自動裝箱的時候數據沒有大於1字節,因此 Integer j = i;
System.out.println(i.equals(j));//Ture

System應用

私有修飾構造函數 調用方法只能使用類名調用

  • public static void currentTimeMillis()

    • 用來計算程序運行的時間
  • public static void exit(int status)

    • 終止正在運行的虛擬機,0正常終止,非0異常終止
  • public static void gc()

    • 垃圾回收,虛擬機回收垃圾的時候回調用finalize()
  • public static Properties getPropertise()

    • 返回當前操做系統的信息
  • public static void arraycopy(Object src ,int srcPos ,Object dest , int destPos, int length)

    • 複製一個數組

    • src:源數組

      srcPos:源數組的開始的索引

      dest:複製後目標數組

      destPos:目標數組起始索引

      length:複製幾個

    • int[] src = {11,22,33,44,55,66};
      int[] dest = {77,88,99,0};
      
      System.arraycopy(src, 1, dest, 1, 2)

Math類應用舉例

靜態方法工具類

  • public static int abs(int i)
    • 返回絕對值
  • public static int max(int i, int j)
  • public static int min(int i,int j)
  • public static double ceil(double d)
    • 返回大於或者等於參數d的最小整數
  • public static double floor(double d)
    • 返回小於或者等於參數d的最大整數
  • public static double pow(double a, double b)
  • public static double sqrt(double a)
  • public static double random()
    • 返回一個隨機數(僞隨機數)0.0~1,0之間
      • 也是使用random類來實現的,推薦直接使用random
  • public static double round(double d)
    • 四捨五入取整

Array工具類

java.util 繼承至java.lang.Object

  • public static void sort(array)

    Arrays.sort(數組)
  • public static int binarySearch(array, element)

    • 返回數組的二分查找,只能搜索有序的數組,元素存在返回- 插入點索引 - 1
  • public static String toString(array)

    • 將數組轉換爲String

BigInteger

超過lang的範圍的就是BigInteger對象

  • public BigInteger(String str)
  • BigInteger的四則運算,都是調用方法來實現的不是經過四則運算符 來完成
    • public static BigInteger add()
    • public static BigInteger subtract()
    • public static BigInteger mutiply()
    • public static BigInteger divied()

BigDecimal

BigDecimal

  • public BiDecimal(String string)
    • add, subtact, multiply
    • divide(BigDecimal dicisor, int scale ,int rindingMode)
      • 無限不循環小數不能顯示,arithmeticException Non-terminating decimal exception
    • int scale保留幾位小數
相關文章
相關標籤/搜索