Back to: Java Tutorials
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:
Java has four access modifiers that are used to set the access levels for classes, variables, and methods:
public: It makes the class, method, or variable accessible to all other classes.
private: It makes the class, method, or variable accessible only within the same class.
protected: It makes the class, method, or variable accessible within the same package or subclasses.
default: It makes the class, method, or variable accessible only within the same package.
Java has several control statements, including:
if: It is used to define a conditional statement.
else: It is used to define an alternate condition for an if statement.
switch: It is used to define a switch statement.
for: It is used to define a for loop.
while: It is used to define a while loop.
do: It is used to define a do-while loop.
break: It is used to exit from a loop.
continue: It is used to skip an iteration in a loop.
return: It is used to return a value from a method.
Java has eight primitive data types, including:
byte: It defines a variable that can store integer values from -128 to 127.
short: It defines a variable that can store integer values from -32,768 to 32,767.
int: It defines a variable that can store integer values from -2^31 to 2^31-1.
long: It defines a variable that can store integer values from -2^63 to 2^63-1.
float: It defines a variable that can store floating-point values.
double: It defines a variable that can store double-precision floating-point values.
boolean: It defines a variable that can store true or false values.
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.
Java has three keywords used to define methods, including:
void: It is used to define a method that does not return any value.
static: It is used to define a class-level variable or method that can be accessed without creating an object of the class.
final: It is used to define a constant variable or a method that cannot be overridden.
Java has five keywords used in exception handling, including:
try: It is used to define a block of code that may generate an exception.
catch: It is used to define a block of code that will handle the exception.
finally: It is used to define a block of code that will be executed regardless of whether an exception occurs or not.
throw: It is used to throw an exception.
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