Reserved words & Operators in Java
Tags: java, beginners, programming, tutorial
Published: 2022-11-17 20:00:36

## 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
        
        ```jsx
        bytes, short, int, long, float, double, char, boolean, void .
        ```
        
    - Access Modifiers
        
        ```java
        public, protected, private, static, final, abstract, native,
        volatile, transient, synchronized, strictfp .. etc
        ```
        
    - Program Flow Controllers.
        
        ```java
        if, else, switch, case, default, for, while, do, break, continue,
        return .. etc.
        ```
        
    - Class/ objects related keywords
        
        ```java
        class, enum, extends, interface, implements, package, import, 
        new, this, super, ... etc
        ```
        
    - Exception Handling keywords
        
        ```java
        throw, throws, try, catch, finally 
        ```
        

The history of different version of java can be seen here [https://en.wikipedia.org/wiki/Java_version_history](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: 
    
    ```jsx
    +,-.,*,/,%,++, --
    ```
    
2. Assignment Operators
    
    ```jsx
    =, +=, -=, *=, /=, %= ...
    ```
    
3. Comparision Operators
    
    ```jsx
    ==, !=, <, >, <=, >=,...
    ```
    
4. Boolean Operator
    
    ```jsx
    &&, ||, !, ^
    ```
    
5. Bitwise Logical Operator
    
    ```jsx
    &, |, ^, <<, >>... 
    ```
    
6. Ternary Operator
    
    ```jsx
    Expression ? resultGenerated : falseResult
    ```
    

Example 1:


![java-operators](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2e2665tlhbubuiqbpwj9.png)

Output:

```jsx
>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:

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


![java-complex-operator](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zladr9hidxnfex49ugt3.png)

Output:

```jsx
>java ComplexOperator
-1
```

Example- 3

```jsx
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++));
	}
}
```

![java-operator](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p0wokmz86bytcydmt20u.png)

Output

```jsx
>java ComplexOperator
196
```


![java-operator](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/von3j4isyercwowddcu9.png)

```jsx
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

```jsx
>java BooleanOperator
false
true
false
false
true
true
true
false
true
false
true
false
```

- Bitwise Operation
    
    ```jsx
    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
    
    ```jsx
    >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....