Java Keywords

Java Keywords are an essential part of the Java programming language, and they are reserved words that have specific meanings and purposes in the language.

Java Keywords are predefined and cannot be used as identifiers or variable names in the code. These words have predefined functionality and are used to define the structure and flow of the code. Some of the commonly used Java keywords are:

Access Modifiers:

Java has four access modifiers that are used to set the access levels for classes, variables, and methods:

a. public: It makes the class, method, or variable accessible to all other classes.

b. private: It makes the class, method, or variable accessible only within the same class.

c. protected: It makes the class, method, or variable accessible within the same package or subclasses.

d. default: It makes the class, method, or variable accessible only within the same package.

Control Statements:

Java has several control statements, including:

a. if: It is used to define a conditional statement.

b. else: It is used to define an alternate condition for an if statement.

c. switch: It is used to define a switch statement.

d. for: It is used to define a for loop.

e. while: It is used to define a while loop.

f. do: It is used to define a do-while loop.

g. break: It is used to exit from a loop.

h. continue: It is used to skip an iteration in a loop.

i. return: It is used to return a value from a method.

Data Types:

Java has eight primitive data types, including:

a. byte: It defines a variable that can store integer values from -128 to 127.

b. short: It defines a variable that can store integer values from -32,768 to 32,767.

c. int: It defines a variable that can store integer values from -2^31 to 2^31-1.

d. long: It defines a variable that can store integer values from -2^63 to 2^63-1.

e. float: It defines a variable that can store floating-point values.

f. double: It defines a variable that can store double-precision floating-point values.

g. boolean: It defines a variable that can store true or false values.

h. char: It defines a variable that can store a single character.

Class Declaration: The keyword class is used to define a class in Java.

Object Creation: The keyword new is used to create an instance of a class.

Method Declaration:

Java has three keywords used to define methods, including:

a. void: It is used to define a method that does not return any value.

b. static: It is used to define a class-level variable or method that can be accessed without creating an object of the class.

c. final: It is used to define a constant variable or a method that cannot be overridden.

Exception Handling:

Java has five keywords used in exception handling, including:

a. try: It is used to define a block of code that may generate an exception.

b. catch: It is used to define a block of code that will handle the exception.

c. finally: It is used to define a block of code that will be executed regardless of whether an exception occurs or not.

d. throw: It is used to throw an exception.

e. throws: It is used to declare the exceptions that may be thrown by a method.

Miscellaneous: 10 miscellaneous keywords in Java and their uses are:

  • abstract: This keyword is used to declare an abstract class or method. An abstract class cannot be instantiated and can only be subclassed. An abstract method does not have a body and must be implemented by the subclass.
  • assert: This keyword is used to perform assertions in Java. Assertions are used to test assumptions about the code during development and can be enabled or disabled at runtime.
  • enum: This keyword is used to define an enumeration in Java. An enumeration is a fixed set of constants that can be used in a program.
  • extends: This keyword is used to create a subclass of a class or to extend an interface.
  • final: This keyword is used to create a constant value or to prevent a class, method, or variable from being modified.
  • implements: This keyword is used to implement an interface in a class.
  • interface: This keyword is used to define an interface in Java. An interface is a collection of abstract methods that define a set of functionalities that a class must implement.
  • native: This keyword is used to declare a method that is implemented in a platform-specific language, such as C or C++.
  • strictfp: This keyword is used to ensure consistent floating-point behavior across different platforms.
  • synchronized: This keyword is used to ensure that only one thread can access a block of code at a time. This is used to prevent race conditions and ensure thread safety.

It is essential to use Java Keywords correctly and not to use them as variable names, as it can result in errors and code malfunctions. Moreover, using keywords inappropriately can make code hard to read and understand. We will see details of these keywords in the following tutorials and appropriate chapters.

Also, see the example code JavaExamples_NoteArena in our GitHub repository. See complete examples in our GitHub repositories.

Follow us on social media
Follow Author