java关键字_Java关键字

java关键字

Java keywords are the reserved words that are used by the Java compiler. These keywords have special meaning to the Java compiler. The reserved keywords help us in writing code and help the compiler in understanding the code and create the bytecode.

Java关键字是Java编译器使用的保留字。 这些关键字对Java编译器具有特殊含义。 保留的关键字可帮助我们编写代码,并帮助编译器理解代码并创建字节码。

We can’t create identifiers (class, variable, method) having the same name as the reserved keyword.

我们无法创建与保留关键字同名的标识符(类,变量,方法)。

Java关键字 (Java Keywords)

Java has 64 reserved keywords. We can divide them into the following categories.

Java有64个保留关键字。 我们可以将它们分为以下几类。

  1. Primitive types and void: 9 keywords

    原始类型和无效 :9个关键字
  2. Modifiers: 10 keywords

    修饰词 :10个关键字
  3. Declarations: 7 keywords

    声明 :7个关键字
  4. Control Flow: 15 keywords

    控制流 :15个关键字
  5. Miscellaneous: 13 keywords

    杂项 :13个关键字

Let’s have a brief look into all these keywords.

让我们简要地看一下所有这些关键字。

1.原始类型和无效 (1. Primitive types and void)

These keywords are used to create variables of primitive data types. The void is used when the method doesn’t return anything.

这些关键字用于创建原始数据类型的变量。 当方法不返回任何内容时,将使用void。

  1. boolean: creates a boolean variable. The only possible values are true and false and the default value is false.

    boolean :创建一个布尔变量。 唯一可能的值为true和false,默认值为false。
  2. byte: creates a byte variable. A byte takes 8-bits and ranges from -128 to 127.

    byte :创建一个字节变量。 字节为8位,范围为-128至127。
  3. char: used to create a character variable. It takes 2-bytes and it’s unsigned. The value ranges from 0 to 65,536.

    char :用于创建字符变量。 它需要2个字节,并且是无符号的。 取值范围是0〜65536。
  4. short: create a short variable of 2-bytes. The value ranges from -32,768 to 32,767.

    short :创建一个2字节的short变量。 取值范围是-32,768〜32,767。
  5. int: create an integer variable, takes 4-bytes and the range is from -2,147,483,648 to 2,147,483,647

    int :创建一个整数变量,占用4个字节,范围从-2,147,483,648到2,147,483,647
  6. long: creates a long variable, takes 8-bytes and the range is from -9,223,372,036,854,775,808 to
    9,223,372,036,854,775,807.

    long :创建一个long变量,占用8个字节,范围为-9,223,372,036,854,775,808至
    9,223,372,036,854,775,807。
  7. float: creates a signed floating point variable using 4-bytes.

    float :使用4个字节创建一个带符号的浮点变量。
  8. double: creates a signed double using 8-bytes.

    double :使用8字节创建带符号的double。
  9. void: used with methods to specify that it doesn’t return anything.

    void :与方法配合使用以指定它不返回任何内容。

Here is a simple example showing the use of these keywords. Notice the use of void keyword in the java main function to indicate that it doesn’t return anything.

这是显示这些关键字用法的简单示例。 请注意,在java main函数中使用void关键字来指示它不返回任何内容。

package com.journaldev.examples;

public class JavaKeywords {

	public static void main(String[] args) {
		boolean flag = true;
		byte b = 10;
		char c = 'a';
		short s = 2;
		int i = 1000;
		long l = 12345678L;
		float f = 1.23F;
		double d = 1.2e3D;

	}

}

2.修饰词关键字 (2. Modifiers Keywords)

These keywords are used to specify the scope of the variable, methods, and class.

