Java Keywords – List of 51 Keywords with Examples

Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java

In a programmer’s life, knowing all the keywords are very essential to build a program properly. In this article, we will discuss all the keywords that JDK has in reserve.

What are Keywords in Java?

Keywords are special tokens that add a different meaning to the language compiler. Each keyword has its own individual function and performs a specific task assigned to it. In Java, we have 50 such reserved words, out of which 48 are in use, and 2 are reserved but not in use. Keywords cannot be used as identifiers in a program, so a programmer must know all the keywords to avoid any kind of syntax or logical errors.

Let us study all the keywords that Java provides us with in detail.

1. abstract: Using the abstract keyword in Java, we can create abstract classes and methods. Abstract keywords are essential to implement abstraction into a program.

Example of abstract in Java:

abstract class DataFlair

2. assert: Using the assert keyword, we can implement assertion in a program. Using it we can check the correctness of any assumptions made in a program. The assert keyword was added in JDK 1.4.

Example of assert in Java:

int i = 10;
           assert i > 5 : "The value is greater than 5";

3. boolean: Using the boolean keyword, we can declare a boolean variable. A Boolean variable is a variable that has two values, true and false.

Example of boolean in Java:

boolean flag = true;

4. break: The break keyword is a jump statement using which we can use to break out of a loop or switch statement. The break statement terminates the currently executing block of code.

Example of break in Java:

i=0;
while(true)
{
     if(i==10)
     break;
     i++;
}

5. byte: Using the byte keyword in Java, we can declare a variable that can hold a value of 1 byte or 8 bit.

Example of byte in Java:

byte num=1;

6. case: Using the case statement, we can declare each case inside a switch-case block. case keyword is used to give multiple conditions to switch statements. This is used in a switch case statement. It contains the statements, which are executed when the value in the case matches a condition.

Example of case in Java:

{
     case 1:
               System.out.println(“ONE”);
               break;
}

7. catch: The catch keyword is part of exception handling in Java. Using the catch keyword, we can catch or capture the error caught by the try block. A catch block can exist only after the try block.

Example of catch in Java:

catch (Exception e)
{
       System.out.println(“An exception was caught by the try block”);
}

8. char: Using the char keyword, we can declare a character variable.

Example of char in Java:

char ch=’A’;

9. class: The class keyword in Java is used to declare Java classes. It usually contains the methods and variables, which are the attributes of the object.

Example of class in Java:

class DataFlair

10. continue: Continue is also a jump statement in Java. Using it, we can terminate the current iteration and continue from the next iteration inside the loop.

Example of continue in Java:

i=0;
while(i<=10)
{
     if(i==5)
     continue;
     i++;
}

11. default: default is the keyword using which we can declare the default statement inside a switch case. It is executed when none of the cases match.

Example of default in Java:

Switch(num)
{
     case 1:
               System.out.println(“ONE”);
               break;
     default:
               System.out.println(“Not ONE”);
}

12. do: Using the do keyword, we can declare a do-while loop. It is an exit-controlled loop, so it doesn’t have any entry condition.

Example of do in Java:

i=0;
do{
        i++;
     }while(i<=10);

13. double: Using the double keyword, we can declare a double variable. A double variable can hold a 64bit long floating point number.

Example of double in Java:

double d= 1.23456;

14. else: Using the else keyword, we can write statements that will be executed when the if block doesn’t execute successfully.

Example of else in Java:

i=5;
if(i!=5)
System.out.println(“Not Five”);
else
System.out.println(“FIVE”);

15. enum: Using the enum keyword, we can declare an enumerated class. It consists of a fixed set of constants. Enum constructors are always private by default. The enum keyword was added to Java in JDK 5.0.

Example of enum in Java:

enum Rainbow
{
  Violet, Indigo, Blue, Green,
  Yellow, Orange, Red;
}

16. extends: The extends keyword is used in inheritance. Using it we can inherit one class into another.

Example of extends in Java:

class subclass extends superclass

17. final: Using the final keyword, we can finalize the value of a variable. It means that the value cannot be updated by the user whatsoever.

Example of final in Java:

final int num = 1000; 

18. finally: The finally keyword is used after the try-catch block. It indicates the end of the block. Unlike the try-catch block, the finally block will be executed regardless of whether an exception is found or not.

Example of finally in Java:

finally{
System.out.println("This block is always executed whatsoever!");
} 

19. float: Using the float keyword, we can declare a float variable in Java. Thus, the float variable holds a 32-bit long floating-point number.

Example of float in Java:

float num= 1.324f;

20. for: Using the for keyword, we can declare a for loop. It is an entry-controlled loop; for this kind of loop, the number of iterations should be known beforehand.

Example of for in Java:

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

21. if: In Java, the if keyword is used to declare an if conditional statement. Therefore, the if statement is used to check the viability of a condition.

Example of if in Java:

i=5;
if(i==5)
{
    System.out.println(“The Number is Five”);
}

22. implements: Using the implements keyword in Java, we can declare an interface. However, it is like the inheritance extends keyword; here, to access an interface, we have to use the implements keyword.

Example of implements in Java:

interface DataFlair{
public void intern();
}
class internship implements DataFlair
{
        Public void intern()
       {
                System.out.println(“Internship Granted”);
        }
}

23. import: Using the import keyword in Java, we can access classes and interfaces present inside a package in the current code.

Example of import in Java:

import java.util.Arrays; 

24. instanceof: Using the instanceof keyword in Java, we can check whether a given object is an instance of another class or not. Hence It returns true if the given object is an instance and false if not.

