These are symbols which use to perform specific arithmetic or logical operations. Injava, we found many type of operators. We will discuss for each of them below.
- Arithmetic Operators – in java, we found arithmetic operators like+(addition) –(subtraction) *(multiplication) /(divide) %(modulus)
Let’s understand through below examples
public class arithMetic { public static void main(String[] args) { int a=10; int b=20; System.out.println(“a is “+a); int c=a+b; System.out.println(“\n c is “+c); int d=b-1; System.out.println(“\n d is “+d); int f=a*b; System.out.println(“\n f is “+f); int g=b/a; System.out.println(“\n g is “+g); } } |

Let’s a make a code, which divides two values and displays remainder and quotient of them.
public class code { public static void main(String[] args) { int a=10; int b=20; // store quotient(b/a) into variable c int c=(b/a); System.out.println(“\n c is “+c); // store remainder(b%a) into variable d int d=(b%a); System.out.println(“\n d is “+d); } } |

2. Logical Operators – In java, there are 3 types of logical operators
AND | && |
OR | || |
NOT | ! |
First discuss about, AND operator– if both input goes same, then output goes same. Let’s see first code
public class logiCal { public static void main(String[] args) { int a=10; int b=20; // AND OPERATOR checks both condition true ? if (a==10 && b==20) { System.out.println(“a is 10”); System.out.println(“\n b is 20”); } } } |

Let’s see another code using AND operator
public class logiCal2 { public static void main(String[] args) { int a=10; int b=20; // AND OPERATOR checks both condition true ? if (a==10 && b==22) { System.out.println(“statement executed!!”); } } } |
Note – in this code, if block will not execute. Because first condition (a==10) satisfies but second condition (b==22) not satisfied.In the case of AND operator it checks both conditions is true.
** Nowlet’s implement above same code base for OR operator.
Let’s see first code
public class orOpe { public static void main(String[] args) { int a=10; int b=20; // OR OPERATOR checks at-least condition true ? if (a==10 || b==20) { System.out.println(“a is 10”); System.out.println(“\n b is 20”); } } } |

Let’s see another code using OR(||) operator
public class orOpe2 { public static void main(String[] args) { int a=10; int b=20; // OR OPERATOR checks at-least condition true ? if (a==10 || b==22) { System.out.println(“statement executed!!”); } } } |

Note – in this code, if block will be execute. Because first condition (a==10) satisfies though second condition (b==22) not satisfied. In the case of OR operator it checks at-least one condition is true.
** Now let’s implement NOT operator.
public class notOpe { public static void main(String[] args) { int a=10; if (a!=11) { System.out.println(“\n not statement executed!”); } // a=10 but (a!=11)->this condition true,that’s why if-block executed! } } |

3. Unary operator –
In java, in the case for unary operator we found increment and decrement cases.
Increment case: a++ ++a
Decrement case: a– –a
** for the increment cases, a++ and ++a output goes same
** for the decrement cases, a—and –a output goes same
Lets first go through with increment cases :
public class inCR { public static void main(String[] args) { int a=10; System.out.println(“a is “+a); // a++ means -> a=a+1 a++; System.out.println(“a is “+a); } } |

Let’s go through with decrement cases :
public class deCR { public static void main(String[] args) { int a=10; System.out.println(“a is “+a); // a– means -> a=a-1 a–; System.out.println(“a is “+a); } } |

Assignment Operators
This operator helps to initializes values into variable. There are many assignment operators which plays their roles differently. Assignment operators like = += *= etc.
Let’s understand through below coding examples –
public class assignMent1 { public static void main(String[] args) { int a; /* it’s mean we assigning integer value ’10’ into integer variavle a */ a=20; String b; /* it’s mean we assigning string value ‘microcodes.in’ into string variable b */ b=”microcodes.in”; float amnt; /* it’s mean we assigning float value ‘34.5’ into float variable ‘amnt’ */ amnt = 34.5F; } } |
Let’s see some another coding examples
x += y it’s mean x = x + y
public class assign2 { public static void main(String[] args) { int a=10; int b=20; /* b+=a -> b = b+a */ b += a; System.out.println(“a is “+a); System.out.println(“\n b is “+b); } } |

Let’s see some another coding examples
x *= y it’s mean x = x * y
public class assign3 { public static void main(String[] args) { int a=10; int b=20; // b*=a -> b = b*a b *= a; System.out.println(“a is “+a); System.out.println(“\n b is “+b); } } |

Let’s see some another coding examples
X %= Y
Means, X = X % Y
Let’s understand through below coding example
public class assign4 { public static void main(String[] args) { int a=10,b=2; a%=b; // (a%=b) -> a=a%b System.out.println(“\n a is “+a); System.out.println(“\n b is “+b); } } |

Relational Operators
In java like other programming languages, we found many relational operators <(greater than) <(less than) >=(greater than equal) <=(less than equal) ==(checks both operands are equal or not)
Let’s see some coding examples
public class rela1 { public static void main(String[] args) { int a=2,b=3; if (a<b) { System.out.println(“\n a is smaller than b”); } int c=5,d=20; if (d>c) { System.out.println(“\n d is greater than c”); } } } |

Let’s see another coding examples
public class rela2 { public static void main(String[] args) { int a=10; int b=a; if (b>=a) { System.out.println(“b >= a”); } int c=5; if (c<=b) { System.out.println(“\n c <= b”); } } } |

public class rela3 { public static void main(String[] args) { int a=10; // checks both operands-> a,10 if (a==10) { System.out.println(“\n a is 10”); } } } |

Ternary Operator
In java, we can work with ternary operator.
Syntax – variable = expression 1 ? expression 2: expression 3
Remember – if ‘expression 1’ satisfies then ‘expression 2’ will get into ‘variable’. If ‘expression 1’ not satisfies then ‘expression 3’ will get into ‘variable’.
Let’s understand through below code example
public class ter { public static void main(String[] args) { int a=10; // a==10 condition satisfies int res = (a==10?3:2); System.out.println(“\n res is “+res); } } |

Let’s understand other examples
public class ter2 { public static void main(String[] args) { int b=20; String res = (b<30?”developer”:”hacker”); System.out.println(“\n res is “+res); b=50; res = (b<30?”developer”:”hacker”); System.out.println(“\n after res is “+res); } } |

Shift Operator
In java, we have basically three types of shift operators – signed left shift(<<) and signed right shift operators(>>) and unsigned right shift(>>>).
Left Shift Operator
Let’s take an example,
10<<2 means 10*(2*2) = 10*4 = 40
20<<3 means 20*(2*3) = 20*8 = 160
Let’s understand through below coding example
public class leftShift { public static void main(String[] args) { int a; a = 10<<2; System.out.println(“a is “+a); int b; b = 20<<3; System.out.println(“\n b is “+b); } } |

Right Shift Operator
10>>2 means 10/(2*2) = 10/4 = 2 (stores quotient)
20>>3 means 20/(2*3) = 20/8 = 2
Let’s understand through below coding example
public class rightShift { public static void main(String[] args) { int a; a = 10>>2; System.out.println(“a is “+a); int b; b = 20>>3; System.out.println(“\n b is “+b); } } |

Difference between >> and >>> operator
Both >> and >>> are used to shift the bits towards the right. The difference is that the >> preserve the sign bit while the operator >>> does not preserve the sign bit. To preserve the sign bit, you need to add 0 in the MSB.
Let’s understand below code example
public class allShift { public static void main(String args[]) { byte x, y; x=10; y=-10; System.out.println(“Bitwise Left Shift: x<<2 = “+(x<<2)); System.out.println(“Bitwise Right Shift: x>>2 = “+(x>>2)); System.out.println(“Bitwise Zero Fill Right Shift: x>>>2 = “+(x>>>2)); System.out.println(“Bitwise Zero Fill Right Shift: y>>>2 = “+(y>>>2)); } } |

In java, we found many type of characters. Let’s discuss about them
- Newline Characters – it adds a new line. It represented using \n . but remember one thing, when we using println() method it generate a new line in it’s statement. For print() method use cases it can’t happened.
Let’s understand by this below code – we will see using newline character and without using newline character.
Without using newline character | Using newline character |
public class newLine { public static void main(String[] args) { System.out.print(“without new-line character”); System.out.print(“this is line 1”); System.out.print(“this is line 2”); System.out.print(“this is line 3”); } } | public class newLine { public static void main(String[] args) { System.out.print(“using new-line character”); System.out.print(“\nthis is line 1”); System.out.print(“\nthis is line 2”); System.out.print(“\nthis is line 3”); } } |


Tab Character – using this character, we can generate tab. Let’s understand through below example…
public class newTab { public static void main(String[] args) { // \t -> generate a tab space System.out.println(“akash\tabdul”); // see without /t between two words ‘akash’ ‘abdul’ System.out.println(“akashabdul”); } } |
