Keywords in Java
Keywords in Java
Reserved Words
In any programming language, there are some fundamental concepts that
you need to know before you can write even the most elementary programs,
such as the case with Java programming language as well.
This article introduces you to one of the Java fundamentals where you will
learn the Java keywords. This is one of the basic units which helps to form a
class in Java.
Let us start our discussion with what is keywords in Java and then we will
discuss some main keywords in Java that we frequently use in our Java
programs.
These are reserved for special purposes and we cannot use them as
identifier names otherwise the compiler will give an error.
In Java. there are 51 keywords, but 2 of them are not used which are-goto
and const. Only 49 keywords are used in Java. All of them have different
purposes and meanings.
Below is the Java Keyword List:
Let’s discuss each of them briefly:
1. abstract keyword
The abstract keyword in Java is used when we declare a class as an abstract
class. Making a class as an abstract class restricts itself from creating its
object.
boolean myVar;
myVar = false;
The assert keyword is used with boolean expressions in two different ways:
assert booleanExpression;
4. break keyword
The break keyword in Java is used to break from a looping statement of
Java. We can use the break keyword as a statement. We write in a single line
with a semicolon.
When we use it with either for, while or do-while loop, the control flow
reaches to the end of the respective loop. We also use it in the switch
statement.
For example:
5. byte keyword
The byte keyword in Java is used to define a variable with a byte data type.
The byte data type can hold 8-bit values.
6. case keyword
The case keyword in Java is used in switch statements to write different
options. We write each option with the case statement and the condition is
checked around each case of the switch statement.
switch (expression) {
case option1:
//statement
case option2:
//statement
case option3:
//statement
case option4:
//statement
case option5:
//statement
}
7. catch keyword
The catch keyword in Java is used together with the try statements. It is
used to catch the exceptions generated by the try block.
The catch block cannot be used alone; it has to be used only after the catch
block.
try {
//statements
}
catch {
//statements
}
8. class keyword
The class keyword in Java is used to declare a class in Java. A class is a
collection of methods and data members.
class MyClass {
//fields
//member functions
}
9. char keyword
The char keyword in Java is used to declare a variable of a char data type.
The char data type can be used to hold a value of 16-bit Unicode characters.
When no condition matches any case statement, then the default case is
executed.
switch (expression) {
case option1:
//statement
case option2:
//statement
case option3:
//statement
default:
//statement
}
12. do keyword
The do keyword in Java is used in control flow statements with the while
statement. They are collectively known as- do-while loop.
do {
//statements
} while ( test - expression )
if (condition) {
Statements
if condition is true
}
else {
Statement
if condition is false
}
Enums in Java represents a class that contains a fixed set of constant values
that can’t be changed. We cannot declare an enum inside a method but we
can declare it inside or outside class.
If a class wants to inherit the properties from another class, it can inherit it
using the extends keyword.
The Child class is written before the extends keyword and the parent class is
written after the extends keyword. We use this keyword to achieve one of
the OOPs concept i.e. Inheritance in Java.
class ParentClass {
//code inside the parent class
}
class ChildClass extends ParentClass {
//code inside the child class’
}
If we use the final keyword with a class, then no other class can inherit the
final class. When you declare a method as final then the method can not be
overridden in the child class.
The statements inside the finally block always executes no matter if the
exception is handled or not. The finally statement always comes after the
try-catch block.
try {
//statements
}
catch {
//statements
}
finally {
//statements
}
21. if keyword
The if keyword in Java is used as a decision-making statement.
We put the test condition inside the if statement and if the condition
evaluates to true then the statements inside the if block is executed,
otherwise they are skipped.
if (test - condition) {
//statement if the condition is true
}
Example:
The abstract methods are the methods that have no method body or
implementation.
interface MyInterface {
//abstract methods
}
We can use native as a modifier for only a method. We can’t apply it with
any other entity.
Example:
class NativeDemo {
//statements
}
public native String getLines();
class Main {
NativeDemo obj = new NativeDemo();
obj.getLines();
}
String myString;
myString = null;
//Now myString is an empty string that holds no value.
MyClass obj = new MyClass();
obj = null;
//Now, the object does not hold the reference of MyClass.
To include a class in a package we write it on the very first line of the Java
program.
For Example:
package com.techvidvan.javatutorials.operators;
//This line creates a class inside the operators subpackage
public class ArithmeticOperators {
//Code inside the class
}
Any entity declared as private can only be accessed within the class itself in
which it has been declared. It can’t be accessed from outside that class.
It has the narrowest scope and is the most restricted access specifier in
Java.
The entities declared with a protected keyword can be accessed within the
same package and also from the different packages but only through the
subclasses of that class.
class Parent {
String color = “Red”;
}
class Child extends Parent {
void getColor() {
System.out.println(super.color);
}
}
public class Main() {
public static void main(String args[]) {
Child obj = new Child();
obj.getColor();
}
}
Output:
Red
39. switch keyword
The switch keyword in Java is used for the switch as a conditional
statement.
The switch keyword is very effective against the if statement as with the help
of switch the compiler directly jumps to the particular case rather than
executing all the conditions which are in the if statement.
Output:
Tuesday
40. synchronized keyword
The synchronized keyword in Java is used to achieve synchronization in
Java in a multi-threaded environment.
The methods that are critical and need to be accessed by only a single
resource at the same time, then these methods are declared as synchronized
with the help of a synchronized keyword.
There are some priorities that may differ from platform to platform. That is
the reason why Java introduces the strictfp keywords to ensure that every
platform may run the code in the same precision.
The strictfp keyword is used with classes, interfaces, and methods. The
Example is given below:
The this keyword is used to remove the ambiguity between the instance
variables and parameters.
class ClassName {
int number;
//constructor
ClassName(int number) {
this.number = number;
}
}
For example if the user enters a number greater than 10 then the program
will throw an exception. In this case to throw the custom exception the Java
introduces the throw keyword.
The throw keyword allows the developer to throw the custom exception.
public classThrowKeyword {
public static void ageRestriction(int age) {
if (age < 18) throw new ArithmeticException("Age restriction to vote");
else System.out.println("Allow to Vote");
}
public static void main(String args[]) {
System.out.println("Enter your age: ");
ageRestriction(13);
}
}
Output:
Enter your age
Exception in thread main java.lang.ArithmeticException: Age restriction to
vote
44. throws keyword
The throws keyword in Java is used in Exceptions. We use the throws
keyword for only the checked exceptions in Java.
It indicates the programmer that to be on the safe side you should use it to
avoid exceptions.
try {
//statements
}
catch {
//statements
}
When we declare the data member with a transient keyword then that data
member will not be serialized.
class Demo {
static volatile int myvar = 5;
}
while (condition) {
// the piece of code
}
Conclusion
Keywords in Java are special words that we can use only for the situation
when they are meant to be used. We can’t use them as identifiers.