Java Java Tutorial Java Methods Java Classes Java File Handling Java Reference



Java Keywords

Java is a popular programming language that is widely used for developing various applications. It is an object-oriented language that is designed to be simple, portable, and secure. Java keywords are the reserved words that have a specific meaning in the Java programming language. These keywords cannot be used as identifiers or variable names in the program. In this article, we will discuss the different Java keywords and their usage in programming.

Brief Explanation of Java Keywords

Java keywords are the predefined words that have a specific meaning in the Java programming language. These keywords cannot be used as variable names or identifiers in the program. Java has 53 keywords that are used for various purposes in programming. These keywords are divided into different categories such as access modifiers, control statements, data types, and others.

Access Modifiers

Access modifiers are used to set the accessibility of the class, method, or variable in the program. Java has four access modifiers that are used for this purpose:

  • public: This keyword is used to make the class, method, or variable accessible from anywhere in the program.
  • private: This keyword is used to make the class, method, or variable accessible only within the same class.
  • protected: This keyword is used to make the class, method, or variable accessible within the same package or subclass.
  • default: This keyword is used to make the class, method, or variable accessible within the same package.

Control Statements

Control statements are used to control the flow of the program. Java has different control statements that are used for this purpose:

  • if: This keyword is used to check a condition and execute a block of code if the condition is true.
  • else: This keyword is used to execute a block of code if the condition in the if statement is false.
  • switch: This keyword is used to select one of the many blocks of code to be executed.
  • while: This keyword is used to execute a block of code repeatedly as long as the condition is true.
  • do: This keyword is used to execute a block of code repeatedly until the condition is true.
  • for: This keyword is used to execute a block of code repeatedly for a fixed number of times.
  • break: This keyword is used to terminate a loop or switch statement.
  • continue: This keyword is used to skip the current iteration of a loop and continue with the next iteration.
  • return: This keyword is used to return a value from a method.

Data Types

Data types are used to define the type of data that can be stored in a variable. Java has different data types that are used for this purpose:

  • int: This keyword is used to define an integer value.
  • double: This keyword is used to define a floating-point value.
  • boolean: This keyword is used to define a boolean value.
  • char: This keyword is used to define a character value.
  • byte: This keyword is used to define a byte value.
  • short: This keyword is used to define a short integer value.
  • long: This keyword is used to define a long integer value.
  • float: This keyword is used to define a floating-point value.

Others

Java has some other keywords that are used for various purposes:

  • class: This keyword is used to define a class.
  • interface: This keyword is used to define an interface.
  • extends: This keyword is used to extend a class or implement an interface.
  • implements: This keyword is used to implement an interface.
  • new: This keyword is used to create an object.
  • this: This keyword is used to refer to the current object.
  • super: This keyword is used to refer to the parent class.
  • static: This keyword is used to define a static method or variable.
  • final: This keyword is used to define a constant value or prevent inheritance or method overriding.
  • abstract: This keyword is used to define an abstract class or method.
  • throws: This keyword is used to declare an exception that can be thrown by a method.
  • try: This keyword is used to enclose a block of code that can throw an exception.
  • catch: This keyword is used to catch an exception thrown by the try block.
  • finally: This keyword is used to execute a block of code after the try or catch block.

Code Examples

Here are some code examples that demonstrate the usage of Java keywords:

Access Modifiers


public class MyClass {
  private int myPrivateVariable;
  protected String myProtectedVariable;
  int myDefaultVariable;
  public void myPublicMethod() {
    // code here
  }
}

Control Statements


int x = 10;
if (x > 5) {
  System.out.println("x is greater than 5");
} else {
  System.out.println("x is less than or equal to 5");
}

for (int i = 0; i < 10; i++) {
  System.out.println(i);
}

while (x > 0) {
  System.out.println(x);
  x--;
}

Data Types


int myInt = 10;
double myDouble = 3.14;
boolean myBoolean = true;
char myChar = 'A';
byte myByte = 127;
short myShort = 32767;
long myLong = 9223372036854775807L;
float myFloat = 3.14f;

Others


public class MyClass extends MyParentClass implements MyInterface {
  public static final int MY_CONSTANT = 10;
  public static void myStaticMethod() {
    // code here
  }
  public void myAbstractMethod();
  public void myMethod() throws MyException {
    try {
      // code here
    } catch (Exception e) {
      // code here
    } finally {
      // code here
    }
  }
}

References

Activity