这些关键字用于指定变量,方法和类的范围。

  1. public: used with class, methods, and fields to define their scope. The private identifiers can be accessed from anywhere.

    public :与类,方法和字段一起使用以定义其范围。 私有标识符可以从任何地方访问。
  2. protected: used with inner class, methods, and fields. The protected members are accessible only from within the class, the sub-classes and the other classes in the same package.

    protected :与内部类,方法和字段一起使用。 受保护的成员只能从同一包中的类,子类和其他类中访问。
  3. private: the private keyword is used with class variables, methods, and inner classes. The private members are accessible only within the class code.

    private :private关键字与类变量,方法和内部类一起使用。 私有成员只能在课程代码中访问。
  4. abstract: used to implement abstraction in Java. It’s used with a class declaration to create an abstract class. It can also be used with methods inside an abstract class to declare abstract methods. The abstract methods must be implemented by the subclass. We can’t create an instance of an abstract class.

    abstract :用于在Java中实现抽象。 它与类声明一起使用以创建抽象类。 它也可以与抽象类内部的方法一起使用,以声明抽象方法。 抽象方法必须由子类实现。 我们无法创建抽象类的实例。
  5. static: can be used with fields, methods, and inner class. The static members belong to the class and shared by all the instances of the class.

    static :可以与字段,方法和内部类一起使用。 静态成员属于该类,并由该类的所有实例共享。
  6. final: used with class, fields, and methods. The final class can’t be extended. The final fields value can’t be changed, once assigned. The final method can’t be overridden in the subclass.

    final :与类,字段和方法一起使用。 期末课程无法扩展。 最终字段值一旦分配就无法更改。 最终方法不能在子类中覆盖。
  7. transient: used with class fields to declare that they won’t be part of serialization. When an object is serialized, only non-transient fields are part of the serialization process. When the object is deserialized, the transient fields are assigned with their default values.

    瞬态 :与类字段一起使用,以声明它们将不属于序列化。 序列化对象时,只有非瞬态字段才是序列化过程的一部分。 反序列化对象时,会为瞬态字段分配其默认值。
  8. volatile: used with class fields to declare that their value might change by other threads. It was intended to use in case of multithreading, but it has several issues and it’s better to stick with synchronization.

    volatile :与类字段一起使用,以声明其值可能被其他线程更改。 它原本打算在多线程的情况下使用,但它有几个问题,最好坚持同步。
  9. synchronized: used with a method or to create a code block. It’s used to create a code block that can be executed by only one thread at a time. It’s very useful in maintaining data consistency in a multithreaded environment.

    同步的 :与方法一起使用或创建代码块。 它用于创建一个代码块,一次只能由一个线程执行。 这对于在多线程环境中保持数据一致性非常有用。
  10. native: used with java method declaration to specify that the method is not implemented in the same Java class, but rather in another language. For example, System class currentTimeMillis() and arraycopy() are native methods.

    native :与java方法声明一起使用,以指定该方法不是在同一Java类中实现,而是在另一种语言中实现。 例如, 系统类 currentTimeMillis()和arraycopy()是本机方法。

Recommended Readings:

推荐读物

Here is a simple example showing usage of modifier keywords in a Java program.

这是一个简单的示例,显示Java程序中修饰符关键字的用法。

package com.journaldev.examples;

public class JavaKeywords {

	private int y = 20;
	protected int x = 10;
	final String name = "JavaKeywords";
	static int count = 0;
	transient Object mutex = new Object();
	volatile int v_random = 999;

	public void print(String s) {
		System.out.println(s);
	}

	synchronized void bar(int i) {
		count = i;
	}

}

abstract class Abs {
	abstract void foo();
}

3.声明关键字 (3. Declarations Keywords)

These keywords are used to create an entity in Java.

这些关键字用于在Java中创建实体。

  1. class: used to create a class.

    class :用于创建一个类。
  2. interface: to create an interface.

    interface :创建一个接口。
  3. enum: added in Java 1.5 to create an enum.

    枚举 :在Java 1.5中添加以创建枚举。
  4. extends: used to create a subclass by extending another class.

    extend :用于通过扩展另一个类来创建子类。
  5. implements: used to implement an interface.

    Implements :用于实现一个接口。
  6. package: defines the pacakage for the class, interface, or enum definitions.

    package :定义类,接口或枚举定义的功能。
  7. throws: used with methods to specify the exceptions that the method may throw.

    throws :与方法一起使用,以指定方法可能抛出的异常。
package com.journaldev.examples;

class Cls{}
interface Int{}
enum En{}
class ClsChild extends Cls{}
class IntImpl implements Int{}

class Utils{
	void foo() throws Exception{}
}

4.控制流关键字 (4. Control Flow Keywords)

These keywords are used to define the execution flow of the java code.

