What are keywords in java

Java Keywords

Java has a set of keywords that are reserved words that cannot be used as variables, methods, classes, or any other identifiers:

Keyword Description
abstract A non-access modifier. Used for classes and methods: An abstract class cannot be used to create objects (to access it, it must be inherited from another class). An abstract method can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from)
assert For debugging
boolean A data type that can only store true and false values
break Breaks out of a loop or a switch block
byte A data type that can store whole numbers from -128 and 127
case Marks a block of code in switch statements
catch Catches exceptions generated by try statements
char A data type that is used to store a single character
class Defines a class
continue Continues to the next iteration of a loop
const Defines a constant. Not in use — use final instead
default Specifies the default block of code in a switch statement
do Used together with while to create a do-while loop
double A data type that can store whole numbers from 1.7e−308 to 1.7e+308
else Used in conditional statements
enum Declares an enumerated (unchangeable) type
exports Exports a package with a module. New in Java 9
extends Extends a class (indicates that a class is inherited from another class)
final A non-access modifier used for classes, attributes and methods, which makes them non-changeable (impossible to inherit or override)
finally Used with exceptions, a block of code that will be executed no matter if there is an exception or not
float A data type that can store whole numbers from 3.4e−038 to 3.4e+038
for Create a for loop
goto Not in use, and has no function
if Makes a conditional statement
implements Implements an interface
import Used to import a package, class or interface
instanceof Checks whether an object is an instance of a specific class or an interface
int A data type that can store whole numbers from -2147483648 to 2147483647
interface Used to declare a special type of class that only contains abstract methods
long A data type that can store whole numbers from -9223372036854775808 to 9223372036854775808
module Declares a module. New in Java 9
native Specifies that a method is not implemented in the same Java source file (but in another language)
new Creates new objects
package Declares a package
private An access modifier used for attributes, methods and constructors, making them only accessible within the declared class
protected An access modifier used for attributes, methods and constructors, making them accessible in the same package and subclasses
public An access modifier used for classes, attributes, methods and constructors, making them accessible by any other class
requires Specifies required libraries inside a module. New in Java 9
return Finished the execution of a method, and can be used to return a value from a method
short A data type that can store whole numbers from -32768 to 32767
static A non-access modifier used for methods and attributes. Static methods/attributes can be accessed without creating an object of a class
strictfp Restrict the precision and rounding of floating point calculations
super Refers to superclass (parent) objects
switch Selects one of many code blocks to be executed
synchronized A non-access modifier, which specifies that methods can only be accessed by one thread at a time
this Refers to the current object in a method or constructor
throw Creates a custom error
throws Indicates what exceptions may be thrown by a method
transient A non-accesss modifier, which specifies that an attribute is not part of an object’s persistent state
try Creates a try. catch statement
var Declares a variable. New in Java 10
void Specifies that a method should not have a return value
volatile Indicates that an attribute is not cached thread-locally, and is always read from the «main memory»
while Creates a while loop
Читайте также:  Php file directory class

Note: true , false , and null are not keywords, but they are literals and reserved words that cannot be used as identifiers.

Источник

Java Reserved and Contextual Keywords

Java has 51 reserved words that have very specific meanings and cannot be used as identifiers in the application code. Also, 16 contextual keywords are treated as keywords when found in a specific context. Programmers should not use these keywords for anything other than what they are meant to be.

1. What is a Keyword in Java?

The keywords are predefined, reserved words that have a very specific meaning for the compiler. These keywords cannot be used as variables, methods, classes, or any other identifiers.

In the following statement, int is a keyword that indicates that the variable age is of integer type (32-bit signed two’s complement integer). We can’t use int as a variable name etc. Using keywords as identifiers will result in compilation errors.

  • The keywords const and goto are reserved, even though they are not currently used.
  • true , false , and null might seem like keywords, but they are literal; we cannot use them as identifiers in our programs.
  • strictfp was added in JDK 1.2.
  • assert was added in JDK 1.4.
  • enum was added in JDK 1.5.
  • Later versions of features such as sealed classes, records and JPMS added few more contextual keywords.

The following 51 keywords cannot be used as identifiers.

