In this tutorial, we will Explore the Java Keywords List and learn about some Important Reserved Words, their Meaning along with Examples:
Keywords in Java are the reserved words that act as a key to the code. As these words are predefined, they cannot be used for any other purpose like variable name or object name or any other identifier. Java has around 51 reserved words or keywords.
In this tutorial, we will discuss the list of keywords in Java. Then we will take up some of the important keywords in Java and see their meaning along with the programming examples.
=> Check Here To See A-Z Of Java Training Tutorials.
What You Will Learn:
Java Keyword List
Given below is a list of all the keywords in Java. In this list, we have also included the keywords that are no longer used in Java.
Keyword | Description |
---|---|
abstract | Indicates the class or method that follows this keyword is abstract and that will have to be implemented by a subclass. |
assert | Assert keyword helps the programmer to declare assertions or assumptions in a program. If an assertion is true, program progresses normally otherwise the AssertionError is thrown at runtime and program aborts. |
boolean | Defines two values, true or false. |
break | Used to break out of loops or iterative constructs. |
byte | Data type capable of holding 8-bit data. |
case | Marks blocks of text (cases) in a Switch statement. |
catch | Used to catch exceptions generated in the try block. |
char | Data type able to hold unsigned 16-bit Unicode characters. |
class | Used to declare a new class. |
continue | It helps to take control outside the loop and continue to the next iteration. |
default | Defines the “block of code” that will execute by default in a Switch statement. |
do | Starting keyword for “do-while” loop. |
double | Data type holding 64-bit numbers (floating-point). |
else | Defines else part in the ‘if’ statements. |
enum | Used to declare enumerations in Java. |
extends | Indicates inheritance. A class is derived or inherited from another class. |
final | Defines a variable which will hold constant values or a method that cannot be overridden. |
finally | Defines the finally block that executes after the try-catch block irrespective of whether the exception was caught or not. |
float | Data type able to hold 32-bit floating-point values. |
for | Indicates the start of a ‘for’ loop. |
if | Start of ‘if’ statement. |
implements | Indicates that a class implements an interface. |
import | Used to include or reference other packages/classes into the program. |
instanceof | Used to check if the given object is an instance of another class. |
int | Data type to hold a 32-bit integer value. |
interface | Used for declaring an interface. |
long | Data type holding 64-bit integer values. |
native | Used to indicate native code (platform-specific). |
new | Operator to create a new object. |
null | Indicates null reference. |
package | Keyword to declare Java package. |
private | Indicates private access specified which means a variable or method can be accessed only by the class in which it is declared. |
protected | This keyword indicates a protected access specifier. When a variable or method is protected then that variable or method can be accessed only by the class they are declared in, its subclass, and other classes in the same package. |
public | The public keyword is used to indicate public access specifier. A variable, method, classes, interfaces declared as public can be accessed throughput the application. |
return | Return is used to send back the value of a method to the calling method. It also is used to return the control to the calling method. |
short | Data type holding 16-bit integer number values. |
static | The static keyword indicates the method or a variable is static and cannot be instantiated. |
strictfp | The keyword strictfp restricts the rounding and precision of floating point values calculation. It ensures portability. |
super | Indicates base or superclass of the class. |
switch | Indicates a Switch statement that tests a condition and executes multiple cases depending on the test value. |
synchronized | Indicates synchronized sections for multithreaded code like critical section. |
this | The keyword ‘this’ indicates the current object. |
throw | Throws an exception. |
throws | This indicates the exception that can be thrown by a method. |
transient | Specifies transient variable that is not part of the persistent state of an object. |
try | Try keywords start a block that contains code that might raise exceptions. |
void | Indicates no return value. |
volatile | Used to define variables that are not stored in Main Memory. They can be changed asynchronously. |
while | Keyword while starts a while loop. |
const | The ‘const’ keyword is no more supported in Java |
goto | The ‘goto’ keyword is no more supported in Java |
true, false and null | The words “true, false, null” are literals. Still, we cannot use them as identifiers in the program. |
We will discuss the below keywords in a separate tutorial as they have a great significance as far as Java programming is concerned.
These words are:
#1) “this” keyword
The keyword “this” points to the current object in the program.
Also Read => Java ‘THIS’ Keyword With Code Examples
#2) “static” keyword
A static keyword is a keyword that is used to indicate an object that cannot be instantiated. So if we have a static method, then it need not be called using an object. It can be called just using a class name.
Similarly, if we have a static variable, then its value is preserved throughout the program.
#3) “super” keyword
The “super” keyword in Java is used to refer to the objects of the immediate parent class. The parent class is also referred to as “Superclass”. We will explore the details of the super keyword while discussing inheritance in our OOPS tutorial series.
#4) “final” keyword
The keyword “final” is used with variables, methods, or classes. A final variable is a constant variable. A final method is a method that cannot be overridden. A final class is a class that cannot be inherited or extended. We will discuss the final one in detail in our OOPS tutorial series.
Now let’s implement a program wherein we will use these important keywords in Java.
import java.util.*; class MyClass { int i; MyClass() { System.out.println("MyClass:: Default Constructor"); } MyClass(int j) { this(); //calling statement to First Constructor System.out.println("MyClass:: Parameterized Constructor"); } //static method static void methodOne() { System.out.println("MyClass:: static methodOne"); } //final method final void methodTwo() { System.out.println("MyClass:: Final methodTwo"); System.out.println("MyClass::Calling methodOne from methodTwo"); //Accessing same class field this.methodOne(); //Accessing same class method } //regular method void methodThree() { System.out.println("MyClass::MethodThree"); //Accessing same class field System.out.println("MyClass::Calling methodTwo from methodThree"); this.methodTwo(); //Accessing same class method } } class MyDerivedClass extends MyClass{ void methodThree(){ System.out.println("MyDerivedClass::methodThree"); System.out.println("MyDerivedClass::Calling methodThree from MyClass"); super.methodThree(); //calling regular method super.methodTwo(); //calling final method super.methodOne(); //calling static method } //void methodOne(){} //overriding final method gives compiler error //void methodTwo(){} //overriding final method gives compiler error } public class Main{ public static void main(String[] args){ MyClass.methodOne(); //call static method from MyClass MyDerivedClass d1 = new MyDerivedClass (); d1.methodOne(); //call static method from MyDerivedClass d1.methodTwo(); //call final method from MyDerivedClass d1.methodThree(); } }
As shown in the above program, the first keyword we have used is import followed by many other keywords like class, int, etc. The main keywords in this program are this, static, final, and super.
We have used this keyword in the second constructor to call the first constructor. The parent class MyClass has a static method and a final method declared in it.
In the derived class – MyDerivedClass, we override a regular method methodThree. Note that we also try to override methodOne and methodTwo but the compiler gives an error as they are static and final methods respectively. Note the commented code.
In the main class, we first call the static class using MyClass and then create a derived class object. Note that no error is given even while calling static and final methods using the derived class objects.
Output
Note the colored output. This entire output is the result of calling methods using the derived class objects.
Frequently Asked Questions
Q #1) What is the use of Keywords in Java?
Answer: Keywords are also called as Reserved words. These are the words that the programming language uses for internal processing and pre-defined actions.
Thus programming language doesn’t allow these keywords to be used by the programmer as identifiers or variable names etc. Despite that, if we use these keywords it will result in a compiler error.
Q #2) How many Keywords are present in Java?
Answer: Java has a total of 51 keywords that have predefined meaning and are reserved for use by Java. Out of these 51 keywords, 49 keywords are currently used while the remaining 2 are no more used.
Q #3) What is the difference between integer and int?
Answer: Both int and Integer store integer values. But ‘int’ is a keyword that is a primitive data type int. An integer is a class type. Integer converts int into an object and vice versa.
Q #4) Is null a keyword in Java?
Answer: No. Null is not a keyword in Java but it is literal. Yet, we cannot use it for declaring identifiers.
Q #5) Is new a keyword in Java?
Answer: Yes, new is a keyword in Java. The new keyword is used to create a new object and allocate memory to this new object on the heap. Apart from the class objects, we also use the new keyword to create array variables and allocate memory.
Conclusion
In this tutorial, we have discussed various keywords used in Java. Java supports a total of 51 keywords out of which 49 keywords are currently used and 2 are not currently used. Of these keywords, four keywords i.e. this, static, super, and final are important keywords that need special attention.
‘This’ keyword points to the current object. The static keyword is used to indicate the instantiation that is not needed. The super keyword is used to refer to the parent class and the final keyword is used to indicate the constant or non-inheritance.
=> Visit Here To See The Java Training Series For All.