Reserved words & Operators in Java

Praveen Kumar
4 min readNov 17, 2022

--

Keywords / Reserved words

Hi guys, I am back with another topic on Java, which is 5th article of our ongoing java programming series. So In today’s article we will focusing on reserved words of Java language which has been used throughout the language for their pre-defined meanings.

Meanwhile if you are reading this article series for the first time then, do checkout the previous articles of the series.

Article-4 Article-3 Article-2 Article-1 click to read the respective article in ongoing java series.

  • What are keywords?
  • If any predefined word having both
  1. word recognition
  2. internal functionality
  • Then that predefined words are called as Keywords .
  • What is reserved words?
  • If any predefined words having only word recognition without internal functionality then that predefined word is called as Reserved words .
  • example:
  • goto, const etc
  • To develop a java application, we have different list of keywords provided by Java.
  • Data Types & Return Types
bytes, short, int, long, float, double, char, boolean, void .
  • Access Modifiers
public, protected, private, static, final, abstract, native,
volatile, transient, synchronized, strictfp .. etc
  • Program Flow Controllers.
if, else, switch, case, default, for, while, do, break, continue,
return .. etc.
  • Class/ objects related keywords
class, enum, extends, interface, implements, package, import, 
new, this, super, ... etc
  • Exception Handling keywords
throw, throws, try, catch, finally

The history of different version of java can be seen here https://en.wikipedia.org/wiki/Java_version_history

Operators

Operator is a symbol, which performs a particular operation over the provided operands.

In java programming language we have given below lists of operators:

  1. Arithmetic Operators:
+,-.,*,/,%,++, --
  1. Assignment Operators
=, +=, -=, *=, /=, %= ...
  1. Comparision Operators
==, !=, <, >, <=, >=,...
  1. Boolean Operator
&&, ||, !, ^
  1. Bitwise Logical Operator
&, |, ^, <<, >>...
  1. Ternary Operator
Expression ? resultGenerated : falseResult

Example 1:

Output:

>java Operators
The number is 10
The post increased number is10
the pre increased number is 12
the post decreased number is 12
the pre decreased number is 10
the number is : 10

Example-2:

class ComplexOperator{
public static void main(String[] args){
int num = 5;
System.out.println("The number is " + ++num-++num);
}
}

Output:

>java ComplexOperator
-1

Example- 3

class ComplexOperator{
public static void main(String[] args){
int num = 5, a = 10;
//System.out.println(++num - ++num);
System.out.println((--a + --a) * (++a - a--) + (--a + a--) * (++a + a++));
}
}

Output

>java ComplexOperator
196
class BooleanOperator{
public static void main(String[] args){
boolean firstDecesion = true;
boolean secondDecesion = false;
System.out.println(firstDecesion && secondDecesion);
System.out.println(firstDecesion && firstDecesion);
System.out.println(secondDecesion && firstDecesion);
System.out.println(secondDecesion && secondDecesion);


System.out.println(firstDecesion || secondDecesion);
System.out.println(firstDecesion || firstDecesion);
System.out.println(secondDecesion || firstDecesion);
System.out.println(secondDecesion || secondDecesion);


System.out.println(firstDecesion ^ secondDecesion);
System.out.println(firstDecesion ^ firstDecesion);
System.out.println(secondDecesion ^ firstDecesion);
System.out.println(secondDecesion ^ secondDecesion);


}

}

Output

>java BooleanOperator
false
true
false
false
true
true
true
false
true
false
true
false
  • Bitwise Operation
class BitwiseOperation{
public static void main(String[] args){
//create variable
int firstNum =10;
int secondNum = 2;
//Bitwise operation using Bitwise operator
System.out.println("The bitwise AND is: " + (firstNum & secondNum));
System.out.println("The bitwise OR is: " + (firstNum | secondNum));
System.out.println("The bitwise XOR is: " + (firstNum ^ secondNum));
//left and right shift operations
System.out.println("The bitwise Left Shift is: " + (firstNum << secondNum));
System.out.println("The bitwise Right Shift is: " + (firstNum >> secondNum));
}
}
  • Output
>java BitwiseOperation
The bitwise AND is: 2
The bitwise OR is: 10
The bitwise XOR is: 8
The bitwise Left Shift is: 40
The bitwise Right Shift is: 2

Comparison of & vs &&

In case of Logical AND operator && , if the first operand value is false then it is not required to check the second operand value, directly, we can predict the result of overall expression will be false.

In case of Bitwise AND operator & , even first operand value is false, still, JVM evaluates second operand value then only JVM will the overall result of the expression as false. So here evaluating second operand value is unnecessary, at the same time it also increases execution time which reduces the application performance.

Note: If the first operand value is true than it is manadatory for JVM to evaluate the second operand value, In order to get overall expression result.

Based on the number of operands involved with an Operator, It can be classified as :

  1. Unary Operator ( performs operation on only one operand)
  2. Binary Operator ( performs operation on two operands)
  3. Ternary Operator ( performs operation on three operands)

That’s all for this article guys, will meet again with new concept of Java till then bye bye !! Be healthy keep coding….

--

--