Recent in Sports

Breaking News

Understanding Operators in Java

In Java, operators are special symbols or keywords used to perform operations on operands (variables or values). Understanding how to use operators is essential for performing calculations, making comparisons, and implementing logical conditions in your Java programs.

Here’s a breakdown of the different types of operators available in Java:


1. Arithmetic Operators

Arithmetic operators are used for basic mathematical calculations such as addition, subtraction, multiplication, and division.

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

Example:

java

int a = 10, b = 5; System.out.println(a + b); // 15 System.out.println(a - b); // 5 System.out.println(a * b); // 50 System.out.println(a / b); // 2 System.out.println(a % b); // 0

2. Relational (Comparison) Operators

These operators compare two values and return a boolean (true or false), helping in making decisions.

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

Example:

java

int a = 10, b = 5; System.out.println(a == b); // false System.out.println(a != b); // true System.out.println(a > b); // true System.out.println(a < b); // false System.out.println(a >= b); // true System.out.println(a <= b); // false

3. Logical Operators

Logical operators are used to combine multiple conditions and return a boolean value.

OperatorDescriptionExample
&&Logical ANDa > b && b < c
``
!Logical NOT!(a > b)

Example:

java

boolean a = true, b = false; System.out.println(a && b); // false System.out.println(a || b); // true System.out.println(!a); // false

4. Unary Operators

Unary operators operate on a single operand to perform operations like incrementing, decrementing, and negating values.

OperatorDescriptionExample
++Incrementa++ or ++a
--Decrementa-- or --a
+Unary plus+a
-Unary minus-a
!Logical NOT!a

Example:

java

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

5. Assignment Operators

Assignment operators are used to assign values to variables, including shorthand operators for addition, subtraction, etc.

OperatorDescriptionExample
=Assign valuea = b
+=Add and assigna += b
-=Subtract and assigna -= b
*=Multiply and assigna *= b
/=Divide and assigna /= b
%=Modulus and assigna %= b

Example:

java

int a = 10; a += 5; // a = a + 5 System.out.println(a); // 15

6. Bitwise Operators

Bitwise operators operate on the binary representation of numbers and perform bit-by-bit operations.

OperatorDescriptionExample
&ANDa & b
``OR
^XORa ^ b
~NOT~a
<<Left shifta << 2
>>Right shifta >> 2
>>>Unsigned right shifta >>> 2

Example:

java

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

7. Ternary Operator

The ternary operator is a shorthand way of writing if-else statements.

OperatorDescriptionExample
? :Conditional (if-else)condition ? value1 : value2

Example:

java

int a = 10, b = 5; int max = (a > b) ? a : b; System.out.println(max); // 10

8. Type Comparison Operator

This operator checks whether an object is an instance of a particular class or interface.

OperatorDescriptionExample
instanceof  Type comparison      obj instanceof MyClass

Example:

java

String str = "Hello"; System.out.println(str instanceof String); // true


No comments