Java中所有的关键字

Java关键字共有53个,其中包括51个正在使用的关键字和2个保留字(const和goto)。以下是这些关键字的列表及其相关解释:

1. 保留字(2个)

  • const:在Java中,const被保留为关键字,但实际上并未被使用。在其他编程语言中,const用于声明常量。
  • goto:goto同样被保留为关键字,但在Java中并不支持goto语句。

2. 基本数据类型(8个)

  • byte:字节类型,占用1字节(8位),取值范围为-128到127。
  • char:字符类型,占用2字节(16位),取值范围为0到65535。
  • short:短整型,占用2字节(16位),取值范围为-32768到32767。
  • int:整型,占用4字节(32位),取值范围为2-231到2231-1。
  • long:长整型,占用8字节(64位),取值范围为2-263到2263-1。声明long型变量时,通常以"L"或"l"结尾。
  • float:单精度浮点类型,占用4字节(32位)。
  • double:双精度浮点类型,占用8字节(64位)。
  • boolean:布尔类型,只有true和false两种值。
        // 基础数据类型关键字
        byte b = 1;
        char c = 'a';
        short s = 1;
        int i = 1;
        long l = 1L;
        float f = 1.0f;
        double d = 1.0;
        boolean bool = true;

3. 访问控制修饰符(3个)

  • public:公有的,表示类、变量或方法可以被任何其他类访问。
  • protected:受保护的,表示类、变量或方法可以被同一包内的类以及所有子类访问。
  • private:私有的,表示类、变量或方法只能被其所在类内部访问。
private static class PrivateClass {
        String name;
        String id;
        /*

        public:公有的,表示类、变量或方法可以被任何其他类访问。

        protected:受保护的,表示类、变量或方法可以被同一包内的类以及所有子类访问。

        private:私有的,表示类、变量或方法只能被其所在类内部访问。

         */
        public PrivateClass(String name, String id) {
            this.name = name;
            this.id = id;
        }

        protected String getName() {
            return name;
        }

        private String getId() {
            return id;
        }

    }

4. 包相关(2个)

  • package:用于声明一个包,将相关的类组织在一起。
  • import:用于导入已存在的包或类,以便在当前文件中使用。
// package 用于定义该类的包位置
package org.groupg.practice.jdk17practice.demo;

// import 用于明确该类引用的对象类是什么

import java.util.Arrays;

5. 类、接口和枚举(6个)

  • class:用于声明一个类。
  • interface:用于声明一个接口。
  • enum:用于声明一个枚举类型。
  • abstract:用于声明抽象类或抽象方法。
  • extends:表示一个类是另一个类的子类,即继承关系。
  • implements:表示一个类实现了某个接口。

interface Animal{
    void eat();
}

abstract class Person implements Animal{
    String name;
    int sex;
    Person(String name ,int sex){
        this.name = name;
        this.sex = sex;
    }
}

class Man extends Person{

    Man(String name, int sex) {
        super(name, sex);
    }

    @Override
    public void eat() {
        System.out.println("Man eat");
    }
}

class Woman extends Person{

    Woman(String name, int sex) {
        super(name, sex);
    }

    @Override
    public void eat() {
        System.out.println("Woman eat");
    }
}

class Cat implements Animal{

    @Override
    public void eat() {
        System.out.println("cat eat");
    }
}

6. 流程控制(12个)

  • ifelse:用于条件判断。
  • switchcasedefault:用于多路分支选择。
  • forwhiledo-while:用于循环控制。
  • break:跳出当前循环或switch语句。
  • continue:跳过当前循环的剩余部分,继续下一次循环。
  • return:从方法中返回值并结束方法执行。
