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:
- package myclass1;
- public class Myclass1 {
- public static void main(String[] args) {
- int argCt = args.length;
- assert argCt == 5: "no of arguments must be 5";
- System.out.println("OK");
- }
- }
Output:
boolean
It can have only two values, one is true and the other is false. The default value of a boolean is false.
Example:
- package myclass1;
- public class Myclass1 {
- public static void main(String[] args) {
- boolean a;
- a = false;
- System.out.println("a is " + a);
- a = true;
- System.out.println("a is " + a);
- if (a) System.out.println("This is executed.");
- a = false;
- if (a) System.out.println("This is not executed.");
- System.out.println("5>1 is " + (5 > 1));
- }
- }
Output:
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:
- package myclass1;
- public class Myclass1 {
- public static void main(String[] args) {
- for (int n = 0; n < 10; n++) {
- System.out.println("n is:" + n);
- if (n >= 5)
- break;
- }
- System.out.println("Out side");
- }
- }
Output:
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:
- package myclass1;
- public class Myclass1
- {
- public static void main(String[] args)
- {
- byte moon = -10;
- System.out.print("moon: ");
- System.out.print(moon);
- }
- }
Output:
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:
- package myclass1;
- public class Myclass1 {
- public static void main(String[] args) {
- char oneChar1 = 'B';
- char oneChar2 = 66;
- System.out.println(oneChar1);
- System.out.println(oneChar2);
- }
- }
Output:
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:
- package myclass1;
- public class Myclass1 {
- public static void main(String[] args) {
- int y = 20;
- do {
- System.out.print("value of y : " + y);
- y++;
- System.out.print("\n");
- }
- while (y < 30);
- }
- }
Output:
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:
- public class Myclass1 {
- private int count = 0;
- public void increment() {
- synchronized(this) {
- count++;
- }
- }
- public int getCount() {
- synchronized(this) {
- return count;
- }
- }
- }
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:
- public int New_Method()
- {
- int i = 2;
- return(i);
- }
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.