Java基礎看這個就足夠了【Java基礎內容總結】

Java基礎看這個就足夠了【Java基礎內容總結】

一、創建第一個工程
二、Java基本數據類型
三、Java的基本流程控制語句
1、If-else
2、Switch
3、For
4、While
5、Break 和 Continue
6、Return
四、對象
五、方法
六、訪問權限
七、繼承和多態
1、toString()繼承和重寫實踐
2、多態
3、接口
4、抽象類
八、容器
九、異常
1、運行時異常
2、檢查性異常
3、自定義異常
十、I/O
1、控制檯I/O
2、文件I/O
一、創建第一個工程

打開Eclipse,創建HelloWorld程序
打開Eclipse選擇菜單 File --> New --> Java Project新建工程

填寫工程名稱爲HelloWorld
右鍵工程名創建類

輸入代碼

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World");
        // TODO Auto-generated constructor stub
    }

}

1
2
3
4
5
6
7
8
9
10
右鍵運行

運行結果顯示


二、Java基本數據類型

char size = 16
char min = 0
char max = 65535
char default = 0
byte size = 8
byte min = -128
byte max = 127
byte default = 0
short size = 16
short min = -32768
short max = 32767
short default = 0
int size = 32
int min = -2147483648
int max = 2147483647
int default = 0
long size = 64
long min = -9223372036854775808
long max = 9223372036854775807
long default = 0
float size = 32
float min = 1.4E-45
float max = 3.4028235E38
float default = 0.0
double size = 64
double min = 4.9E-324
double max = 1.7976931348623157E308
double default = 0.0
max int = 2147483647
max int to short = -1
max int to long = 2147483647
max int to float = 2.14748365E9
max int to double + 2.147483647E9
max int = 2147483647
max int + 1 = -2147483648
min int = -2147483648
min int - 1 = 2147483647
max double = 1.7976931348623157E308
max double + max double = Infinity
- max double = -1.7976931348623157E308
- max double - max double = -Infinity
boolean value = true
int i/j = 1
double i/j = 1.0
double (double)i/j = 1.2
double i*1.0/j = 1.2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
三、Java的基本流程控制語句

1、If-else

if-else語句主要是根據if的判斷結果,選擇不同的分支路徑,可以if-else嵌套,也可以單獨使用if語句,還可以使用 if-else if-else if-…-else進行嵌套

