Tutorials Logic, IN info@tutorialslogic.com
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Website Development
Practice
Quiz Challenge Interview Questions Certification Practice
Tools
Online Compiler JSON Formatter Regex Tester CSS Unit Converter Color Picker
Compiler Tools

Operators in Java Arithmetic, Logical, Bitwise: Tutorial, Examples, FAQs & Interview Tips

What are Operators in Java?

Operators in Java are special symbols that perform specific operations on one, two, or three operands, and then return a result. Java provides a rich set of operators to manipulate variables and perform computations. Operators are the foundation of any programming language and are essential for building logic in your programs.

Java operators are classified into several categories: arithmetic, relational, logical, bitwise, assignment, unary, ternary, and special operators like instanceof. Understanding these operators and their precedence is crucial for writing efficient and bug-free code.

1. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication, division, and modulus.

OperatorNameExampleResultDescription
+Addition5 + 38Adds two operands
-Subtraction5 - 32Subtracts second operand from first
*Multiplication5 * 315Multiplies two operands
/Division10 / 33Divides first operand by second (integer division)
%Modulus10 % 31Returns remainder of division
++Incrementa++a = a + 1Increases value by 1
--Decrementa--a = a - 1Decreases value by 1
Arithmetic Operators
public class ArithmeticOps {
    public static void main(String[] args) {
        int a = 10, b = 3;
        
        // Basic arithmetic
        System.out.println("a + b = " + (a + b));   // 13
        System.out.println("a - b = " + (a - b));   // 7
        System.out.println("a * b = " + (a * b));   // 30
        System.out.println("a / b = " + (a / b));   // 3 (integer division)
        System.out.println("a % b = " + (a % b));   // 1 (remainder)
        
        // Division with doubles
        double x = 10.0, y = 3.0;
        System.out.println("x / y = " + (x / y));   // 3.3333...
        
        // Increment and decrement
        int count = 5;
        System.out.println("count++ = " + count++); // 5 (post-increment: use then increment)
        System.out.println("count = " + count);     // 6
        System.out.println("++count = " + ++count); // 7 (pre-increment: increment then use)
        System.out.println("count-- = " + count--); // 7 (post-decrement)
        System.out.println("count = " + count);     // 6
    }
}

2. Relational (Comparison) Operators

Relational operators compare two values and return a boolean result (true or false). They are commonly used in conditional statements and loops.

OperatorNameExampleResultDescription
==Equal to5 == 3falseChecks if two values are equal
!=Not equal to5 != 3trueChecks if two values are not equal
>Greater than5 > 3trueChecks if left is greater than right
<Less than5 < 3falseChecks if left is less than right
>=Greater than or equal5 >= 5trueChecks if left is greater than or equal to right
<=Less than or equal5 <= 3falseChecks if left is less than or equal to right

3. Logical Operators

Logical operators are used to combine multiple boolean expressions or invert boolean values. They are essential for complex conditional logic.

OperatorNameExampleResultDescription
&&Logical ANDtrue && falsefalseReturns true if both operands are true
||Logical ORtrue || falsetrueReturns true if at least one operand is true
!Logical NOT!truefalseInverts the boolean value
Relational & Logical Operators
public class RelationalLogical {
    public static void main(String[] args) {
        int a = 10, b = 3;
        
        // Relational operators
        System.out.println("a > b: " + (a > b));    // true
        System.out.println("a < b: " + (a < b));    // false
        System.out.println("a == b: " + (a == b));  // false
        System.out.println("a != b: " + (a != b));  // true
        System.out.println("a >= 10: " + (a >= 10)); // true
        System.out.println("b <= 5: " + (b <= 5));  // true
        
        // Logical operators
        boolean x = true, y = false;
        System.out.println("x && y: " + (x && y));  // false (AND)
        System.out.println("x || y: " + (x || y));  // true (OR)
        System.out.println("!x: " + (!x));          // false (NOT)
        
        // Complex conditions
        int age = 25;
        boolean hasLicense = true;
        boolean canDrive = (age >= 18) && hasLicense;
        System.out.println("Can drive: " + canDrive); // true
        
        // Short-circuit evaluation
        int num = 0;
        if (num != 0 && (10 / num) > 1) {  // Second part not evaluated
            System.out.println("This won't execute");
        }
        System.out.println("No division by zero error!");
    }
}

