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/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

发表回复

登录后才能评论