public static void main(String[] args) {

        // 基础数据类型关键字
        byte b = 1;
        char c = 'a';
        short s = 1;
        int i = 1;
        long l = 1L;
        float f = 1.0f;
        double d = 1.0;
        boolean bool = true;

        Arrays.sort(args);

        if (args.length == 0) {

            System.out.println("默认情况下的处理。");

        } else if (args.length == 1) {

            System.out.println("当命令中给了一个参数时的。");

        } else {
            System.out.println("当命令中给了多个参数时的。");
        }

        callSwitch("getName");
        callSwitch("getId");
        callWhile();
        callReturn();

    }
    public static void callSwitch(String method) {
        switch (method) {
            case "getName":
                System.out.println("getName:" + method);
                break;
            case "getId":
                System.out.println("getId:" + method);
                break;
            default:
                System.out.println("default:" + method);
                break;
        }
    }
    public static void callWhile() {

        for (int i = 1; i < 11; i++) {
            for (int j = 1; j < i; j++) {
                System.out.print("\t" + i + " * " + j + " = " + (i * j));
            }
            System.out.println();
        }

        int i = 1;
        while (i <= 9) {
            int j = 1;
            while (j <= i) {
                System.out.print("\t" + i + " * " + j + " = " + (i * j));
                j++;
            }
            System.out.println();
            i++;
        }

        if (i > 13) return;
        i = 1;
        do {
            int j = 1;
            do {
                System.out.print("\t" + i + " * " + j + " = " + (i * j));
                j++;
            } while (j <= i);
            System.out.println();
            i++;
        } while (i <= 9);

    }
    public static String callReturn() {
        StringBuffer sb = new StringBuffer();
        for (int i = 1; i < 11; i++) {
            if (i % 2 == 0)
                continue;
            if (i == 10) {
                break;
            }
            for (int j = 1; j <= i; j++) {
                sb.append("\t" + i + " * " + j + " = " + (i * j));
            }
            sb.append("\r\n");
        }
        return sb.toString();
    }

7. 异常处理(5个)

  • trycatchfinally:用于异常处理。
  • throw:抛出异常。
  • throws:声明方法可能抛出的异常。
        File file = new File("/Data/DemoFile");
        try {
            file.createNewFile();
        } catch (IOException e) {
            file.delete();
            file.createNewFile();
        }

8. 其他关键字(11个)

  • new:用于创建对象的实例。
  • this:引用当前对象。
  • super:引用当前对象的父类对象。
  • static:表示静态的,可用于修饰变量、方法和代码块。
  • final:表示最终的,可用于修饰变量、方法和类。
  • native:声明一个方法是用本地代码(如C或C++)实现的。
  • synchronized:用于同步方法或代码块,防止多个线程同时访问。
  • transient:声明变量不需要序列化。
  • volatile:表示变量可能会被并发地修改,强制从主内存中读写。
  • strictfp:确保浮点运算遵循IEEE 754标准。
  • instanceof:用于判断对象是否是指定类的实例。

全部示例如下

// package 用于定义该类的包位置
package org.groupg.practice.jdk17practice.demo;

// import 用于明确该类引用的对象类是什么

import java.io.File;
import java.io.IOException;
import java.util.Arrays;


public class KeyWordsDemo {

    static final String USERDIR = System.getProperty("user.dir");

    synchronized static String getUserdir() {
        return USERDIR;
    }

    private volatile boolean running = true;

    public void doWork() {
        while (running) {
            // 执行一些工作
            System.out.println(Arrays.toString(Thread.currentThread().getStackTrace()));
        }
    }

    public void stopWork() {
        running = false; // 当这个值被改变时,所有线程都会看到最新的值
    }

    private static class PrivateClass {
        String name;
        transient String id;


        /*

        public:公有的,表示类、变量或方法可以被任何其他类访问。

        protected:受保护的,表示类、变量或方法可以被同一包内的类以及所有子类访问。

        private:私有的,表示类、变量或方法只能被其所在类内部访问。

         */
        public PrivateClass(String name, String id) {
            this.name = name;
            this.id = id;
        }

        protected String getName() {
            return name;
        }

        private String getId() {
            return id;
        }

    }

