codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

9 / 65 topics
1Java Intro2Java Get Started3Java Syntax4Java Output5Java Comments6Java Variables7Java Data Types8Java Type Casting9Java Operators10Java Strings11Java Math12Java Booleans13Java If...Else14Java Switch15Java While Loop16Java For Loop17Java Break/Continue18Java Arrays
Tutorials/Java Programming/Java Operators
☕Java Programming

Java Operators

Updated 2026-05-12
30 min read

Java Operators

In this tutorial, we will explore various types of operators available in Java. Understanding these operators is crucial for performing operations on variables and controlling the flow of your programs. Whether you're performing arithmetic calculations or making logical decisions, operators are the backbone of any Java program.

Introduction

Operators in Java are symbols that perform specific operations on one or more operands (variables or values). They help manipulate data, control program execution, and make comparisons. Operators can be categorized into several types: arithmetic, assignment, comparison, logical, and bitwise. Each type serves a unique purpose and is essential for writing effective Java code.

Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, and modulus (remainder).

Common Arithmetic Operators

OperatorDescriptionExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Modulus (remainder)a % b

Example

Let's see an example of using arithmetic operators in a Java program.

ArithmeticOperators.java
1public class ArithmeticOperators {
2 public static void main(String[] args) {
3 int a = 10;
4 int b = 5;
5
6 System.out.println("Addition: " + (a + b));
7 System.out.println("Subtraction: " + (a - b));
8 System.out.println("Multiplication: " + (a * b));
9 System.out.println("Division: " + (a / b));
10 System.out.println("Modulus: " + (a % b));
11 };
12}
Output
Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2
Modulus: 0

Assignment Operators

Assignment operators are used to assign values to variables. The most common assignment operator is =.

Common Assignment Operators

OperatorDescriptionExample
=Simple assignmenta = b
+=Add and assigna += b (equivalent to a = a + b)
-=Subtract and assigna -= b (equivalent to a = a - b)
*=Multiply and assigna *= b (equivalent to a = a * b)
/=Divide and assigna /= b (equivalent to a = a / b)
%=Modulus and assigna %= b (equivalent to a = a % b)

Example

Here's an example demonstrating the use of assignment operators.

AssignmentOperators.java
1public class AssignmentOperators {
2 public static void main(String[] args) {
3 int a = 10;
4 int b = 5;
5
6 a += b; // equivalent to a = a + b
7 System.out.println("a += b: " + a);
8
9 a -= b; // equivalent to a = a - b
10 System.out.println("a -= b: " + a);
11
12 a *= b; // equivalent to a = a * b
13 System.out.println("a *= b: " + a);
14
15 a /= b; // equivalent to a = a / b
16 System.out.println("a /= b: " + a);
17
18 a %= b; // equivalent to a = a % b
19 System.out.println("a %= b: " + a);
20 }
21}
Output
a += b: 15
a -= b: 10
a *= b: 50
a /= b: 10
a %= b: 0

Comparison Operators

Comparison operators are used to compare two values and return a boolean result (true or false). These operators are essential for decision-making in Java.

Common Comparison Operators

OperatorDescriptionExample
==Equal toa == b
!=Not equal toa != b
>Greater thana > b
<Less thana < b
>=Greater than or equala >= b
<=Less than or equala <= b

Example

Let's see how comparison operators work in a Java program.

ComparisonOperators.java
1public class ComparisonOperators {
2 public static void main(String[] args) {
3 int a = 10;
4 int b = 5;
5
6 System.out.println("a == b: " + (a == b));
7 System.out.println("a != b: " + (a != b));
8 System.out.println("a > b: " + (a > b));
9 System.out.println("a < b: " + (a < b));
10 System.out.println("a >= b: " + (a >= b));
11 System.out.println("a <= b: " + (a <= b));
12 }
13}
Output
a == b: false
a != b: true
a > b: true
a < b: false
a >= b: true
a <= b: false

Logical Operators

Logical operators are used to combine multiple conditions. They help in making complex decisions based on multiple criteria.

Common Logical Operators

OperatorDescriptionExample
&&Logical ANDa && b
||Logical ORa || b
!Logical NOT!a

Example

Here's an example demonstrating the use of logical operators.

LogicalOperators.java
1public class LogicalOperators {
2 public static void main(String[] args) {
3 boolean a = true;
4 boolean b = false;
5
6 System.out.println("a && b: " + (a && b));
7 System.out.println("a || b: " + (a || b));
8 System.out.println("!a: " + (!a));
9 }
10}
Output
a && b: false
a || b: true
!a: false

Bitwise Operators

Bitwise operators perform operations on the binary representation of numbers. They are used for low-level bit manipulation.

Common Bitwise Operators

OperatorDescriptionExample
&Bitwise ANDa & b
|Bitwise ORa | b
^Bitwise XORa ^ b
~Bitwise NOT~a
<<Left shifta << n
>>Right shifta >> n

Example

Let's see an example of using bitwise operators.

BitwiseOperators.java
1public class BitwiseOperators {
2 public static void main(String[] args) {
3 int a = 6; // binary: 0110
4 int b = 3; // binary: 0011
5
6 System.out.println("a & b: " + (a & b)); // binary: 0010 -> decimal: 2
7 System.out.println("a | b: " + (a | b)); // binary: 0111 -> decimal: 7
8 System.out.println("a ^ b: " + (a ^ b)); // binary: 0101 -> decimal: 5
9 System.out.println("~a: " + (~a)); // binary: ...11111111111111111111111111111010 -> decimal: -7
10 System.out.println("a << 1: " + (a << 1)); // binary: 1100 -> decimal: 12
11 System.out.println("a >> 1: " + (a >> 1)); // binary: 0011 -> decimal: 3
12 }
13}
Output
a & b: 2
a | b: 7
a ^ b: 5
~a: -7
a << 1: 12
a >> 1: 3

Practical Example

Let's create a complete Java program that uses various operators to calculate the area and perimeter of a rectangle.

Rectangle.java
1public class Rectangle {
2 public static void main(String[] args) {
3 double length = 10.5;
4 double width = 5.2;
5
6 double area = length * width;
7 double perimeter = 2 * (length + width);
8
9 System.out.println("Area of the rectangle: " + area);
10 System.out.println("Perimeter of the rectangle: " + perimeter);
11 }
12}
Output
Area of the rectangle: 54.6
Perimeter of the rectangle: 31.4

Summary

TypeDescription
ArithmeticPerform basic mathematical operations
AssignmentAssign values to variables
ComparisonCompare two values and return a boolean result
LogicalCombine multiple conditions
BitwisePerform operations on the binary representation of numbers

Understanding and effectively using these operators is fundamental to writing efficient and effective Java programs. Each operator serves a specific purpose, and mastering them will greatly enhance your programming skills.

What's Next?

In the next tutorial, we will explore Java Strings, which are used to represent sequences of characters. Strings are essential for handling textual data in Java applications. Make sure you have a solid grasp of operators before moving on to learn about strings.

Stay tuned for more tutorials and happy coding!


PreviousJava Type CastingNext Java Strings

Recommended Gear

Java Type CastingJava Strings