Keywords in Java

Introduction

 
In the Java language, there are 50 reserved words with predefined meanings. These words are known as "keywords". Since these are reserved words and their work is defined we cannot use them as names for variables, methods, classes and so on. 
 

List of Keywords

 

assert

 
The assert keyword is generally used for debugging purposes. If a programmer thinks that a specific statement will always be true then he puts the assert keyword in the code. If the statement is true then it will do the required task but if the statement is false then it will throw an assertion error and the entire program is terminated.
 
Example:
  1. package myclass1;    
  2. public class Myclass1 {    
  3.  public static void main(String[] args) {    
  4.   int argCt = args.length;    
  5.   assert argCt == 5"no of arguments must be 5";    
  6.   System.out.println("OK");    
  7.  }    
  8. }     
Output:
assertion1.jpg

boolean

 
It can have only two values, one is true and the other is false. The default value of a boolean is false.
 
Example:
  1. package myclass1;    
  2. public class Myclass1 {    
  3.  public static void main(String[] args) {    
  4.   boolean a;    
  5.   a = false;    
  6.   System.out.println("a is " + a);    
  7.   a = true;    
  8.   System.out.println("a is " + a);    
  9.   if (a) System.out.println("This is executed.");    
  10.   a = false;    
  11.   if (a) System.out.println("This is not executed.");    
  12.   System.out.println("5>1 is " + (5 > 1));    
  13.  }    
  14. }     
Output:
boolean1.jpg

break

 
When we use a break statement in our code then it will forward the program execution to the statement immediately following the current statement or block. Generally it is used for terminating the loop.
 
Example:
  1. package myclass1;    
  2. public class Myclass1 {    
  3.  public static void main(String[] args) {    
  4.   for (int n = 0; n < 10; n++) {    
  5.    System.out.println("n is:" + n);    
  6.    if (n >= 5)    
  7.     break;    
  8.   }    
  9.   System.out.println("Out side");    
  10.  }    
  11. }     
Output:
break1.jpg

byte

 
When we declare a variable as byte then it can hold an 8-bit integer number. Its value ranges from -128 to +127. This is also used to declare that a method is of primitive type byte return type.
 
Example:
  1. package myclass1;    
  2. public class Myclass1    
  3. {    
  4.     public static void main(String[] args)    
  5.     {    
  6.         byte moon = -10;    
  7.         System.out.print("moon: ");    
  8.         System.out.print(moon);    
  9.     }    
  10. }     
Output:
byte1.jpg

char

 
It is used as a data type. It can store a 16-bit Unicode character. This is also used to declare a method as having a char return type.
 
Example:
  1. package myclass1;    
  2. public class Myclass1 {    
  3.  public static void main(String[] args) {    
  4.   char oneChar1 = 'B';    
  5.   char oneChar2 = 66;    
  6.   System.out.println(oneChar1);    
  7.   System.out.println(oneChar2);    
  8.  }    
  9. }     
Output:
char.jpg

do

 
The do keyword in Java starts a do-while loop. The do-while loop is similar to the while loop, except that in a do-while loop the condition is determined after the statements execute.
 
Example: 
  1. package myclass1;    
  2. public class Myclass1 {    
  3.  public static void main(String[] args) {    
  4.   int y = 20;    
  5.   do {    
  6.    System.out.print("value of y : " + y);    
  7.    y++;    
  8.    System.out.print("\n");    
  9.   }    
  10.   while (y < 30);    
  11.  }    
  12. }     
Output:
do.jpg
 

synchronized

 
The synchronized keyword is very important in Java. It controls the access of multiple threads from any single shared resource. It is very useful in preventing thread interference and consistency problems.
 
Example:
  1. public class Myclass1 {    
  2.  private int count = 0;    
  3.  public void increment() {    
  4.   synchronized(this) {    
  5.    count++;    
  6.   }    
  7.  }    
  8.  public int getCount() {    
  9.   synchronized(this) {    
  10.    return count;    
  11.   }    
  12.  }    
  13. }      

int

 
Used in Java for defining 32-bit signed integer values. It is also used to declare a variable of int type, an expression.
 
Syntax:
 
int <variable-name>  =  <integer-name>
 
Example:
 
int i=10;
 

package

 
It is declared with the package keyword. Classes are organized in categories in packages.
 
Examples:
 
java.lang
java.util
java.io
java.math
java.sql
 

return

 
A method can return a value or does not return a value. The Return keyword specifies that the method will return something. The method returns something when its execution is complete.
 
Example:
 
A method returning an int value:
  1. public int New_Method()  
  2. {  
  3. int i = 2;  
  4. return(i);  
  5. }  

new

 
For creating objects of classes on the heap in Java we use the new keyword.
 
Syntax:
 
MyObject object = new Myobject();
 

Summary

 
In this article, we have explained many keywords used in Java. These keywords are very useful and used frequently. We have also provided some examples related to these keywords.