    public static void main(String[] args) throws IOException {

        // 基础数据类型关键字
        byte b = 1;
        char c = 'a';
        short s = 1;
        int i = 1;
        long l = 1L;
        float f = 1.0f;
        double d = 1.0;
        boolean bool = true;

        Arrays.sort(args);

        if (args.length == 0) {

            System.out.println("默认情况下的处理。");

        } else if (args.length == 1) {

            System.out.println("当命令中给了一个参数时的。");

        } else {
            System.out.println("当命令中给了多个参数时的。");
        }

        callSwitch("getName");
        callSwitch("getId");
        callWhile();
        callReturn();
        getDounle();

        File file = new File("/Data/DemoFile");
        try {
            file.createNewFile();
        } catch (IOException e) {
            file.delete();
            file.createNewFile();
        }

        if (file instanceof File) {
            KeyWordsDemo example = new KeyWordsDemo();
            new Thread(example::doWork).start();
            // 让线程运行一段时间
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            example.stopWork(); // 停止工作线程
        }
    }

    public strictfp static double getDounle() {
        double a = 0.1;
        double b = 0.2;
        double sum = a + b;

        return sum; // 在 strictfp 模式下,这将打印出严格的IEEE 754加法结果
    }

    public static void callSwitch(String method) {
        switch (method) {
            case "getName":
                System.out.println("getName:" + method);
                break;
            case "getId":
                System.out.println("getId:" + method);
                break;
            default:
                System.out.println("default:" + method);
                break;
        }
    }

    public static void callWhile() {

        for (int i = 1; i < 11; i++) {
            for (int j = 1; j < i; j++) {
                System.out.print("\t" + i + " * " + j + " = " + (i * j));
            }
            System.out.println();
        }

        int i = 1;
        while (i <= 9) {
            int j = 1;
            while (j <= i) {
                System.out.print("\t" + i + " * " + j + " = " + (i * j));
                j++;
            }
            System.out.println();
            i++;
        }

        if (i > 13) return;
        i = 1;
        do {
            int j = 1;
            do {
                System.out.print("\t" + i + " * " + j + " = " + (i * j));
                j++;
            } while (j <= i);
            System.out.println();
            i++;
        } while (i <= 9);

    }

    public static String callReturn() {
        StringBuffer sb = new StringBuffer();
        for (int i = 1; i < 11; i++) {
            if (i % 2 == 0)
                continue;
            if (i == 10) {
                break;
            }
            for (int j = 1; j <= i; j++) {
                sb.append("\t" + i + " * " + j + " = " + (i * j));
            }
            sb.append("\r\n");
        }
        return sb.toString();
    }
}


interface Animal {
    void eat();
}

abstract class Person implements Animal {
    String name;
    int sex;

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

class Man extends Person {

    Man(String name, int sex) {
        super(name, sex);
    }

    @Override
    public void eat() {
        System.out.println("Man eat");
    }
}

class Woman extends Person {

    Woman(String name, int sex) {
        super(name, sex);
    }

    @Override
    public void eat() {
        System.out.println("Woman eat");
    }
}

class Cat implements Animal {

    @Override
    public void eat() {
        System.out.println("cat eat");
    }
}

关键字版本提供信息

关于Java关键字是在哪个版本提供的,大多数关键字从Java的早期版本(如Java 1.0或更早)就开始存在,并一直沿用至今。不过,也有一些关键字或特性是在后续版本中引入的,例如:

  • enum 关键字是在Java 5(也称为JDK 1.5)中引入的,用于定义枚举类型。
  • assert 关键字虽然在早期版本中就存在,但在Java 1.4及以前版本中默认是禁用的,从Java 1.4开始可以通过命令行参数启用,而在Java 6及后续版本中默认启用。
  • strictfp 关键字在Java的早期版本中就已存在,用于确保浮点运算的精确性。在 JDK17版本不在需要在方法上使用。