Java知識點總結歸納

一、Java基礎語法

1、Java數據類型(byte、short、int、long、float、double、boolean、char)。

public class DataTypeDemo {
    public static void main(String[] args) {
        byte a = 10;
        short b = 100;
        int c = 1000;
        long d = 10000;
        float e = 1.2345f;
        double f = 1.23456789;
        boolean g = true;
        char h = 'a';
    }
}

2、Java關鍵字和標識符。

public class KeyWordDemo {
    public static void main(String[] args) {
        int class1 = 10;
        int abstract = 100;
        int num = class1 + abstract;
        System.out.println(num);
    }
}

3、Java注釋(單行注釋、多行注釋、文檔注釋)。

/**
 * 簡單計算器類
 */
public class Calculator {
    /**
     * 加法運算
     *
     * @param a 加數1
     * @param b 加數2
     * @return 加法運算結果
     */
    public static int add(int a, int b) {
        return a + b;
    }
}

二、Java面向對象編程

1、Java類與對象。

public class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void sayHello() {
        System.out.println("Hello, my name is " + name + ", I'm " + age + " years old.");
    }

    public static void main(String[] args) {
        Person person = new Person("Tom", 20);
        person.sayHello();
    }
}

2、繼承與接口。

public class Animal {
    public void eat() {
        System.out.println("Animal eat.");
    }
}

public interface Flyable {
    void fly();
}

public class Bird extends Animal implements Flyable {
    @Override
    public void fly() {
        System.out.println("Bird fly.");
    }
}

3、多態。

public class Animal {
    public void eat() {
        System.out.println("Animal eat.");
    }
}

public class Dog extends Animal {
    @Override
    public void eat() {
        System.out.println("Dog eat.");
    }
}

public class Cat extends Animal {
    @Override
    public void eat() {
        System.out.println("Cat eat.");
    }
}

public class Test {
    public static void main(String[] args) {
        Animal animal1 = new Dog();
        Animal animal2 = new Cat();
        animal1.eat();
        animal2.eat();
    }
}

三、Java IO流

1、字節流(InputStream、OutputStream)。

public class CopyBytes {
    public static void main(String[] args) throws IOException {
        FileInputStream in = null;
        FileOutputStream out = null;

        try {
            in = new FileInputStream("input.txt");
            out = new FileOutputStream("output.txt");
            int c;

            while ((c = in.read()) != -1) {
                out.write(c);
            }
        } finally {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        }
    }
}

2、字符流(Reader、Writer)。

public class CopyCharacters {
    public static void main(String[] args) throws IOException {
        FileReader inputStream = null;
        FileWriter outputStream = null;

        try {
            inputStream = new FileReader("input.txt");
            outputStream = new FileWriter("output.txt");
            int c;

            while ((c = inputStream.read()) != -1) {
                outputStream.write(c);
            }
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
            if (outputStream != null) {
                outputStream.close();
            }
        }
    }
}

3、緩衝流(BufferedInputStream、BufferedOutputStream、BufferedReader、BufferedWriter)。

public class CopyBuffered {
    public static void main(String[] args) throws IOException {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;

        try {
            bis = new BufferedInputStream(new FileInputStream("input.txt"));
            bos = new BufferedOutputStream(new FileOutputStream("output.txt"));
            byte[] buffer = new byte[1024];
            int length;

            while ((length = bis.read(buffer)) != -1) {
                bos.write(buffer, 0, length);
            }
        } finally {
            if (bis != null) {
                bis.close();
            }
            if (bos != null) {
                bos.close();
            }
        }
    }
}

四、Java異常處理

1、try-catch語句。

import java.io.*;

