A blog for computer science passionates.

Sunday 1 March 2020

Java: Operators Part-1

Hello Friends!!!

In this article, we are going to learn different operators in Java to work on different operands.
What are the Operators?
Operators are the symbols to perform different operations on operands (values or variables).
There are three different types of operators available in Java.
  • Unary
  • Binary
  • Ternary
Unary operators work on one operand (variable) only. Increment/ decrement operators are unary operators.
Increment Operators: These types of operators increase the value of a variable with one value only.
Decrement Operators: These types of operators decrease the value of a variable with one value only.
It has two different types:
  • Prefix Increment/Decrement
    • For example, ++a Increases the value of a with one value
    • --a Decreases the value of a with one value.
  • Postfix Increment/Decrement
    • For example, a++ Increases the value of a with one value
    • a-- Decreases the value of a with one value.
Difference between prefix/postfix: When we work with a prefix for example if we write ++a it means to increase the value of a first then work for the next step, and when we work with postfix for example if we write a++ it means to perform the operation with current value then increase the value of a.

Binary operators work on two operands (variable/value). There are lots of different binary operators available in Java. Those are,

=
Assignment Operator
a=b (a=5)
Arithmetic Operators
+
Addition
a+b
-
Subtraction
a-b
*
Multiplication
a*b
/
Division
a/b
%
Modulus
a%b
+=
Addition assignment
a+=b
-=
Subtraction assignment
a-=b
*=
Multiplication assignment
a*=b
/=
Division assignment
a/=b


Example of Arithmetic Operator,
The output of the above program,
Fig. The output of the Arithmetic Operators

In the next article, we will learn more operators. Till then try with arithmetic operators.
Happy Coding...☺😊

No comments:

Post a Comment