
The Java consists of a set of reserved words that have a predefined meaning in the language which is known as Java Keywords. In this article, you will learn about the java keywords which is the basic unit that helps in creating a class using the declaration of variables in Java.
Let’s start with the basic idea about keywords, and then we will go through some of the main keywords in detail and then finally we will know the basic difference between throw and throws keywords.
Table of Contents
What is Java Keyword?
Java Keywords implement specific features of the language. These keywords, combined with operators and separators according to a syntax and it forms definition of the Java language. So for the java programmers it is important to know the meaning of the those words (keywords.)
We do not use these reserved words to declare classes, interfaces, variables, methods or any other identifiers. In other words, you cannot use any of the keywords as identifiers in your programs.
Since, Java is case-sensitive, you can use these keywords as identifiers by changing one or more letters in upper case, but it is bad practice and never recommended.
Although const and goto are keywords but they are not in use. And true, false and null are not keywords, they are literals in Java and you cannot use them as identifiers.
List of Java Keywords
List of java keywords or reserved words are as follow:
- abstract : You can use this keyword to make a abstract class and abstract method.
- boolean : Declare a variable of boolean type.
- break : To break out the loop and switch statement.
- byte : Declare a variable of byte type.
- case : We use this keyword to switch statement.
- catch : To catch the exception.
- char : Declare a variable of char type.
- class : Declare a class.
- continue : Skip the current iteration of the loop.
- default : Create a default block in switch statement.
- do : We use the do and while together for do-while loop.
- double : Declare a variable of double type.
- else : For the conditional statement (if … else)
- enum : Defines a fixed set of constant.
- extends : To inherit a class.
- final : You can use this keyword with the variable, method and class.
- finally : Block of try…catch statement which is always executed.
- float : Declare a variable of float type.
- for : The for loop.
- if : Conditional statement.
- implements : To implement an interface.
- import : Import the classes and interfaces.
- instanceof : Compatibility check.
- int : Data type which accepts integer value.
- interface : To declare an interface.
- long : The long data type.
- new : To create an object.
- package : To define the package of the class.
- private : The private modifier.
- protected : The protected modifier.
- public : The public modifier.
- return : By using this keyword you return something.
- short : The short data type.
- static : The static keyword, you can use this keyword with variable, method, class.
- super : To represent the base class.
- switch : For switch statement.
- this : To point the current object.
- throw : Throw the user defined error.
- throws : Pass the exception handling responsibility.
- try : We use for exception handling.
- void : It returns nothing.
- while : The while loop.
Basic interview questions related to Java Keywords.
Apart from above, there are following most basic questions related to Java keywords, which will help you in every interview.
What is a static keyword in Java?
You can use a static keyword with the variable, method, or inner class as a class field. You can access a static method and variable without creating object by using class name only.
What is this keyword in Java?
We use this keyword to refer the current object from constructor and method.
What is final keyword in Java?
We use final keyword with class, method and variable. A final class cannot be inherited, a final method cannot be overridden, and a value final variable can’t be changed.
What is super keyword in Java?
Basically we use super keyword to refer the parent class.
What is transient keyword in Java?
A transient variable is not serialized.
What is the difference between throw and throws keywords in Java?
The throw keyword is used to create a custom error whereas throws keyword is used to pass the responsibility of exception handling to the caller method.