Reserved words & Operators in Java
Praveen kumar
Posted on November 17, 2022
Keywords / Reserved words
-
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:
-
Arithmetic Operators:
+,-.,*,/,%,++, --
-
Assignment Operators
=, +=, -=, *=, /=, %= ...
-
Comparision Operators
==, !=, <, >, <=, >=,...
-
Boolean Operator
&&, ||, !, ^
-
Bitwise Logical Operator
&, |, ^, <<, >>...
-
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 :
- Unary Operator ( performs operation on only one operand)
- Binary Operator ( performs operation on two operands)
- Ternary Opertor ( 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....
Posted on November 17, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 6, 2024