Sitemap

Operators in Java

4 min readJun 5, 2024

--

Java Operators: An In-Depth Guide

Java, being a robust and versatile programming language, provides a rich set of operators to perform various operations on variables and values. Understanding these operators is crucial for writing efficient and effective Java programs. This article will explore the different types of operators in Java, providing examples and explanations for better understanding.

1. Arithmetic Operators

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

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

Example:

int a = 10;
int b = 20;
System.out.println("Addition: " + (a + b)); // Output: 30
System.out.println("Subtraction: " + (a - b)); // Output: -10
System.out.println("Multiplication: " + (a * b)); // Output: 200
System.out.println("Division: " + (b / a)); // Output: 2
System.out.println("Modulus: " + (b % a)); // Output: 0

2. Unary Operators

Unary operators operate on a single operand. They are used for incrementing, decrementing, negating, or inverting a value.

OperatorDescriptionExample+Unary plus+a-Unary minus-a++Incrementa++--Decrementa--!Logical complement!a

Example:

int a = 10;
System.out.println("Unary Plus: " + (+a)); // Output: 10
System.out.println("Unary Minus: " + (-a)); // Output: -10
System.out.println("Increment: " + (++a)); // Output: 11
System.out.println("Decrement: " + (--a)); // Output: 10
System.out.println("Logical Complement: " + (!true)); // Output: false

3. Assignment Operators

Assignment operators are used to assign values to variables. The most basic is the = operator, but there are also compound operators for arithmetic and bitwise operations.

OperatorDescriptionExample=Simple assignmenta = b+=Addition assignmenta += b-=Subtraction assignmenta -= b*=Multiplication assignmenta *= b/=Division assignmenta /= b%=Modulus assignmenta %= b

Example:

int a = 10;
int b = 5;
a += b; // a = a + b
System.out.println("Addition Assignment: " + a); // Output: 15
a -= b; // a = a - b
System.out.println("Subtraction Assignment: " + a); // Output: 10
a *= b; // a = a * b
System.out.println("Multiplication Assignment: " + a); // Output: 50
a /= b; // a = a / b
System.out.println("Division Assignment: " + a); // Output: 10
a %= b; // a = a % b
System.out.println("Modulus Assignment: " + a); // Output: 0

4. Relational Operators

Relational operators compare two values and determine the relationship between them. They return a boolean result.

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

Get Tannu kumari’s stories in your inbox

Join Medium for free to get updates from this writer.

Example:

int a = 10;
int b = 20;
System.out.println("Equal to: " + (a == b)); // Output: false
System.out.println("Not Equal to: " + (a != b)); // Output: true
System.out.println("Greater than: " + (a > b)); // Output: false
System.out.println("Less than: " + (a < b)); // Output: true
System.out.println("Greater than or Equal to: " + (a >= b)); // Output: false
System.out.println("Less than or Equal to: " + (a <= b)); // Output: true

5. Logical Operators

Logical operators are used to perform logical operations, combining multiple boolean expressions or values.

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

Example:

boolean a = true;
boolean b = false;
System.out.println("Logical AND: " + (a && b)); // Output: false
System.out.println("Logical OR: " + (a || b)); // Output: true
System.out.println("Logical NOT: " + (!a)); // Output: false

6. Bitwise Operators

Bitwise operators perform operations on individual bits of integer values.

OperatorDescriptionExample&Bitwise ANDa & b|Bitwise ORa | b^Bitwise XORa ^ b~Bitwise complement~a<<Left shifta << 2>>Right shifta >> 2>>>Unsigned right shifta >>> 2

Example:

int a = 5; // 0101 in binary
int b = 3; // 0011 in binary
System.out.println("Bitwise AND: " + (a & b)); // Output: 1 (0001 in binary)
System.out.println("Bitwise OR: " + (a | b)); // Output: 7 (0111 in binary)
System.out.println("Bitwise XOR: " + (a ^ b)); // Output: 6 (0110 in binary)
System.out.println("Bitwise Complement: " + (~a)); // Output: -6 (in binary: 11111111111111111111111111111010)
System.out.println("Left Shift: " + (a << 1)); // Output: 10 (1010 in binary)
System.out.println("Right Shift: " + (a >> 1)); // Output: 2 (0010 in binary)
System.out.println("Unsigned Right Shift: " + (a >>> 1)); // Output: 2 (0010 in binary)

7. Ternary Operator

The ternary operator is a shorthand for the if-else statement. It takes three operands and is used to evaluate boolean expressions.

OperatorDescriptionExample? :Ternary operatorcondition ? a : b

Example:

int a = 10;
int b = 20;
int result = (a > b) ? a : b;
System.out.println("The greater value is: " + result); // Output: The greater value is: 20

Conclusion

Operators are fundamental building blocks in Java programming. They enable us to perform various operations on data, making our code functional and expressive. Understanding the different types of operators and how to use them effectively is essential for any Java developer.

ऑपरेटर क्या होते हैं?

जावा में ऑपरेटर वे सिंबल्स होते हैं जो हमें वेरिएबल्स और वैल्यूज़ पर विभिन्न प्रकार के ऑपरेशंस (क्रियाएँ) करने की सुविधा देते हैं। यह हमें मैथमैटिकल (गणितीय), लॉजिकल (तार्किक), रिलेशनल (संबंधित) और बिटवाइज (बिट स्तर) ऑपरेशंस को आसानी से लागू करने में मदद करते हैं। उदाहरण के लिए, “+” ऑपरेटर दो संख्याओं को जोड़ता है, “&&” ऑपरेटर दो लॉजिकल कंडीशंस को चेक करता है कि वे दोनों सही हैं या नहीं।

इस आर्टिकल में हमने जावा के विभिन्न प्रकार के ऑपरेटर्स जैसे कि Arithmetic, Unary, Assignment, Relational, Logical, Bitwise, और Ternary Operators को विस्तार में समझा। हर प्रकार के ऑपरेटर का उपयोग कैसे किया जाता है, यह उदाहरणों के माध्यम से स्पष्ट किया गया है।

--

--