Operators in Java

Operators in Java

Hey all! Let's know about some operators in java. Today in this article we will see operators which will help to program the java code.

What are the roles of the operators? Operators are used to performing operations on variables and values to get the desired output. Eg:

int x = 5 + 4;

In the above example, we have used the operator + to do an addition operation. Based on operations, operators are divided into the following groups:

Arithmetic Operators: These operators are used to perform basic mathematical operations. Following are arithmetic operators.

  • Addition operator ----> + ----> Adds two values ----> Eg: a + b
  • Subtraction operator ----> - ---> Subtract one values from another ----> Eg: a - b
  • Multiplication operator ----> * ----> Multiply two values -----> Eg: a * b
  • Division operator ---->/ ---->Divide one value by another ----> Eg: a / b
  • Modulus operator ----> % ----> Returns the division remainder----> Eg: a % b
  • Increment operator ----> ++ ----> Increase the value by one ----> Eg: a++
  • Decrement operator ----> -- ----> Decrease the value by one ----> Eg: a--

Assignment Operators: These operators are used to assign values to variables. You may have come through a code statement like int x = 5;, this = operator is called assignment operator which symbolizes that 5 value is stored (or assigned) to a variable named by x. Based on this operator the are many assignment operators which are as follows :

  • Assignment Operator ---->=---->Assign value to variable ---->Eg: a = 5;
  • Addition assignment ----> += ----> Similar to addition operation ----> Eg: a+=5 i.e a= a+5
  • Subtraction assignment ----> -= ----> Similar to subtraction operation ----> Eg: a-=5 i.e a= a-5
  • Multiplication assignment ----> += ----> Similar to multiplication operation ----> Eg: a=5 i.e a= a5
  • Division assignment ----> /= ----> Similar to division operation ----> Eg: a/=5 i.e a= a/5
  • Modulus assignment ----> /= ----> Similar to modulus operation ----> Eg: a%=5 i.e a= a % 5

Comparison Operators: These operators are used to compare two values. These operators are as follows. :

  • Equal to ----> == ----> To check if two values are equal ----> Eg: x == y
  • Not Equal to ----> != ----> To check if two values are not equal ----> Eg: x != y
  • Less than ----> < ----> To check if one value is less than another ----> Eg: x < y
  • Greater than ----> < ----> To check if one value is greater than another ----> Eg: x > y
  • Less than equal to ----> <= ----> To check if one value is less or equal to another ----> Eg: x <= y
  • Greater than equal to ----> >= ----> To check if one value is greater or equal to another ----> Eg: x >= y

Logical Operator: Logical Operators are used to determining the logic between values or variable. Following are the logical operators:

  • Logical AND ---> &&---> Returns true if both statement are true ---> Eg: x > 10 && x < 20
  • Logical OR ---> ||---> Returns true if one of the statement is true ---> Eg: x > 10 || x < 20
  • Logical NOT ---> !---> Returns true if the result is false i.e it reverse the output ---> Eg: !( x > 10 && x < 20 )

Actually, there are many operators but these mentioned operators are used mostly in programming. Hope you have got today's concept. Thanks for your time. Happy Programming!

Tip: It is not compulsory to remember all these operators. Practising these operators will help you to remember them.

Did you find this article valuable?

Support Soham Dnyaneshwar Dixit by becoming a sponsor. Any amount is appreciated!