public class ReadFile {
    public static void main(String[] args) {
        try {
            File file = new File("file.txt");
            FileReader fileReader = new FileReader(file);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            String line;

            while ((line = bufferedReader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            System.out.println("讀取文件出錯" + e);
        }
    }
}

2、throws關鍵字。

public class Test {
    public static void main(String[] args) throws Exception {
        throw new Exception("test");
    }
}

五、Java集合框架

1、List(ArrayList、LinkedList)。

public class ArrayListTest {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("A");
        list.add("B");
        list.add("C");
        System.out.println(list.get(0));
        list.remove(0);
        System.out.println(list.get(0));
    }
}

2、Set(HashSet、LinkedHashSet)。

public class HashSetTest {
    public static void main(String[] args) {
        HashSet<String> set = new HashSet<>();
        set.add("A");
        set.add("B");
        set.add("C");
        System.out.println(set.contains("A"));
        set.remove("A");
        System.out.println(set.contains("A"));
    }
}

3、Map(HashMap、TreeMap、LinkedHashMap)。

public class HashMapTest {
    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<>();
        map.put("A", 1);
        map.put("B", 2);
        map.put("C", 3);
        System.out.println(map.get("A"));
        map.remove("A");
        System.out.println(map.get("A"));
    }
}

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/235892.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-12 11:57
下一篇 2024-12-12 11:57

相關推薦

  • Java JsonPath 效率優化指南

    本篇文章將深入探討Java JsonPath的效率問題,並提供一些優化方案。 一、JsonPath 簡介 JsonPath是一個可用於從JSON數據中獲取信息的庫。它提供了一種DS…

    編程 2025-04-29
  • java client.getacsresponse 編譯報錯解決方法

    java client.getacsresponse 編譯報錯是Java編程過程中常見的錯誤,常見的原因是代碼的語法錯誤、類庫依賴問題和編譯環境的配置問題。下面將從多個方面進行分析…

    編程 2025-04-29
  • Java騰訊雲音視頻對接

    本文旨在從多個方面詳細闡述Java騰訊雲音視頻對接,提供完整的代碼示例。 一、騰訊雲音視頻介紹 騰訊雲音視頻服務(Cloud Tencent Real-Time Communica…

    編程 2025-04-29
  • Java Bean加載過程

    Java Bean加載過程涉及到類加載器、反射機制和Java虛擬機的執行過程。在本文中,將從這三個方面詳細闡述Java Bean加載的過程。 一、類加載器 類加載器是Java虛擬機…

    編程 2025-04-29
  • Java Milvus SearchParam withoutFields用法介紹

    本文將詳細介紹Java Milvus SearchParam withoutFields的相關知識和用法。 一、什麼是Java Milvus SearchParam without…

    編程 2025-04-29
  • Java 8中某一周的周一

    Java 8是Java語言中的一個版本,於2014年3月18日發布。本文將從多個方面對Java 8中某一周的周一進行詳細的闡述。 一、數組處理 Java 8新特性之一是Stream…

    編程 2025-04-29
  • Java判斷字符串是否存在多個

    本文將從以下幾個方面詳細闡述如何使用Java判斷一個字符串中是否存在多個指定字符: 一、字符串遍歷 字符串是Java編程中非常重要的一種數據類型。要判斷字符串中是否存在多個指定字符…

    編程 2025-04-29
  • VSCode為什麼無法運行Java

    解答:VSCode無法運行Java是因為默認情況下,VSCode並沒有集成Java運行環境,需要手動添加Java運行環境或安裝相關插件才能實現Java代碼的編寫、調試和運行。 一、…

    編程 2025-04-29
  • Java任務下發回滾系統的設計與實現

    本文將介紹一個Java任務下發回滾系統的設計與實現。該系統可以用於執行複雜的任務,包括可回滾的任務,及時恢復任務失敗前的狀態。系統使用Java語言進行開發,可以支持多種類型的任務。…

    編程 2025-04-29
  • Java 8 Group By 會影響排序嗎?

    是的,Java 8中的Group By會對排序產生影響。本文將從多個方面探討Group By對排序的影響。 一、Group By的概述 Group By是SQL中的一種常見操作,它…

    編程 2025-04-29

發表回復

登錄後才能評論