Important Keywords in Java – Part 1

Keywords play a vital role in every programming language. In standard terms, they are an integral part of a programming language as grammar is to a spoken language. As programmers, we must have a clear understanding of such words and their usage. This article will lend you a helping hand in knowing important keywords in java. Let’s start!!!

Keywords in Java

Here is a list of all important keywords in Java:

S.No Keyword Usage
1 abstract It denotes that a class or method will be implemented later as a subclass.
2 assert It implies that the programmer has assertions or assumptions in the program. If this assertion is encountered to be true, the program runs in a normal mode. Else, the AssertionError will occur at runtime to abort the program.
3 boolean The data type that holds the values true or false.
4 break It breaks out of the loops.
5 byte Data type that holds 8-bit data values.
6 case It denotes individual blocks of text in a switch statement.
7 catch It catches the exceptions thrown by try block.
8 char A data type that holds unsigned 16-bit Unicode characters
9 class It denotes the declaration of a class.
10 continue Sends the control outside the loop block.
11 default It denotes the default code block in switch
12 do Denotes the beginning of a do-while loop
13 double The data type that holds 64-bit floating-point numbers
14 else It includes the alternative block that executes when the ‘if’ block results in false.  
15 enum Denotes enumerated data type.
16 extends Specifies that a class is derived from another class.
17 final Specifies that no overring can take place over the value.
18 finally It consists of a code block in a try-catch structure that will predominantly execute.
19 float Data type that holds 32-bit floating-point value.
20 for Indicates the beginning of a ‘for’ loop.
21 if Indicates the beginning of the ‘if’ loop.
22 implements Indicates that the class implements an interface
23 import Denotes the presence of other packages or classes in the program.
24 instanceof We can check if the object is an instance of another class using this keyword.
25 int Data type that holds 32-bit integer.
26 interface To declare an interface.
27 long Data type that holds 64-bit integer values.
28 native Indicates native code.
29 new Keyword to create a new object.
30 null Denotes null reference.
31 package Declaring a package.
32 private It denotes that the variable or method can be accessed only by the class where it is declared. 
33 protected It denotes that the variable or method can be accessed by the class, its subclass, and other classes under the same package.
34 public It denotes that the variable or method can be accessed throughout the application.
35 return Returns a value to the method when called.
36 short A data type that holds a 16-bit integer.
37 static To denote that a variable or method is a class.
38 strictfp Prevents the precision and rounding of values present in float data type.
39 super Refers to the base class of a class in the method.
40 switch It indicates the switch statement to test a condition and execute based on the test value. 
41 synchronized Used in multithreaded code to denote critical sections and methods.
42 this Refers to the particular object in a method.
43 throw It creates an exception.
44 throws It denotes the exception that the method is likely to throw.
45 transient If any variable or data member is defined as a transient, it cannot be serialized.
46 try It indicates the start of a test block looking for exceptions.
47 void It denotes that the method returns no value.
48 volatile We use this keyword to denote that a variable tends to change asynchronously.
49 while It indicates the start of a while loop.

Let us now see these java keywords in detail with their syntax.

Explanation of java keywords:

In this article, we will explore the syntax and examples for the first 25 Java keywords listed in the above table. Rest we will see in Java Keywords part 2.

1. abstract keyword in java

We use this keyword to declare a class in Java as an abstract class. Once a class is declared as abstract, it is prevented from creating an object. This class handles both abstract and non-abstract methods. To make a class abstract, just add the keyword abstract before the class name.

Here is a snippet to depict it:

abstract class FirstCode{
// coding statements;
}

2. assert keyword in java

We can attain assertion in Java by using the assert keyword. It helps in noticing the assumptions that take place in a Java program. There are a couple of ways to use this keyword with a boolean expression.

The first way lets us add only one boolean expression. The syntax for it is:

assert booleanExpression;

the second way lets us add two boolean expressions.

assert booleanExpression1: booleanExpression2;

3. boolean keyword in java

The boolean keyword returns either true or false as a return value. We can simply add the word boolean in front of a variable to achieve this.

boolean a;
boolean = true;

4. break keyword in java

This keyword is generally used in looping statements to get out of loops. The looping statements where a break can be applied are: while, do-while and for. It also gives the same result when applied with switch statements.

Sample snippet applying break statement:

for(int a=0; a<5; a++)
if(num==3) break;

5. byte keyword in java

Using this keyword, we can declare java data members as byte data types. It holds 8-bit values.

byte b = 20;

6. case keyword in java

This keyword helps us write various statements in programs that use the concept of the switch. Each option is denoted as a case here. Once the condition of a particular case is met, the statement in it gets executed.

