Skip to main content

Java Operators

 

Java Operators – A Complete Guide

Operators in Java are special symbols that perform operations on variables and values. They help in performing arithmetic, logical, bitwise, and other computations in Java programs. In this post, we will explore different types of operators with examples.


1. Types of Java Operators

Java operators can be categorized into the following types:

  1. Arithmetic Operators
  2. Relational (Comparison) Operators
  3. Logical Operators
  4. Bitwise Operators
  5. Assignment Operators
  6. Unary Operators
  7. Ternary Operator
  8. Instanceof Operator

Let’s dive into each type with examples.


2. Arithmetic Operators

Used for mathematical operations like addition, subtraction, multiplication, etc.

OperatorDescriptionExample (a = 10, b = 5)Result
+Additiona + b15
-Subtractiona - b5
*Multiplicationa * b50
/Divisiona / b2
%Modulus (Remainder)a % b0

Example:

public class ArithmeticExample {
public static void main(String[] args) { int a = 10, b = 5; System.out.println("Addition: " + (a + b)); System.out.println("Subtraction: " + (a - b)); System.out.println("Multiplication: " + (a * b)); System.out.println("Division: " + (a / b)); System.out.println("Modulus: " + (a % b)); } }

3. Relational (Comparison) Operators

Used to compare values and return a boolean result (true or false).

OperatorDescriptionExample (a = 10, b = 5)Result
==Equal toa == bfalse
!=Not equal toa != btrue
>Greater thana > btrue
<Less thana < bfalse
>=Greater than or equal toa >= btrue
<=Less than or equal toa <= bfalse

Example:

public class RelationalExample {
public static void main(String[] args) { int a = 10, b = 5; System.out.println(a > b); // true System.out.println(a < b); // false System.out.println(a == b); // false System.out.println(a != b); // true } }

4. Logical Operators

Used to combine multiple conditions and return a boolean result.

OperatorDescriptionExample (x = true, y = false)Result
&&ANDx && yfalse
``OR
!NOT!xfalse

Example:

public class LogicalExample {
public static void main(String[] args) { boolean x = true, y = false; System.out.println(x && y); // false System.out.println(x || y); // true System.out.println(!x); // false } }

5. Bitwise Operators

Operate at the bit level to perform binary calculations.

OperatorDescriptionExample (a = 5, b = 3)Result
&ANDa & b1
``OR`a
^XORa ^ b6
~Complement~a-6
<<Left shifta << 110
>>Right shifta >> 12

Example:

public class BitwiseExample {
public static void main(String[] args) { 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); // -6 System.out.println(a << 1); // 10 System.out.println(a >> 1); // 2 } }

6. Assignment Operators

Used to assign values to variables.

OperatorDescriptionExample (a = 10)Equivalent To
=Assigna = 10
+=Add and assigna += 5a = a + 5
-=Subtract and assigna -= 5a = a - 5
*=Multiply and assigna *= 5a = a * 5
/=Divide and assigna /= 5a = a / 5
%=Modulus and assigna %= 5a = a % 5

Example:

public class AssignmentExample {
public static void main(String[] args) { int a = 10; a += 5; // a = a + 5 System.out.println(a); // 15 } }

7. Unary Operators

Used with a single operand.

OperatorDescriptionExample (a = 10)Result
+Unary plus+a10
-Unary minus-a-10
++Increment++a11
--Decrement--a9

Example:

public class UnaryExample {
public static void main(String[] args) { int a = 10; System.out.println(++a); // 11 System.out.println(--a); // 10 } }

8. Ternary Operator (? :)

A shorthand for if-else.

Syntax:

variable = (condition) ? value_if_true : value_if_false;

Example:

public class TernaryExample {
public static void main(String[] args) { int a = 10, b = 20; int min = (a < b) ? a : b; System.out.println("Minimum: " + min); } }

9. instanceof Operator

Used to check whether an object belongs to a specific class.

Example:

public class InstanceofExample {
public static void main(String[] args) { String str = "Hello"; System.out.println(str instanceof String); // true } }

Conclusion

Operators are essential for performing computations in Java. Mastering them will help you write efficient and concise code. In the next post, we’ll explore Control Flow Statements in Java, including loops and conditional statements.

Comments