public static void testIfElse(int num) {
        System.out.println("num = " + num);
        if(num < 10) {
            System.out.println("num < 10");
        }
        
        if(num < 100) {
            System.out.println("num < 100");
        }else {
            System.out.println("num >= 100");
        }
        
        if(num < 50) {
            System.out.println("num < 50");
        }else if(num>=50 && num <100) {
            System.out.println("num>=50 && num<100");
        }else {
            System.out.println("num > 100");
        }
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2、Switch

當需要判斷的條件比較多時會出現很多的if-else,這種代碼的可讀性比較差,所以我們可以選擇使用switch語句

public static void testSwitch(Color color) {
        switch (color) {
        case RED:
            System.out.println("color is " + Color.RED);
            break;
        case GREEN:
            System.out.println("color is " + Color.GREEN);
            break;
        case BLACK:
            System.out.println("color is " + Color.BLACK);
            break;
        case YELLOW:
            System.out.println("color is " + Color.YELLOW);
            break;
        default:
            break;
        }
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
3、For

for循環是依靠三個字段達到循環的目的,三個字段分別是初始值,結束條件,遊標移動。也就是設置一個初始條件,每次循環進行一次遊標移動,當達到結束條件時推出循環。

public static void testFor() {
        int[] array = new int[10];
        for(int i=0;i<10;i++) {
            array[i] = i;
        }
        
        for(int j:array) {
            System.out.print(j+" ");
        }
    }

1
2
3
4
5
6
7
8
9
10
11
4、While

while語句是循環語句的另一種方式,當while後面的條件成立時繼續循環,當條件不成立是時退出循環,也可以使用do-while嵌套,在do後面首先執行一次循環再到while中進行循環是否繼續的檢測。

public static void testWhile() {
        int[] array = new int[10];
        int i = 0;
        while(i<array.length) {
            array[i] = i;
            i++;
        }
        
        int j = 0;
        do {
            System.out.print(array[j]+" ");
            j++;
        } while (j<array.length);
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
5、Break 和 Continue

在循環中都起着很重要的作用,其中break語句用於結束循環體也就是退出本層循環,continue語句用於結束本次循環也就是退出本次循環進行下一次循環。

public static void testBreakAndContinue() {
        int[] array = new int[10];
        for(int i=0;i<10;i++) {
            array[i] = i;
        }
        
        for(int j:array) {
            if(j == 3) {
                continue;
            }
            if(j == 6) {
                break;
            }
            System.out.print(j+" ");
        }
        
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
6、Return

return語句可以退出當前方法,並且可以帶返回值void類型的方法返回值會有一個隱式的return作爲函數的返回,除了finally特例之外,return後面的語句不會被執行

public static void testReturn(int num) {
        System.out.println("testReturn start*******");
        if(num == 1) {
            return;
        }else if(num == 2) {
            try {
                System.out.println("testReturn try *******");
                return;
            } finally {
            // 此處的語句雖然在return語句後面但是依然會被執行。
                System.out.println("testReturn finally*******");
            }
        }
        System.out.println("testReturn end*******");
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
四、對象

我們將生活中的任何東西都可以抽象成對象,比如手機抽象成對象,那麼手機的硬件如電池、系統、屏幕等就是對象裏的字段;而具體的打電話,打短信,逛淘寶就是對象裏的方法。面向對象的核心其實就是把任何事物都能夠抽象成對象類,這個事物具備的能力就是對象的方法,事物具備的實際事物就是抽象出來的字段。

public class Student {
    // 對象公共字段 
    private int age = 24;
    private String name = "HARRY";
    
    private static int count = 0;
    
    public static int getCount() {
        return count;
    }
    
    、、
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
    public Student(int age,String name) {
        count++;
        this.age = age;
        this.name = name;
    }
    
    public Student() {
        count++;
        this.age = 0;
        this.name = "HARRY";
    }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
五、方法

通俗的說,方法就是一個函數體實現某種功能的模塊,方法中重要的有四個部分:

返回值:就是方法實現功能或者運行之後返回的內容,使用return返回,默認空返回值

方法名:方法的名稱,也就是函數名,可是使用某些關鍵字修飾方法從而實現其他功能

參數:調用方法時所傳入的參數,在方法名後面的括號內標記

方法體:方法的具體實現

類調用之前會進行初始化,我們使用構造器實現,構造器就是與類名相同並且沒有返回值的方法,並且構造器是可以有多個的,並且參數可以不同。

public class Student {
    // 類名是Student
    private int age = 18;
    private String name = "todo";
    
    // 第一個構造器
    public Student(int age,String name) {
        count++;
        this.age = age;
        this.name = name;
    }
    
    // 第二個構造器,這是無參數構造器必須有,否則無法正確編譯
    // 因爲默認的構造器沒有自動生成
    public Student() {
        count++;
        this.age = 0;
        this.name = "todo";
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
六、訪問權限

java有4種訪問權限,分別是公開訪問權限,保護訪問權限、包含訪問權限、私有訪問權限

權限名稱

關鍵字

權限範圍

用法

公開訪問權限

public

所有類都可以訪問

一些希望別人使用的方法或者公開的API

保護訪問權限

protected

派生子類可用

不希望所有人都可以使用,但派生子類可用或者更改

包訪問權限

default

默認訪問權限無關鍵字

限於同一包內,僅希望同一個包裏的其他類使用

私有訪問權限

private

僅自己類內部可用

類裏的方法完全私有,只能類內部使用

七、繼承和多態

繼承是指派生類基於基類的一種針對屬性或者行爲的繼承,從而擁有基類相同的屬性或行爲。
多態指的是派生類在基類的基礎上進行重寫,從而能夠表現出不同的性狀的特性。

1、toString()繼承和重寫實踐

public class Person {
    public long id;
    public String name;
    public Eyes eyes = new Eyes();
    // 創建一個眼睛的類
    
    // 創建person的構造器
    public Person(long id, String name) {
        this.id = id;
        this.name = name;
    }

    // 對toString進行重寫
    @Override
    public String toString() {
        return "id = " + this.id + " name = " + this.name;
    }

    // 對equals進行重寫
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        Person person = (Person) obj;
        if ((this.id == person.id) && (this.name == person.name)) {
            return true;
        }
        return false;
    }
    
    public static class Eyes {
        public String left = "zuoyan";
        public String right = "youyan";
    }

    // 使用main函數調用實現某些功能
    public static void main(String[] args) {
         //創建一個xiaonming的實例1
        Person person1 = new Person(1, "xiaoming");
        //創建一個xiaonming的實例2
        Person person2 = new Person(1, "xiaoming");
        //輸出實例1和實例2的對比使用等號表示計算兩個實例的地址是否一致
        System.out.println("person 1 == person 2 = " + (person1 == person2));
        // 輸出實例1和實例2使用equals函數的計算結果,實例使用的equals是重寫之後的函數
        System.out.println("person 1 equals person 2 = " + (person1.equals(person2)));
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
2、多態

首先我們創建一個動物的基類

public class Animal {
    public int weight;
    
    public Animal(int weight) {
        this.weight = weight;
    }
    
    public void move() {
        System.out.println("animal can move!");
    }
    
    public void eat(){
        System.out.println("animal can eat!");
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
然後對基類進行繼承

// 對Animal 進行繼承
public class Tiger extends Animal{
    public String roar = "ao";
    
    public Tiger(int weight,String roar) {
        super(weight);
        this.roar = roar;
    }
    // 對move方法進行重寫之後就表現出類多態的特性從而構成老虎
    // 這裏重寫成其他內容 如gegege就變成了雞
    @Override
    public void move() {
        System.out.println("tiger can run!");
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
3、接口

java不能通過多重繼承來引入更多的功能,並且又無法將一些能力全部都封裝在基類object中,所以我們就需要通過接口來實現一些針對衆多 實例的一些通用能力。

4、抽象類

簡單描述抽象類就是不可以創建實例的基類,使用abstract描述。在抽象類中可以定義抽象的方法,也是使用abstract描述在方法名前,方法就不需要再基類中實現,而在派生類中必須實現抽象的方法否則就會報錯,這樣就避免了創建一些沒有意義的派生類。

public abstract class Animal {
    public int weight;
    
    public Animal(int weight) {
        this.weight = weight;
    }
    
    public void move() {
        System.out.println("animal can move!");
    }
    
    public abstract void eat();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
八、容器

容器是存放對象的區域,當大量的對象需要在內存中存在,並且單個對象分別使用起來很不方便的時候就可以使用容器,目前比較常見的有List、Set、Map,使用方法與其他編程語言類似,其實換一個名稱也就是數據結構,存儲的內容也不僅僅是類。

九、異常

程序運行的過程中我們需要檢查數據等操作的合法性,但是當我們無法驗證這些內容的時候,我們就可以使用異常來保證程序的健壯性。

1、運行時異常

其實這種異常在編程的過程中都是可以避免的,並且這類異常一般都不會影響程序的編譯是否通過

public static void testDivisor() {
        try {
            int i = 1/0;
            System.out.println("i = " + i);
        } catch (Exception e) {
            System.out.println("divisor can not be 0");
        }
    }
1
2
3
4
5
6
7
8
2、檢查性異常

這類異常無法使用編程技巧進行規避,並且雜編寫的過程中會影響代碼的編譯通過與否,所以這類問題就需要使用異常來規避了


import java.io.FileReader;

public class FileExceptionDemo {
    
    public static String readFile(){
        boolean bool = true;
        StringBuilder builder = new StringBuilder();
        try {
            FileReader fReader = new FileReader("文件路徑");
            char[] cs = new char[10];
            while (fReader.read(cs)!=-1) {
                builder.append(cs);
                cs = new char[10];
            }
            fReader.close();
        } catch (Exception e) {
            bool = false;
            e.printStackTrace();
        } finally {
            if(bool) {
                System.out.println("read file ok!");
            }else {
                System.out.println("read file fail!");
                builder.replace(0, builder.length(), "fail");
            }
        }
        return builder.toString();
    }
    
    public static void main(String[] args) {
        System.out.println(readFile());
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
3、自定義異常

我們在編寫代碼的過程中希望碰到某些異常時,拋出的內容能夠讓我們快速的調查出錯的原因,這時候我們就可以自定義異常了。而自定義異常只需要對相關的異常進行繼承,然後實現自己需要的功能就可以了。

public class CustomException extends Exception{

}
1
2
3
十、I/O

通過前面的內容我們可以實現基本的程序功能,但是在使用java構建一個稍大的系統時需要使用到控制檯、文件、數據庫、緩存等衆多的其他java服務,所以我們需要使用I/O來解決這些問題。

1、控制檯I/O

控制檯其實就是控制輸入輸出的Console窗口,通過窗口輸入數據我們能夠讀取到對應的數據並將其反饋輸出到桌面。


2、文件I/O

前面有簡單的文件操作,這裏再重新實現一次。

這裏讀取的是一個python的requirements文件並輸出文件的內容到前端。

import java.io.FileReader;

public class FileExceptionDemo {
    
    public static String readFile(){
        boolean bool = true;
        StringBuilder builder = new StringBuilder();
        try {
            FileReader fReader = new FileReader("C:\\Users\\Harry\\Desktop\\requirements.txt");
            char[] cs = new char[10];
            while (fReader.read(cs)!=-1) {
                builder.append(cs);
                cs = new char[10];
            }
            fReader.close();
        } catch (Exception e) {
            bool = false;
            e.printStackTrace();
        } finally {
            if(bool) {
                System.out.println("read file ok!");
            }else {
                System.out.println("read file fail!");
                builder.replace(0, builder.length(), "fail");
            }
        }
        return builder.toString();
    }
    
    public static void main(String[] args) {
        System.out.println(readFile());
    }
}

————————————————
版權聲明:本文爲CSDN博主「harry_c」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/harry_c/article/details/103108304

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