---
# System prepended metadata

title: Operators

---

# Operators 


## Type Casting Revision 
**Rules :** 
	
1. While type casting if there is no chance of data loss, we will not get any error. 
    * Implicit/ Widening (Automatically)
2. If there is even slight possibility of dataloss in that case we get error. If we still want to typecast we forcefully have to do it.
    * Explicit/ Narrowing (forcefully)
		
 ---       
#### Rule 1: When we do operation between non decimal & decimal, the result is of decimal type	

Code : 
```java
Line 1:    int x = 10;
Line 2:    double y = 10.25;
Line 3:    int z =  x + y ; 
Line 4:    System.out.println(z);		
``` 
Output : 
``` java
Error : possible lossy conversion from double to int.
```
Explanation : 
```
When we perform operation between int and double, the output is double.
And when we try to store double value into int type variable we get error.
```

Code : 
```java 
Line 1:    int x = 10;
Line 2:    double y = 10.25;
Line 3:    double z =  x + y ; 
Line 4:    System.out.println(z);
```
Output :
``` java
20.25
```
Explanation : 
``` 
10 + 10.25 = 20.25
``` 
---
#### Rule 2: Whenever you perform operation between two operands of same category then the result of bigger type
				
Code : 
```java
Line 1:    int x = 20;
Line 2:    long y = 150L;
Line 3:    int z = x+y;
```

Output : 
```java 
Error : possible lossy conversion from long to int.
```

Explanation : 
```
When we perform operation between int and long, the output is long.
And when we try to store long value into int type variable we get error.
```
---
## Operators 
**1. Arithmetic Operators** 
Arithmetic operators include ( + , - , * , / , % )

Code : 
```java 
Line 1:    int a = 10, b = 24;
Line 2:    System.out.println(a+b);
Line 3:    System.out.println(a-b);
Line 4:    System.out.println(a*b);
Line 5:    System.out.println(b/a);
```
Output : 
``` java 
34
-14
240
2
```
Explanation : 
``` 
10 + 24 = 34
10 - 24 = -14
10 * 24 = 240
24 / 10 = 2
```

**Modulus operator ( % )**
This operator gives remainder as output.

Code : 
``` java 
Line 1:    System.out.println(12 % 4);
Line 2:    System.out.println(9 % 7);
Line 3:    System.out.println(24 % 5);
```
Output : 
``` java 
0
2
4
```
Explanation : 
```
When we divide 12 by 4, we get remainder as 0.
When we divide 9 by 7, we get remainder as 2.
When we divide 25 by 5, we get remainder as 4.
```

**2. Relational Operators** 
Relational operators include ( > , >= , < , <= , != , == )

Code : 
``` java
Line 1:    int a = 45, b = 16;
Line 2:    System.out.println(a > b);
Line 3:    System.out.println(a >= b);
Line 4:    System.out.println(a < b);
Line 5:    System.out.println(b <= a);
Line 6:    System.out.println(a != b);
Line 7:    System.out.println(b == a);
``` 
Output : 
``` java 
true
true 
false 
false
true
false
```
Explanation : 
```
45 > 16 gives true because 45 is greater than 16
45 >= 16 gives true because 45 is greater than or equals to 16
45 < 16 gives false because 45 is less than 16
45 <= 16 gives false because 45 is less than or equals to 16
45 != 16 gives true because 45 is not equals to 16
45 == 16 gives false since 45 and 16 are not equal
```

**3. Logical Operators** 
The logical operators include  AND (&&) , OR (||) , NOT (!)

---
#### Truth table of AND Operator

|   A   |   B   | A AND B |
|:-----:|:-----:|:-------:|
| false | false |  false  |
| false | true  |  false  |
| true  | false |  false  |
| true  | true  |  true   |



Code 1: 
``` java 
Line 1: System.out.println( (5 > 4) && (6 < 10) ;
``` 
Output : 
``` java
true
```

Explanation : 
``` 
Condition 1 : 5 > 4 which is true
Condition 2 : 6 < 10 which is true
Since, both conditions are true the output is true.
```

Code 2: 
``` java 
Line 1: System.out.println( (9 >= 4) && (3 >= 50) ;
``` 
Output : 
``` java
false
```

