JAVA-實用類

常用類

一)Scanner

1)JDK5以後出現,鍵盤錄入的類
2)構造方法
    A:System.in是什麼 InputStream 的子類
3)基本使用方法
    A:hashNextXxx()
    B: nextXxx()
4)注意的問題
    先調用nextXxx(),然後使用nextLine()
    nextLine()可以接受空格,字符串
    可以使用next() 替換

二)String類的概述和使用

1)多個字符組成的一串數據。

2)構造方法:
    A:public String()
    B:public String(String original)
    C:public String(byte[] bytes)
    D:public String(char[] value)

3)字符串的特點(==和equals的區別)
    A:字符串一旦被賦值,就不能改變。(注,不能改變指的是內容,不是引用)
    B:注意注意String str = new String("hello");和String str="hello";

4)字符串的功能
    A:判斷功能:equals,equalsIgnoreCalse,contains,startsWith,endsWith,isEmpty
    B:獲取功能:length,charAt,indexOf,subString,
    C:轉換功能:getBytes(),toCharArray(),valueOf
    D:替換功能: replace(char old,char new)、replace(String old,String new)
    E:其他功能:String trim()去空格、compareTo比較

 5)面試題:

    字符串如果是變量相加,先開空間,在拼接
    String s1 = "hello";
    String s2 = "world";
    String s3 = "helloworld";
    System.out.println(s3 == s1 + s2);
    System.out.println(s3.equals((s1 + s2)));

    System.out.println(s3 == "hello" + "world");
    System.out.println(s3.equals("hello" + "world"));

三)StringBuffer

1)用字符串做拼接,耗時耗內存。所以java提供字符串緩衝區
2)A:StringBuffer()\(int size)\(String str)
3)StringBuffer的常見功能
4)StringBuffer<--->相互轉換
    A:
    String---->StringBuffer 構造方法,
    StringBuffer---->String  toString方法
    B:字符串的拼接
    C:判斷是否是迴文串
5)StringBuffer和StringBuilder的區別
    StringBuffer同步,數據安全,效率低

四)Integer

0)自動裝箱
    Integer i = 127; // Integer i = Integer.valueOf(127);
   自動拆箱
    Integer i = 1234; 
    Integer i1 = i + 1000;// Integer i1 = i.intValue() + 1000;
1)讓基本數據類型 可以當做一個對象使用,提供了包裝類型
    int--->Integer
    char--->Character
2)Integer的構造方法
    A:Integer i = new Integer(100);
    B:Integer i = new Integer("100");
3)String 和 int的相互轉換
    A:String--->int   Integer.parseInt("100");
    B:int--->String   String.valueOf(100);
4)拓展性問題:
    -128到127之間的數據緩衝池問題

五)Character – 拓展

1)Character構造方法

  Character ch = new Character('a');

2)方法

        Character.isDigit('1');
        Character.isLetter('a');
        Character.isLowerCase('a');
        Character.isUpperCase('A');
        Character.toLowerCase('A');
        Character.toUpperCase('b');

3)統計字符串大寫,小寫,數字個數


六)Math

1)
2)方法    
     最大值:static int max(int a, int b)  當然也有min
     a值的自然對數(底數是 e):static double log(double a) 
     a的b次冪  :static double pow(double a, double b) 
     絕對值 static double abs(double a)
     向上取整 static double ceil(double a) 
     向下取整 static double floor(double a) 
     四捨五入 static int round(float a) 
     平方根   public static double sqrt(double a)
     π    Math.PI

……
返回角的三角正弦。

隨機值 [0.0 ,1.0)的double值:static double random()
可以實現獲取任意範圍內的隨機數。


七)Random —擴展

1)由Math.random()方法的源碼 — 看隨機怎麼實現
2)看情況自己實現獲取整形隨機值
3)這個是僞隨機和隨機的區別
A:new Random();是否傳參
給定種子,獲取隨機數相同
使用默認的時間爲種子,隨機每次不同
B:new Random().nextInt() — 整形範圍值
new Random().nextInt(n) — [0,n)


八)BigInteger

浮點存儲方式和整形存儲方式不同,
浮點運算很少是精確的,超過精度表示範圍會產生誤差。由此,產生的結果接近但不等於想要的結果。在使用 float 和 double 作精確運算要注意。
可以使用: BigDecimal 或 使用 long 類型來轉換。

    BigDecimal add(BigDecimal augend)
    subtract(BigDecimal subtrahend)
    multiply(BigDecimal multiplicand)
    divide(BigDecimal divisor)
    divide(BigDecimal divisor,int scale,int roundingMode):商,小數位數,取捨方式
發佈了38 篇原創文章 · 獲贊 1 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章