这些关键字用于定义Java代码的执行流程。

  1. if: used to create if statement.

    if :用于创建if语句。
  2. else: used in conjunction with if to create an if-else statement.

    else :与if一起使用,以创建if-else语句。
  3. try: used to create a block of code for exception handling.

    try :用于创建代码块以进行异常处理。
  4. catch: used in conjunction with try block to catch the exceptions and process them.

    catch :与try块结合使用,以捕获异常并对其进行处理。
  5. finally: used with try-catch block. The finally block code is always executed.

    最终 :与try-catch块一起使用。 最终块代码始终执行。
  6. do: used in conjunction with while to create a do-while loop.

    do :与while结合使用以创建do-while循环。
  7. while: can be used to create while loop or do-while loop.

    while :可用于创建while循环或do-while循环。
  8. for: used to create a for loop.

    for :用于创建for循环。
  9. continue: used in the loops to skip the execution of the current cycle and proceed with the next cycle.

    继续 :在循环中使用,以跳过当前循环的执行并继续下一个循环。
  10. switch: used to create switch-case statements.

    switch :用于创建switch-case语句。
  11. case: used in conjunction with switch to create switch-case statements.

    case :与switch一起使用,以创建switch-case语句。
  12. default: used with the switch-case statements for the default case. From Java 8 onwards, it can also be used to create default methods in the interfaces. We can also use it to declare default value in an annotation.

    default :与switch-case语句一起用于默认大小写。 从Java 8开始,它还可用于在接口中创建默认方法。 我们还可以使用它在注释中声明默认值。
  13. break: used in the loops to end the execution of the current loop body.

    break :在循环中使用,以结束当前循环主体的执行。
  14. throw: used to throw exceptions.

    throw :用于引发异常。
  15. return: used to return value from a method.

    return :用于从方法返回值。

Here is an example showing the usage of the control flow keywords in Java.

这是显示Java中控制流关键字用法的示例。

package com.journaldev.examples;

public class JavaKeywords {
	static int x = 10;

	public static int foo() {

		if (x < 10) {
			// do something
		} else {
			// do something else
		}

		try {
			throw new Exception("Excp");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			System.out.println("Done");
		}

		do {
			// some code
		} while (false);

		for (int i = 0; i < x; i++) {
			if (i == 5)
				continue;
			if (i == 8)
				break;
			System.out.println(i);
		}

		switch (x) {
		case 1, 2, 3, 4, 5:
			System.out.println(x);
			break;
		default:
			System.out.println("NA");

		}
		return -1;
	}

}

5.其他关键字 (5. Miscellaneous Keywords)

  1. this: used to get access to the current object.

    this :用于访问当前对象。
  2. new: used to create an instance by calling the constructor.

    new :用于通过调用构造函数来创建实例。
  3. super: used incase of inheritance to access superclass methods, constructors, and variables.

    super :在继承的情况下用于访问超类方法,构造函数和变量。
  4. import: used to import a class so that we can use its functions.

    import :用于导入一个类,以便我们可以使用其功能。
  5. instanceof: An operator to check if an object is instance of a class.

    instanceof :用于检查对象是否为类实例的运算符。
  6. null: used to define null values of a variable.

    null :用于定义变量的空值。
  7. true: a boolean literal, returned when a condition is true.

    true :布尔文字,当条件为true时返回。
  8. false: a boolean literal, returned when a condition is false.

    false :布尔文字,当条件为false时返回。
  9. strictfp: used to restrict the precision and rounding of floating point calculations to ensure portability.

    strictfp :用于限制浮点计算的精度和舍入以确保可移植性。
  10. assert: added in Java 1.4 to create assertions.

    assert :在Java 1.4中添加以创建断言。
  11. _ (underscore): added in Java 1.9 for underscore literals.

    _(下划线) :在Java 1.9中添加了下划线文字。
  12. goto: not used.

    goto :未使用。
  13. const: not used.

    const :未使用。
package com.journaldev.examples;

import java.util.Arrays;

public class JavaKeywords {
	private int value;

	public int getValue() {
		return value;
	}

	public void setValue(int value) {
		this.value = value;
	}

	public static void main(String[] args) {
		JavaKeywords jk = new JavaKeywords();
		System.out.println(Arrays.toString(new int[] { 1, 2, 3 }));
		String s = "abc";
		if (s instanceof String) {
			System.out.println(s);
		}
		s = null;
		boolean flag = false;
		flag = true;
	}
}

class Base {
	Base(int i) {
	}
}

class Child extends Base {

	Child(int i) {
		super(i);
	}

}

如何检查字符串是否为关键字? (How to Check if a String is a Keyword?)

We can use SourceVersion.isKeyword() method to check if a string is part of reserved keywords or not.

我们可以使用SourceVersion.isKeyword()方法检查字符串是否为保留关键字的一部分。

package com.journaldev.examples;

import javax.lang.model.SourceVersion;

public class JavaKeywords {

	public static void main(String[] args) {
		String key = "try";
		if (SourceVersion.isKeyword(key)) {
			System.out.println(key + " is a keyword");
		} else {
			System.out.println(key + " is not a keyword");
		}
	}
}

结论 (Conclusion)

Java keywords define the way to write the code. It's the rulebook for the java compiler to interpret the code and generate bytecode.

Java关键字定义了编写代码的方式。 这是Java编译器解释代码并生成字节码的规则手册。

翻译自: https://www.journaldev.com/33226/java-keywords

java关键字

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值