Java基礎常用類總結筆記

常用類

包裝類基本知識

基本數據類型 包裝類
byte Byte
boolean Boolean
short Short
char Character
int Integer
long Long
folat Folat
double Double

自動裝箱和拆箱

自動裝箱:

基本類型的數據處於需要對象的環境時,會自動轉換爲“對象”

在JDK1.5以前,這樣的代碼Integer i = 5 是錯誤的,必須要通過Integer i = new Integer(5)這樣的語句來實現基本數據類型轉換成包裝類的過程;而在JDK1.5以後,Java提供了自動裝箱的功能,因此只需要Integer i =5這樣的語句就能實現基本數據類型轉換成包裝類,這是因爲JVM爲我們執行了Integer i = Integer.valueOf(5)這樣的操作,這就是Java的自動裝箱

Integer i = 100;//自動裝箱
//相當於編譯器自動爲您執行以下的語法編譯:
Integer i = Integer.valueOf(100);//調用的是valueOf(100),而不是new Integer(100)

自動拆箱:

每當需要一個值時,對象會自動轉換成基本數據類型,沒必要再去顯示調用intValue()、doubleValue()等轉型方法

Integer i = 100;
int j = i;//自動拆箱
//相當於編譯器自動爲您執行以下的語法編譯
int j = i.intValue();

緩存;

  1. 緩存[-128,127]之間的數字。實際就是系統初始的時候,創建了[-128,127]之間的一個緩存數組
  2. 當我們調用valueOf()的時候,首先檢查是否在[-18,127]之間,如果在這個範圍則直接從緩存數組中拿出已經存在的對象
  3. 如果不在這個範圍,則創建新的Integer對象

String類

StringBuilder

 public static void main(String[] args) {
        //線程不安全,效率高(一般使用);StringBuffer線程安全,效率低
        StringBuilder sb = new StringBuilder("abcdefg");

        System.out.println(Integer.toHexString(sb.hashCode()));
        System.out.println(sb);
        System.out.println("-----------------------");
        sb.setCharAt(1,'B');
        System.out.println(Integer.toHexString(sb.hashCode()));
        System.out.println(sb);
    }
@Test
    public void test(){
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < 26; i++){
            sb.append((char)(65+i));
        }
        System.out.println(sb);
        System.out.println("-------------------");
        sb.reverse();//倒敘
        System.out.println(sb);
        sb.setCharAt(3,'Q');
        //鏈式調用 核心就是該方法返回return this
        sb.insert(4,'W').insert(5,'E');
        System.out.println("----------");
        //也可以鏈式調用
        sb.delete(24,25);
        System.out.println(sb);
    }

時間處理相關類

public static void main(String[] args) throws ParseException {
        Date date = new Date();
        System.out.println(date.getTime());
        System.out.println(System.currentTimeMillis());
        //把時間對象按照指定格式轉換成字符串
        SimpleDateFormat df = new SimpleDateFormat("yyyy年-MM月-dd日");
        System.out.println(df.format(date));

        System.out.println("-------------------");
        //把字符串按照“格式字符串指定的格式”轉成相應的時間對象
        SimpleDateFormat df2 = new SimpleDateFormat("yyyy年-MM月-dd日");
        Date d = df2.parse("2018年-12月-25日");
        System.out.println(d);
    }

Calendar日曆類

 public static void main(String[] args) {
        GregorianCalendar calendar = new GregorianCalendar(2018,9,21);
        System.out.println(calendar.get(Calendar.YEAR));
    }

File類的基本用法

 public static void main(String[] args) throws IOException {
        File file = new File("a.txt");
        //判斷文件是否存在
        System.out.println(file.exists());
        System.out.println(file);
        //改名操作
        file.renameTo(new File("b.txt"));
        //目錄 打印工程目錄
        System.out.println(System.getProperty("user.dir"));
        //創建文件
        File file1 = new File("gg.txt");
        file1.createNewFile();
        //創建目錄
        File file2 = new File("test");
        file2.mkdir();

        //使用遞歸遍歷目錄樹
        printFile(new File("F:\\Java\\視頻教程\\2018年尚學堂_高淇_Java300集"),0);
    }
    public static void printFile(File file, int leve){
        //輸出層數
        for(int i = 0; i < leve; i++){
            System.out.print("-");
        }
        //輸出文件名字
        System.out.println(file.getName());
        //判斷文件是否爲目錄
        if (file.isDirectory()){
            //將目錄下的文件存放到file數組中
            File[] files = file.listFiles();
            //遍歷
            for(File temp : files){
                //遞歸調用
                printFile(temp,leve+1);
            }
        }
    }

枚舉(enum)

枚舉體就是放置一些常量

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