DEV Community

Praveen kumar
Praveen kumar

Posted on

Reserved words & Operators in Java

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` .
Enter fullscreen mode Exit fullscreen mode
  • 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:

    +,-.,*,/,%,++, --
    
  2. Assignment Operators

    =, +=, -=, *=, /=, %= ...
    
  3. Comparision Operators

    ==, !=, <, >, <=, >=,...
    
  4. Boolean Operator

    &&, ||, !, ^
    
  5. Bitwise Logical Operator

    &, |, ^, <<, >>... 
    
  6. Ternary Operator

    Expression ? resultGenerated : falseResult
    

Example 1:

java-operators

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
Enter fullscreen mode Exit fullscreen mode

Example-2:

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

java-complex-operator

Output:

>java ComplexOperator
-1
Enter fullscreen mode Exit fullscreen mode

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++));
    }
}
Enter fullscreen mode Exit fullscreen mode

java-operator

Output

>java ComplexOperator
196
Enter fullscreen mode Exit fullscreen mode

java-operator

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);


    }

}
Enter fullscreen mode Exit fullscreen mode

Output

>java BooleanOperator
false
true
false
false
true
true
true
false
true
false
true
false
Enter fullscreen mode Exit fullscreen mode
  • 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 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....

Top comments (0)