Java Keywords – List of 51 Keywords with Examples

Java keywords are reserved words, that have a special meaning associated with them. For easy identification, they are usually highlighted in Java. Out of 50 keywords, 48 are in use while 2 are not.

Let us study some of the important Java Keywords in detail.

List of Keywords used in Java

List of Java Keywords

  • abstract: It is used to achieve abstraction in Java. It is a non-access to modifier relevant for classes and methods.
  • enum: Enum contains a fixed set of constants.
  • instanceof: instanceof is used to check whether the object is an instance of the class, subclass or interface.
  • private: It is an access modifier. If any class or method is declared private then it cannot be accessible by its class.
  • protected: It is mainly used in the concept of inheritance to only get accessed by the subclasses.
  • public: Anything inside public is publically accessible by any class or variable.

Access Modifiers –

  • static: Static can be used to create a (block, method, variable, nested class) which is used itself, without reference to an instance.
  • strictfp: As the name says strictfp it is used for restricting floating-point calculations. Using strictfp it ensures the same result on every platform while performing operations using the floating-point variable.
  • synchronized: It is applicable to blocks and methods. It is used for synchronization. 
  • transient: It is a variable modifier used for serialization. During serialization, if we do not want to save the value of a variable in a file, then we use transient.
  • volatile: It tells the compiler that the variable modified by volatile can be changed unexpectedly by other parts of your program. 

Do you know how can we use Java Static Keywords?

Here, is the list of all keywords used in Java-

Java keywords list

enum and strictfp Keywords in Java

1. enum

Enum is a special type of data type which contains a fixed set of constants. An enumeration defines a class type. Enumeration has constructors, methods and instance variables. Enumeration is declared the same as the primitives.

Master the concept of Constructor in Java in a single shot!

1.1 Declaration of enum in Java

  • enums cannot be declared inside a method but declare inside or outside a class.

Syntax:

public enum Days
{
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY;
}

Example-

package com.dataflair.keyword;

enum Day
{
  SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
  THURSDAY, FRIDAY, SATURDAY;
}
public class EnumExample {
  Day day;
  public EnumExample(Day day)
  {
    this.day = day;
  }
  public void dayIsLike()
  {
    switch (day)
    {
    case MONDAY:
      System.out.println("Mondays are bad.");
      break;
    case FRIDAY:
      System.out.println("Fridays are better.");
      break;
    case SATURDAY:
    case SUNDAY:
      System.out.println("Weekends are best.");
      break;
    default:
      System.out.println("Midweek days are so-so.");
      break;
    }
  }
  public static void main(String[] args)
  {
    String str = "SUNDAY";
    EnumExample t1 = new EnumExample(Day.valueOf(str));
    t1.dayIsLike();
  }
}

Output-Enum-exampleConstants in the enum are always public static and final. We cannot have child enums as we use finalWe can declare the main method inside the enum and hence, we can use a command prompt to call it.

Let’s study in detail the implementation of the Final Keyword in Java.

1.2 Enum and Inheritance

  • All enums extend java.lang.Enum class. As a class extends one parent in Java, so an enum can’t extend anything else.
  • toString() method is overridden in java.lang.Enum class, which returns enum like a name.
  • enum can execute various interfaces.

1.3 values(), ordinal() and valueOf() methods

  • These methods are available inside java.lang.Enum.
  • values() method in Java is used to restore all qualities exhibited inside enum.
  • An order is essential in enums. By using the ordinal() method, each enum file can discover, same as cluster file.
  • valueOf() method returns the enum constant of the predefined string values if exists.

2. strictfp

strictfp is a keyword in java used for restricting floating-point values and ensuring the same output on each stage while performing operations on the floating-point variable.

The calculations of the floating-point are platform-dependent there are different outputs using different processors. When any file runs on 16 bit or 32-bit or 64-bit processor then there are chances that the output may differ. To solve this issue strictfp was introduced in JDK 1.2 for the calculations of floating-point.

Important points:

  • If we declare a class or interface using strictfp modifier, then all the methods declared inside that class, and the nested types are implicitly strictfp.
  • We cannot use strictfp with abstract methods. Though we can use it with abstract classes in Java.
  • All the methods of the interface are implicitly abstract so we cant use strictfp with any of them.

Example-

package com.dataflair.keyword;

public class StrictFPExample {

  public strictfp double sum()
  {
    double operand1 = 12e+05;
    double operand2 = 9e+10;
    return (operand1+operand2);
  }
  public static strictfp void main(String[] args)
  {
    StrictFPExample sumObject = new StrictFPExample();
    System.out.println(sumObject.sum());
  }
}

Output-strictfp-example

Summary

All the Java Keywords are predefined for a compiler, we can’t use them as an identifier. This is just the beginning of keywords. In our next tutorial –  Keywords in Java (part – 2) we will learn more about them.

Hope, you liked the explanation. Please share your experience with us!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.