C

Core Java tutorial for beginners

Clean • Professional

Core Java Operators – Arithmetic, Relational, Logical & More

6 minute

Java Operators

In Java, operators are special symbols used to perform operations on variables and values.

They form the backbone of any Java program, enabling calculations, decisions, and logical flows.


What Is an Operator in Java?

An operator is a symbol that tells the compiler to perform specific mathematical, logical, or relational operations on operands (values or variables).

Example:

int a = 10, b = 5;
int sum = a + b; // '+' is an arithmetic operator

Here,

  • a and b are operands
  • + is the operator
  • sum is the result

Types of Operators in Java

Java provides several categories of operators:

learn code with durgesh images

CategoryDescriptionExamples
Arithmetic OperatorsPerform mathematical calculations+, -, *, /, %
Relational OperatorsCompare two values==, !=, >, <, >=, <=
Logical OperatorsCombine multiple conditions&&, `
Bitwise OperatorsOperate on bits&, `
Assignment OperatorsAssign values to variables=, +=, -=, *=, /=, %=
Unary OperatorsWork with a single operand++, --, +, -, !, ~
Ternary OperatorConditional short form of if-else? :
instanceof OperatorTests whether an object is of a specific typeobj instanceof ClassName

1. Arithmetic Operators

Used for basic mathematical operations.

OperatorDescriptionExampleOutput
+Addition10 + 515
-Subtraction10 - 55
*Multiplication10 * 550
/Division10 / 33
%Modulus (Remainder)10 % 31

Example:

int a = 10, b = 3;
System.out.println(a + b);
System.out.println(a / b);
System.out.println(a % b);

2. Assignment Operators

Used to assign values to variables.

They can also perform an operation while assigning.

OperatorExampleEquivalent To
=a = 5;
+=a += 5;a = a + 5;
-=a -= 5;a = a - 5;
*=a *= 5;a = a * 5;
/=a /= 5;a = a / 5;
%=a %= 5;a = a % 5;

3. Unary Operators

Used with a single operand to perform increment, decrement, or negation.

OperatorDescriptionExampleOutput
+Unary plus (no change)+aa
-Unary minus (negates value)-anegative of a
++Incrementa++ or ++aincreases by 1
--Decrementa-- or --adecreases by 1
!Logical NOT!truefalse
~Bitwise Complement~5-6

Example:

int a = 5;
System.out.println(++a); // 6
System.out.println(a--); // 6, then a = 5
System.out.println(-a);  // -5

4. Relational Operators

Used to compare two values, returning a boolean result (true or false).

OperatorDescriptionExampleResult
==Equal to5 == 5true
!=Not equal to5 != 3true
>Greater than5 > 3true
<Less than5 < 3false
>=Greater than or equal5 >= 5true
<=Less than or equal5 <= 4false

5. Logical Operators

Used to combine multiple boolean expressions.

OperatorDescriptionExampleResult
&&Logical AND(a > 5 && b < 10)true if both true
` `Logical OR
!Logical NOT!(a > 5)reverses result

Example:

int a = 10, b = 20;
System.out.println(a > 5 && b > 15); // true
System.out.println(a < 5 || b > 15); // true
System.out.println(!(a == b));       // true

6. Bitwise Operators

Work at bit level (0s and 1s).

Used for low-level programming, encryption, and optimization.

OperatorDescriptionExampleResult
&AND5 & 31
``OR`5
^XOR5 ^ 36
~Complement~5-6
<<Left Shift5 << 110
>>Right Shift5 >> 12
>>>Unsigned Right Shift-5 >>> 1large positive value

Example:

int a = 5, b = 3;
System.out.println(a & b); // 1
System.out.println(a | b); // 7
System.out.println(a ^ b); // 6
System.out.println(a << 1); // 10
System.out.println(a >> 1); // 2

7. Ternary Operator (Conditional Operator)

A short form of if-else.

Syntax:

variable = (condition) ? value_if_true : value_if_false;

Example:

int age = 20;
String result = (age >= 18) ? "Eligible" : "Not Eligible";
System.out.println(result);

8. instanceof Operator

Checks whether an object belongs to a particular class or subclass.

String name = "Java";
boolean check = name instanceof String;
System.out.println(check); // true

9. Operator Precedence & Associativity

Determines the order in which operators are executed in complex expressions.

PrecedenceOperatorDescription
Highest(), [], .Parentheses, array access
 ++, --, !, ~Unary
 *, /, %Multiplicative
 +, -Additive
 <<, >>, >>>Shift
 <, >, <=, >=, instanceofRelational
 ==, !=Equality
 &, ^, ``
 &&, ` 
 ?:Ternary
Lowest=, +=, -= etc.Assignment

Example:

int result = 10 + 5 * 2;  // result = 20, not 30

Example Program

public class JavaOperators {
    public static void main(String[] args) {
        int a = 10, b = 5;

        System.out.println("Arithmetic: " + (a + b));
        System.out.println("Relational: " + (a > b));
        System.out.println("Logical: " + (a > 0 && b > 0));
        System.out.println("Bitwise AND: " + (a & b));
        System.out.println("Ternary: " + ((a > b) ? "A is greater" : "B is greater"));
    }
}

Output:

Arithmetic: 15
Relational: true
Logical: true
Bitwise AND: 0
Ternary: A is greater

🏆 Top 5 Java Interview Questions - Operators in Java

1. What is an operator in Java?

Answer: An operator is a symbol that tells the compiler to perform mathematical, logical, or relational operations on operands (variables or values).

Example:

int a = 10, b = 5;
int sum = a + b; // '+' is an arithmetic operator

  • a and b → operands
  • + → operator
  • sum → result

2. Name the main types of operators in Java.

Answer: Java operators can be classified into several categories:

TypeDescriptionExample
ArithmeticMathematical operations+, -, *, /, %
RelationalCompare values==, !=, >, <, >=, <=
LogicalCombine boolean expressions&&, `
BitwiseOperates at bit level&, `
AssignmentAssigns values=, +=, -=, *=, /=
UnaryWorks with a single operand++, --, +, -, !, ~
TernaryShort if-else?:
instanceofChecks object typeobj instanceof ClassName

3. What is the difference between unary, binary, and ternary operators?

Answer:

Operator TypeOperandsExampleDescription
Unary1++a, !trueWorks on a single operand
Binary2a + b, a > bWorks on two operands
Ternary3(a > b) ? x : yWorks on three operands (conditional operator)

4. Explain the difference between logical and bitwise operators in Java.

Answer:

FeatureLogical OperatorsBitwise Operators
Operates OnBoolean values onlyInteger bits (0s and 1s)
Examples&&, ` 
Short-circuitYes (&&, ` 
Use CaseDecision-makingLow-level programming & optimization

Example:

int a = 5, b = 3;
System.out.println(a & b);  // 1 → bitwise AND
System.out.println(a > 0 && b > 0); // true → logical AND

5. What is the ternary operator and how is it used?

Answer: The ternary operator (?:) is a short form of if-else used to assign values based on a condition.

Syntax:

variable = (condition) ? value_if_true : value_if_false;

Example:

int age = 20;
String result = (age >= 18) ? "Eligible" : "Not Eligible";
System.out.println(result); // Eligible

 

Article 0 of 0