Java Operators used to perform data manipulation while programming. Although there are multiple Class and Methods present in the Java library to perform such operations. We have some java operators which are used to perform basic calculation and operations.
Operation Types
- Arithmetic Operations: To perform the basic calculation on numeric values.
- Relational Operations: To perform the comparison between two values.
- Logical Operations: To perform the action based on multiple conditions.
- Assignment Operations: To assign a value to a variable.
- Ternary Operations: Single statement to check any condition and perform the action.
- Bitwise Operations: To perform operations on bit values.

Related Links:
- Basic Java – 1 || Understand Java before start learning JAVA.
- Basic Java – 2 || Variables and Data Types used in JAVA.
- Basic Java – 3 || Understanding Class, Objects, Methods in Java.
- Basic Java – 4 || More on methods(Return Type and Parameters)
- Basic Java – 5 || Methods- Call by Value and Call by Reference in Java.
- Basic Java – 6 || Understanding of Constructor and Destructor in JAVA.
- Basic Java – 7 || Static Variables and Methods.
- Basic Java – 8 || Lets learn about Arrays in Java.
- Basic Java – 9 || Performing multiple operations using Java Operators.
- Basic Java – 10 || Conditions (If and Switch) in JAVA.
- Basic Java – 11 || for and for-each in Java. (Loops Part-1)
- Basic Java – 12 || Alternate looping concepts while and do-while. (Loops Part-2)
- Basic Java – 13 || Decimal values v/s Octal base(8) values in JAVA.
- Basic Java – 14 || Learn about String literals in Java.
- Basic Java – 15 || Runtime User Input using Scanner Class (Part-1).
- Basic Java – 16 || Runtime User Input using BufferedReader Class (Part-2).
- Basic Java – 17 || Runtime User Input using Console Class (Part-3).
- Basic Java – 18 || Difference between break and continue keywords.
- Basic Java – 19 || Sending Email using Java (Part-1).
- Basic Java – 20 || Sending Email with attachment using Java (Part-2).
- Basic Java – 21 || Stack memory and Heap memory in Java.
- Basic Java – 22 || Let’s learn more about String.
- Basic Java – 23 || String, StringBuffer & StringBuilder in Java.
- Basic Java – 24 || Exception Handling using Try Catch.
- QnA || How to use Constructors in Abstract class?
- QnA | Difference between Integer and int keywords.
- QnA | Can main method be overloaded?
- QnA | How do I reverse a String/Sentence in Java?
- QnA | Perform Multiplication and Division without * or / or % operators.
- QnA | How do I get the default value of data type?
- QnA | How to split String if it contains period symbol (.) in between?
Code Example of Arithmetic Operators
public class Arithmetic { public static void main(String[] args) { int a=20; int b=30; int c=7; System.out.println("Arithmetic Operations on variables a and b"); System.out.println(a+b); // Returns sum of two values. System.out.println(b-a); // Returns difference between two values. System.out.println(a*b); // Returns multiplication of two values. System.out.println(a/c); // Divide two values and returns Remainder. System.out.println(a%c); // Divide two values and returns Quotient. } } Output: Arithmetic Operations on variables a and b 50 10 600 2 6
Along with the above basic operation, there are two more arithmetic operators present:
- Increment(++): Post Increment and Pre Increment
- Decrement(–): Post Decrement and Pre Decrement
Combination of two consecutive addition(+) symbols increases a value by 1. On the other hand, two consecutive negative(-) symbols decrease value by 1.
public class Arithmetic { public static void main(String[] args) { int a=20; int b=30; // ++ increment // -- decrement System.out.println("\nPre Increment and Decrement Operations"); System.out.println(++a); System.out.println(- -b); System.out.println("\nPost Increment and Decrement Operations"); System.out.println(a++); System.out.println(b- -); } } Output: Pre Increment and Decrement Operations 21 29 Post Increment and Decrement Operations 21 29
Always remember in Pre Increment/Decrement value gets change before the current line termination or end of the statement. In the above example, the value of a get increased by 1 value and get printed on the console.
On the other hand, in Post Increment/Decrement value gets changed only after the execution of current line get finished. And hence value doesn’t get the change in last two statements.
Code Example of Relational Operators
public class Relational { public static void main(String[] args) { int a=10; int b=20; int c=10; if(a<b) { System.out.println("Value of a is less than b."); } if(a==c) { System.out.println("Value of a is equal to c."); } // Same way we can use other relational operators. } }
Code Example of Logical Operators
public class Logical { public static void main(String[] args) { int a=10; int b=20; // Result of both conditions should be true if((a!=0) && (a<b)) { System.out.println("&& - Value of a is less than b and not equal to 0."); } // Result of at least one condition(s) should be true. if((a==0) || (a<=10)) { System.out.println("|| - Value of a is not equal to 0 but less than 10."); } // ! operator reverses the result of condition. // If result is True ! converts the same in False. // If result is False ! converts the same in True. if(!(a==12)) { System.out.println("! - Value of a is not equal to 12, still result of condition get reversed."); } } } Output: && - Value of a is less than b and not equal to 0. || - Value of a is not equal to 0 but less than 10. ! - Value of a is not equal to 12, still result of condition get reversed.
Code Example of Assignment Operators
public class Assignment { public static void main(String[] args) { // Assign value to a variable int a=10; System.out.println("Value of a: "+a); // Following statement is same as 'a=a*5' // First it multiplies value of "a with 5" // Then it assign new value to variable "a" a*=5; System.out.println("Value of a after increment: "+a); // The same way we can use other Arithmetic Operators. } } Output: Value of a: 10 Value of a after increment: 50
Code Example of Ternary Operator
Ternary Operator takes 3 arguments, first, it checks the condition, the second argument gets executed if the condition is true and third argument get executed if the condition is false.
public class Ternary { public static void main(String[] args) { int a=10; int b=20; // First argument verify condition. // Second statement execute when condition is True. // Third statement execute when condition is False. String result=(a>b)?"True- value of a is greater than b.":"False- value of a is less than b."; System.out.println(result); } } Output: False- value of a is less than b.
Code Example of Bitwise Operators
public class Bitwise { public static void main(String[] args) { int a=10; int b=15; // Performed operations on bits of numeric values. System.out.println("Numeric value of a :"+a); System.out.println("Numeric value of b :"+b); System.out.println("\nBinary value of a :"+Integer.toBinaryString(a)); System.out.println("Binary value of b :"+Integer.toBinaryString(b)); System.out.println("\nBinary value of a&b :"+Integer.toBinaryString(a&b)); System.out.println("Binary value of a|b :"+Integer.toBinaryString(a|b)); System.out.println("Binary value of a^b :"+Integer.toBinaryString(a^b)); System.out.println("Binary value of ~a :"+Integer.toBinaryString(~a)); System.out.println("\n'Bitwise AND' of 10 and 15 is ="+(a&b)); System.out.println("'Bitwise OR' of 10 and 15 is ="+(a|b)); System.out.println("'Bitwise XOR' of 10 and 15 is ="+(a^b)); System.out.println("'Bitwise Complement' of 10 is ="+(~a)); } } Output: Numeric value of a :10 Numeric value of b :15 Binary value of a :1010 Binary value of b :1111 Binary value of a&b :1010 Binary value of a|b :1111 Binary value of a^b :101 Binary value of ~a :11111111111111111111111111110101 'Bitwise AND' of 10 and 15 is =10 'Bitwise OR' of 10 and 15 is =15 'Bitwise XOR' of 10 and 15 is =5 'Bitwise Complement' of 10 is =-11