Tuesday - 18 March, 2025 +91

Operators in Java


Welcome again to Java session after a long gap. In today’s session we are going to deal with various types of operators in Java.

Let us assume one statement c=a+b. In the above statement we can say that variables a,b and c are operands. Whereas + and = are operators.

An operator is a one which is going to perform some action on operands.

Here + is used to add a and b and again = is used to assign the result to variable c.

But here there is a catch. In mathematics some times we may even write a+b=c and c=a+b but in Java you are not supposed to write a+b=c because a+b contains some value. And we cannot assign some variable to value. But we can assign a value to a variable. I mean c=a+b is just fine. I think this is clear to you.

Arithmetic Operators:

Java programming language provides operators that perform addition, subtraction, multiplication, and division. These operators are similar to the operators found in basic mathematics. The only symbol that might look new to you is %, which divides one operand by another and returns the remainder as its result.

+ (Addition Operator, also used to add two strings)
- (Subtraction operator)
* (Multiplication operator)
/ (Division operator)
% (Remainder operator)

Assignment Operator:

The assignment operator available in Java programming language is =. We already know that the purpose of this operator is to assign a value to a variable.

Compound assignment operators are also there in Java as like in C. For instance if you want to increment a variable value by 10, we generally write a=a+10. The same statement can also written as a+=10. So we can say that +=, -=, *=, /=, %= can be called as compound assignment operators (there are many more!).

Unary Operators:

The unary operators require only one operand; they perform various operations such as incrementing/decrementing a value by one, negating an expression, or inverting the value of a boolean.

+ (Unary plus operator. This indicates positive value)
- (Unary minus operator)
++ (Increment operator; increments a value by 1)
– (Decrement operator; decrements a value by 1)
! (Logical complement operator; inverts the value of a boolean)

Equality and Relational Operators:

== (Equal to)
!= (Not equal to)
> (Greater than)
>= (Greater than or equal to)
< (Less than) <= (Less than or equal to) Conditional Operators: && (Conditional-AND) || (Conditional-OR) ?: (Ternary operator this is a shorthand for if-then-else statement) Bitwise and Bit Shift Operators: ~ (Unary bitwise complement) << (Signed left shift) >> (Signed right shift)
>>> (Unsigned right shift)
& (Bitwise AND)
^ (Bitwise exclusive OR also known as XOR)
| (Bitwise inclusive OR)

We will cover these operators in more depth when the need arises. Let me know about your feedback regarding today’s session by commenting below.

0 comments:

Post a Comment