Example of instanceof in Java:

public class DataFlair {
  public static void main(String[] args) {
    DataFlair Obj1 = new DataFlair();
    System.out.println(Obj1 instanceof DataFlair);// It will return True.
  }
}

25. int: Using the int keyword, we can declare a variable of integer datatype. Therefore, it can hold a value of 32-bit long.

Example of int in Java:

int num=100;

26. interface: Using the interface keyword in Java, we can declare an interface inside the code.

Example of interface in Java:

interface DataFlair{}

27. long: Using the long keyword, we can declare variables with the long data type. Therefore, the long data type is an integer that can hold 64 bits of data.

Example of long in Java:

long num=100000;

28. native: The native keyword in Java is used to indicate that a method is implemented in native code using JNI[Java Native Interface].

Example of native in Java:

Public native void DataFlair();

29. new: Using the new keyword, we can create a new instance of a class.

Example of new in Java:

public class DataFlair {
  public static void main(String[] args) {
    DataFlair Obj1 = new DataFlair();
}

30. Package: Using the package keyword in Java, we can create a new package in the Java library.

Example of Package in Java:

package com.DataFlair.Keywords;

31. private: The private keyword is an access modifier that declares a class or method as private; i.e, it can only be accessed by its local variables.

Example of private in Java:

private class DataFlair{}

32. protected: protected is also an access modifier that declares a class or method as protected; i.e, it can be accessed by files in the PWD(Present Working Directory).

Example of protected in Java:

protected class DataFlair{}

33. public: public is another access modifier that declares a class or method as public; i,e; it can be accessed globally.

Example of public in Java:

public class DataFlair{}

34. return: The return keyword is used inside a method when it is required to return a value of a certain return type, except void.

Example of return in Java:

int sum(int a, int b)
{
     return (a+b);
}
 

35. short: In Java, the short keyword declares variables that can hold an integer value of 16 bits.

Example of short in Java:

short num=1;

36. static: In Java, the static keyword declares a static variable or method. Hence, A static variable or method is stored in a static memory location.

Example of static in Java:

public static void main() 

37. strictfp: In Java, the strictfp keyword ensures that every platform gets the same result for floating-point operations. The strictfp keyword is used on classes, methods, and interfaces. However, this keyword was introduced in JDK 1.2.

Example of strictfp in Java:

strictfp class DataFlair{}

38. super: The super keyword distinguishes variables with the same name in both the parent class and child class in inheritance.

Example of super in Java:

class DataFlair
{
     String name=”Company”;
}
class internship extends DataFlair/
{
     String name=”Intern”;
     System.out.println(name);//Intern will be printed
     System.out.println(super.name);//Company will be printed
}

39. switch: Using the switch keyword, we can initiate the switch case block in Java code.

Example of switch in Java:

Switch(num)
{
     case 1:
               System.out.println(“ONE”);
               break;
     default:
               System.out.println(“Not ONE”);
}

40. synchronized: Using the synchronized keyword in Java, we can specify the critical section to be executed during multithreaded coding.

Example of synchronized keyword in Java:

synchronized void print(int num)

41. this: this keyword in Java is used to distinguish local variables from global variables.

Example of this in Java:

class DataFlair{
String name;
DataFlair(String name)
{
     this.name=name;
}
}

42. throw: The throw keyword in Java is used to throw custom-made exceptions explicitly into the code.

Example of void in Java:

throw new IOException("The Input is Not Acceptable”); 
 

43. throws: The throws keyword is used to propagate a checked exception.

Example of throws in Java:

import java.io.*;  
class DataFlair{  
 void internship()throws IOException{  
  throw new IOException("Internship Not Found");  
 }  
}  

44. transient: Using the transient keyword in Java, we can deserialize a variable. Therefore, when a variable is declared transient, it is not considered for serialization.

Example of a transient in Java:

transient int num;

45. try: The try block is used in exception handling to test a block of code for exceptions. Therefore, the try block is always followed by a catch block or finally block.

Example of try in Java:

 try{
//statement
}catch(exception e){}

46. void: Using the void keyword, we can declare that the method will not return any value.

Example of void in Java:

void main()

47. volatile: The volatile keyword in Java is used to indicate that a variable can change asynchronously.

Example of volatile in Java:

static volatile int num=100;

48. while: Using the While keyword, we can declare a while loop. A while loop is an entry-controlled loop, where the number of iterations is not fixed.

Example of while in Java:

i=5;
while(i<=10)
{
    i++;
}

Note: Two other keywords are reserved but not used: const and goto. Words like true, false, and null might seem like keywords, but they are actually literals.

Learning and Remembering Keywords in Java

While memorising all Java keywords can seem daunting at first, there are effective strategies to aid your learning process. Here are some tips to consider:

  • Group keywords by functionality: Categorise keywords based on their purpose (e.g., control flow, object-oriented programming, exception handling). Therefore, this mental organization can strengthen your understanding and recall.
  • Practice writing simple code snippets: Create small programs that utilize various keywords. Hence, experimenting with code reinforces your knowledge and clarifies how keywords interact within the broader Java syntax.
  • Utilize online resources: Take advantage of interactive quizzes, flashcards, and mnemonic devices available online. Therefore, these tools can make learning keywords more engaging and interactive.

Conclusion

In this article, we took a brief look at all the keywords that are present in Java. However, each of these keywords has a vast role in coding. Knowing each keyword in detail is essential for every programmer.

We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google

courses

DataFlair Team

DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.

Leave a Reply

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