Explanation : 
``` 
Condition 1 : 9 >= 4 which is true
Condition 2 : 3 >= 50 which is false
Since, one of the condition is false the output is false.
```


---
#### Truth table of OR Operator

|   A   |   B   | A OR B |
|:-----:|:-----:|:------:|
| false | false | false  |
| false | true  |  true  |
| true  | false |  true  |
| true  | true  |  true  |



Code 1: 
``` java 
Line 1: System.out.println( (9 >= 4) || (3 >= 50) ;
``` 
Output : 
``` java
true
```

Explanation : 
``` 
Condition 1 : 9 >= 4 which is true
Condition 2 : 3 >= 50 which is false
Since, one of the condition is true the output is true.
```

Code 2: 
``` java 
Line 1: System.out.println( (4 != 4) || (5 <= 9) ) ;
``` 
Output : 
``` java
true
```

Explanation : 
```  
Condition 1 : 4 != 4 which is false
Condition 2 : 5 <= 9 which is true
Since, both the conditions are false the output is true.
```
---
#### Truth table of NOT Operator 


|   A   |  !A   |
|:-----:|:-----:|
| false | true  |
| true  | false |


Code 1: 
``` java 
Line 1: System.out.println( !(5==5) ) ;
``` 
Output : 
``` java
false
```

Explanation : 
``` 
Condition : 5 == 5 which is true
So, !(5 == 5) gives false.
```
Code 2: 
``` java 
Line 1: System.out.println( !(15 < 6) ) ;
``` 
Output : 
``` java
true
```

Explanation : 
``` 
Condition : 15 < 6 which is false
So, !(15 < 6) gives true.
```


---

**4. Assignment Operators**
The assignment operators include ( = , += , -= , *= , /=, %= )

Code : 
``` java 
Line 1: int a = 10;
Line 2: a += 15;
Line 3: System.out.println(a);
``` 
Output : 
``` java
25
```

Explanation : 
```  
Line 2 is equivalent to a = a + 15 
So, the value of variable a is incremeneted by 15
```

Code : 
``` java 
Line 1: int x = 5;
Line 2: x *= 100;
Line 3: System.out.println(x);
``` 
Output : 
``` java
500
```

Explanation : 
```  
Line 2 is equivalent to x = x * 100 
So, the value of variable x is updated to 500
```

**5. Unary Operators**
* Post Operator - 
    * Post Increment - First use and then increment
        Code : 
        ```java
        Line 1: int a = 10;
        Line 2: int b = a++;
        Line 3: System.out.println(a);
        Line 4: System.out.println(b);
        ```
        Output : 
        ```java 
        11
        10
        ```
        Explanation : 
        ```
        a++ is a post increment operator. 
        So, value of variable a which is 10 is stored in variable b 
        And then value of a is incremented to 11
        ```
    * Post decrement - First use and then decrement
        Code : 
        ```java
        Line 1: int a = 10;
        Line 2: int b = a--;
        Line 3: System.out.println(a);
        Line 4: System.out.println(b);
        ```
        Output : 
        ```java 
        9
        10
        ```
        Explanation : 
        ```
        a-- is a post decrement operator. 
        So, value of variable a which is 10 is stored in variable b 
        And then value of a is decremented to 9.
        ```
        


* Pre Operator - 
    * Pre Increment - Increment first and then use
    
         Code : 
        ```java
        Line 1: int a = 10;
        Line 2: int b = ++a;
        Line 3: System.out.println(a);
        Line 4: System.out.println(b);
        ```
        Output : 
        ```java 
        11
        11
        ```
        Explanation : 
        ```
        ++a is a pre increment operator. 
        So, value of variable a is updated to 11 
        And then 11 is stored in variable b
        ```
    * Pre Decrement - Decrement first and then use
         Code : 
        ```java
        Line 1: int a = 10;
        Line 2: int b = --a;
        Line 3: System.out.println(a);
        Line 4: System.out.println(b);
        ```
        Output : 
        ```java 
        9
        9
        ```
        Explanation : 
        ```
        --a is a pre increment operator. 
        So, value of variable a is updated to 9.
        And then 9 is stored in variable b
        ```

## Java operator precedence and associativity

![](https://i.imgur.com/tjaYKFW.png)



