Operators in Java

 

✅ Java Operators – Complete Guide

In Java, operators are special symbols used to perform operations on variables and values. They are grouped into several categories:


🔹 1. Arithmetic Operators

Used for basic mathematical operations.

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


🔹 2. Relational (Comparison) Operators

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

OperatorDescriptionExampleResult
==Equal toa == bfalse
!=Not equal toa != btrue
>Greater thana > btrue
<Less thana < bfalse
>=Greater than or equala >= btrue
<=Less than or equala <= bfalse

🔹 3. Logical Operators (used with boolean values)

OperatorNameDescriptionExample
&&Logical AND    True if both conditions are true(a > 0 && b > 0)true
||
Logical OR
 True if any one of the  conditions is true   (a > 0 || b > 0) → true   
!Logical NOTReverses the boolean result!(a > b)true if a < b
📝 Note: && and || use short-circuit evaluation:
  • A || B: If A is true, B is not evaluated.

  • A && B: If A is false, B is not evaluated.


🔹 4. Bitwise Operators (used with integer types for binary operations)

OperatorNameDescriptionExample (a = 5, b = 3)Result
&AND1 if both bits are 1a & b = 0101 & 0011 = 0001 → 1
|OR
1 if any one of  bit is 1
1 if at least one bit is 1`a
^XOR1 if bits are differenta ^ b = 0101 ^ 0011 = 0110 → 6
~NOTInverts bits~a = ~0101 = 1010
<<Left shiftShifts bits to the left (×2 per shift)a << 1 = 1010 = 10
>>Right shiftShifts bits to the right (÷2 per shift)a >> 1 = 0010 = 2
>>>Unsigned right shiftSame as >> but fills left with 0a >>> 1

🔹 5. Assignment Operators

Used to assign values or update a variable using an operation.

OperatorExampleMeaning
=a = bAssign b to a
+=a += ba = a + b
-=a -= ba = a - b
*=a *= ba = a * b
/=a /= ba = a / b
%=a %= ba = a % b
&=a &= ba = a & b
`=``a
^=a ^= ba = a ^ b
<<=a <<= ba = a << b
>>=a >>= ba = a >> b
>>>=a >>>= ba = a >>> b

🔹 6. Unary Operators

Operate on a single operand.

OperatorDescriptionExample
+Unary plus+a
-Unary minus-a
++Increment (prefix or postfix)++a, a++
--Decrement (prefix or postfix)--a, a--
!Logical NOT!true → false
~Bitwise NOT~a

🔹 7. Ternary Operator

A short-hand for if-else.


int max = (a > b) ? a : b;

This means:
If a > bmax = a, elsemax = b.


🔹 8. Type Comparison Operator

OperatorDescriptionExample
instanceofChecks object types instanceof Stringtrue

🔹 9. Other Special Operators

OperatorDescription
.Member access (dot)
[]Array access
()Method call
newObject creation
::Method reference (Java 8+)

Sample Code to Demonstrate Key Operators


public class OperatorDemo { public static void main(String[] args) { int a = 10, b = 5; // Arithmetic System.out.println("Addition: " + (a + b)); // Relational System.out.println("a > b: " + (a > b)); // Logical System.out.println("Logical OR: " + (a > 0 || b < 0)); // Bitwise System.out.println("Bitwise OR: " + (a | b)); System.out.println("Bitwise XOR: " + (a ^ b)); // Assignment a += 3; System.out.println("After a += 3: " + a); // Ternary int max = (a > b) ? a : b; System.out.println("Max = " + max); } }

Comments

Popular posts from this blog

Object Oriented Programming PBCST 304 KTU BTech CS Semester 3 2024 Scheme - Dr Binu V P

Object Oriented Programming PBCST 304 Course Details and Syllabus

Type casting in Java