4. Assignment Operators

Assignment operators are used to assign values to variables. Java provides compound assignment operators that combine arithmetic operations with assignment.

OperatorExampleEquivalent ToDescription
=a = 5-Simple assignment
+=a += 5a = a + 5Add and assign
-=a -= 5a = a - 5Subtract and assign
*=a *= 5a = a * 5Multiply and assign
/=a /= 5a = a / 5Divide and assign
%=a %= 5a = a % 5Modulus and assign

5. Bitwise Operators

Bitwise operators perform operations on individual bits of integer types. They are used in low-level programming, optimization, and working with flags.

OperatorNameExample (a=5, b=3)BinaryResult
&Bitwise AND5 & 30101 & 00111 (0001)
|Bitwise OR5 | 30101 | 00117 (0111)
^Bitwise XOR5 ^ 30101 ^ 00116 (0110)
~Bitwise NOT~5~0101-6
<<Left shift5 << 10101 << 110 (1010)
>>Right shift5 >> 10101 >> 12 (0010)
Assignment & Bitwise Operators
public class AssignmentBitwise {
    public static void main(String[] args) {
        // Assignment operators
        int n = 10;
        n += 5;   // n = n + 5 → 15
        n -= 3;   // n = n - 3 → 12
        n *= 2;   // n = n * 2 → 24
        n /= 4;   // n = n / 4 → 6
        n %= 4;   // n = n % 4 → 2
        System.out.println("n = " + n);  // 2
        
        // Bitwise operators
        int a = 5;  // 0101 in binary
        int b = 3;  // 0011 in binary
        
        System.out.println("a & b = " + (a & b));   // 1 (0001)
        System.out.println("a | b = " + (a | b));   // 7 (0111)
        System.out.println("a ^ b = " + (a ^ b));   // 6 (0110)
        System.out.println("~a = " + (~a));         // -6
        System.out.println("a << 1 = " + (a << 1)); // 10 (multiply by 2)
        System.out.println("a >> 1 = " + (a >> 1)); // 2 (divide by 2)
        
        // Practical: check even/odd with bitwise AND
        int num = 7;
        if ((num & 1) == 0) {
            System.out.println(num + " is even");
        } else {
            System.out.println(num + " is odd");
        }
    }
}

6. Ternary Operator (?:)

The ternary operator is a shorthand for if-else statements. Syntax: condition ? valueIfTrue : valueIfFalse

Ternary & instanceof
public class TernaryInstanceof {
    public static void main(String[] args) {
        // Ternary operator
        int age = 20;
        String status = (age >= 18) ? "Adult" : "Minor";
        System.out.println(status);  // Adult
        
        // Find maximum
        int a = 10, b = 20;
        int max = (a > b) ? a : b;
        System.out.println("Max: " + max);  // 20
        
        // instanceof operator
        String str = "Hello";
        System.out.println(str instanceof String);   // true
        System.out.println(str instanceof Object);   // true
        
        // Pattern matching (Java 16+)
        Object obj = "World";
        if (obj instanceof String s) {
            System.out.println("Length: " + s.length());
            System.out.println("Uppercase: " + s.toUpperCase());
        }
    }
}
Key Takeaways
  • Java provides 7 types of operators: arithmetic, relational, logical, bitwise, assignment, unary, and ternary.
  • Increment (++) and decrement (--) have two forms: prefix (++a) increments before use, postfix (a++) increments after use.
  • Logical AND (&&) and OR (||) use short-circuit evaluation - the second operand may not be evaluated.
  • Bitwise operators work on individual bits and are useful for low-level programming and optimization.
  • The ternary operator (? :) is a concise way to write simple if-else statements.
  • Operator precedence determines evaluation order - use parentheses to make your intentions clear.

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.