Keyword Description
abstract Used with classes and methods. An abstract class cannot be instantiated. An abstract method is incomplete without the body and must be implemented in the child class to create an instance of the child class.
assert enables us to test the assumptions about our program.
boolean represents only one of two possible values i.e. either true or false .
break is used to terminate for , while , or do-while loop. It may also be used to terminate a switch statement as well.
byte can store whole numbers from -128 and 127.
case represents a block of code in switch statements.
catch follows the try block and handles the checked exceptions thrown by try block and any possible unchecked exceptions.
char used to store a single character.
class defines a class.
const is a reserved keyword for constant values. Use final instead.
continue skips the current iteration of a for, while, or do-while loops and jumps to the next iteration.
default used to specify the default block in a switch statement and default methods in functional interfaces.
do used to contain the statements to execute repeatedly until the condition in the while statement is true.
double used to declare a variable that can hold 64-bit floating-point number.
else used to indicate the alternative branches in an if statement.
enum is a type whose fields consist of a fixed set of constants.
extends used for extending a class.
final used with class variables, methods or classes. A final variable cannot be assigned another value after it has been initialized. A final method cannot be overridden in the child class. No class can subclass a final class.
finally contains code to be executed everytime a try-catch block is completed – either with errors or without any error.
float used to declare a variable that can hold a 32-bit floating-point number.
for start a loop to execute a set of instructions repeatedly when a condition is true. If the number of iterations is known, it is recommended to use for loop.
goto Currently, not in use.
if used for writing conditional statements.
implements used for implementing an interface.
import import a package, class or interface to the current class.
instanceof Checks whether an object is an instance of a specific class or an interface.
int used to store a 32-bit integer value.
interface declares an interface.
long used to store a 64-bit integer value.
native indicates native code (platform-specific).
new creates a new object of the specified class.
package declares a package for storing the related classes.
private access modifier to indicate that a method or variable may be accessed only in the class in which it is declared.
protected access modifier to indicate that a class, method or variable may be accessed only in the current package, or inherited outside the current package.
public access modifier to indicate that a class, method or variable is accessible everywhere.
return used to return from a method when its execution is complete.
short used to store a 16-bit integer value.
static indicates that a variable or method belongs to the class object, not to the individual instances of that class.
strictfp used to restrict the floating-point calculations to ensure portability.
super used to refer to parent class objects.
switch help in providing multiple possible execution paths for a program.
synchronized marks a block or method a critical section where one and only one thread is executing at a time.
this used to refer to the current object.
throw used to explicitly throw an exception from a method or constructor.
throws used to declare the list of exceptions that may be thrown by that method or constructor.
transient used on class attributes/variables to indicate that the serialization process of this class should ignore such variables.
try contains the application code which is expected to work in normal conditions.
void specifies that a method should not have a return value.
volatile indicates that an attribute is not cached thread-locally, and is always read from the “main memory”.
while continually executes a block of statements until a particular condition evaluates to true
_ (Underscore) added in Java 9, to prevent writing underscores as an unused lambda, method, or catch formal parameter.
Читайте также:  Поддержка php на апачей

The following 16 words can be interpreted as keywords or as other tokens, depending on the context in which they appear.

  • The module and open are used for declaring modules.
  • The exports , opens , provides , requires , to , uses , and with are used for importing and exporting the modules.
  • The transitive is recognized as a terminal in a RequiresModifier.
  • The var is used to infer local variable types.
  • The yield is used to yield a value in a switch statement.
  • The record is used to define new record types.
  • The non-sealed , permits , and sealed are used to define sealed classes and interfaces.

Learning about all keywords on a single page is not possible. We will learn about each Java keyword in its dedicated tutorial.

Источник

Java Language Keywords

Here is a list of keywords in the Java programming language. You cannot use any of the following as identifiers in your programs. The keywords const and goto are reserved, even though they are not currently used. true , false , and null might seem like keywords, but they are actually literals; you cannot use them as identifiers in your programs.

abstract continue for new switch
assert *** default goto * package synchronized
boolean do if private this
break double implements protected throw
byte else import public throws
case enum **** instanceof return transient
catch extends int short try
char final interface static void
class finally long strictfp ** volatile
const * float native super while
* not used
** added in 1.2
*** added in 1.4
**** added in 5.0

Источник

Оцените статью