Here is the general format for applying case statements in switch.

case 1: 
//statement
case 2:
//statement 
case 3:
//statement
} 

7. catch keyword in java

The catch keyword is mostly paired with another reserved word, ‘try.’ It catches the exceptions that the try block throws. The general format for this statement is:

try{
//statements
}
catch {
//statements
} 

8. class keyword in java

Object-oriented programming languages are incomplete without the presence of a class. This keyword declares a class in Java. It contains numerous variables and functions in it.

Syntax to declare a class:

class class_name

Example snippet to declare a class in java:

class FirstCode{
//variables
//methods
}

9. char keyword in java

This is another keyword to declare a data type. The char keyword holds the value of 16-bit Unicode characters.

char c = “r”;

10. continue keyword in java

This reserved word comes along with looping statements. When you need to make the control reach back to the beginning of the loop, the continue statement does the task. The remaining statements that are given after the continue keyword are put to a halt.

while(i<5){
if (i%2==0) continue;
} 

11. default keyword in java

The switch statements consist of multiple cases in which one care returns true and executes. In situations where no cases match the condition, a default statement that is present would be executed. This statement is denoted by the keyword default.

switch(expression){
case 1: 
//statement
case 2:
//statement 
case 3:
//statement
default:
//statement
} 

12. do keyword in java

The keywords do and while combinedly make the do-while looping structure possible. It iterates each time until the condition is met.

do{
//statements
} while(condition)

13. double keyword in java

To declare a variable as a double data type, we can use this keyword. It holds 64-bit floating-point values.

Double d = 233.17;

14. else keyword in java

The else keyword is often paired with the ‘if’ statement. The else part executes when the if block results in false.

if(condition){
//statements
}
else {
//statements
}

if(age>=18){
System.out.println(“You can vote”);
}
else{
System.out.println(“You cannot vote”);
}

15. enum keyword in java

The keyword enum represents a class that holds a definite set of constant values that cannot be changed. Declaration of enum inside a function is not possible hence it is usually done inside or outside a class.

public enum FirstCode{
Java,
C,
C++,
}

16. extends keyword in java

Inheritance is one of the main features of an Object-oriented programming language. To fetch the properties from a parent class, we use the extends keyword.

The syntax to extend a class is:

class_name Child_class extends Parent_class
class FirstCode{
//code inside parent class
}
class Java_course extends FirstCode{
//code inside child class
}

17. for keyword in java

The for loop statement is declared using the keyword ‘for’. The syntax to create a for loop in Java is:

for(initialization; condition; increment/decrement){
//statements
}

18. final keyword in java

To restrict a value, data member or function from being modified, we use the final keyword. In such circumstances, the values cannot be changed, and the methods with the final keyword cannot be inherited too. These methods are not allowed to be overridden in the child class.

Declaration of final data members:

final int num = 5;
Declaration of final methods:
final FirstCode{
//statements
}
Declaration of final class:
final FirstCodeClass{
//statements
}

19. finally keyword in java

Do not confuse with the Java keywords final and finally. The reserved word finally plays a major role in the Exception Handling concept.

The coding given in the finally block will execute irrespective of the exception being handled or not.

//statements
}
catch{
//statements
}
finally{
//statements
}

20. float keyword in java

This keyword declares a variable as a float data type. It can hold 32-bit floating values. In Java, the letter F or f is written alongside a float variable to denote it as a floating value.

float value = 23.12F;

21. if keyword in java

The if statement in Java is a vital decision-making statement. The if block executes only when the condition results in true.

if(condition){
//statement
}

22. int keyword in java

This int keyword mentions that a variable belongs to the integer data type. It holds a 32-bit integer signed value.

int num = 23;

23. implements keyword in java

This keyword implements an interface. This allows a class to access various methods present in that interface.

class class_name implements interface_name{
//statements
}

24. instanceOf keyword in java

To check if an object belongs to a certain class, interface or subclass, we can make use of the instanceOf keyword.

if(object_name instanceOf class_name)
if(course instanceOf FirstCode)

25. import keyword in java

The import keyword in Java enables us to access various classes and interfaces that are present inside the same source code. We can also use the same keyword to import other packages at the start of the program.

import package com.firstcode.javacourse.TutorialKeywords;

Conclusion:

This table lists almost every keyword that is present in Java. The description helps you in understanding the usage of the keywords in java. You can find a brief explanation for the first 25 keywords given in the table. Also, look into our article “Important Keywords in java – Part 2” to know the description for the rest Java keywords

Leave a Reply

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