Learn about Operators in Java Programming

0
2110
Java (6) - Using Operators

Java (6) - Using Operators

As I told you in the conclusion part of the previous article: After learning the variables and data types, we should know what to do with those variables. Operators are going to give us the answer.

What are Operators?
In a very short and descriptive definition: Operators help us manipulate data stored in variables.
As in other programming languages, Java supports several types of operators: Arithmetic operators, Increment/Decrement operators, Assignment operators, Comparison operators, and Logical operators.

Arithmetic Operators
As their name suggests, arithmetic operators perform the basic arithmetic operations.

OperatorFunction
+Adds two numbers.
Subtracts the right side operand from the left side one.
*Multiplies two numbers.
/Divides the left side operand by the right side one.
%Divides the left side operand by the right side one and returns the remainder.

Examples

Increment / Decrement Operators
Consider the case when we need to increment or decrement a variable by one. This can be easily achieved using the Increment operators ++ and the Decrement operator —

The above statements are equivalent to saying:

Post and Pre Increment / Decrement
Both the increment ++ and the decrement — operators can be used before or after the variable. In a case like the above one, using the operator before or after the variable won’t make a difference.

Given that x = 5:

To understand the difference, consider the following statements starting from x=5:

I think it is clear now.

Assignment Operators
Besides to the normal assignment operator = , Java has the following list of assignment operators:

OperatorFunction
+=Adds the right side operand to the left side one, and stores the result in the left side operand.
-=Subtracts the right side operand from the left side one, and stores the result in the left side operand.
*=Multiplies the two operands and stores the result in the left side one.
/=Divides the left side operand by the right side one, and stores the result in the left side operand.
%=Calculates the remainder of the division operation and assigns it to the left side operand.

Operators of this type are useful when the value of a variable is being manipulated and the result should be assigned to that variable. Consider the following case:

Using the assignment operators, this statement could be re-written as follows:

Comparison Operators
The Comparison operators are essential for Decision Making. Expressions that use comparison operators are called Relational Expressions. Their result is either true or false.

OperatorFunction
<Returns true if the left side operand is less than the right side one.
<=Returns true if the left side operand is less than or equal to the right side one.
>Returns true if the left side operand is greater than the right side one.
>=Returns true if the left side operand is greater than or equal to the right side one.
==Returns true if both operands are equal.
!=Returns true if the two operands are not equal.

Java Programming Course for Beginner From Scratch

Logical Operators
Two or more relational expressions can be combined using the Logical Operators. Java supports three basic logical operators that represent the software-developing equivalent to the three basic Logic Gates: AND, OR, and NOT.

OperatorFunction
&&The logical AND operator. It returns true only if both operands are true.
||The logical OR operator. It returns true if any of its operands is true.
!The logical NOT operator. It inverts the value of its operand.
If the input is true, the output will be false, and vice versa.

Example
The following program will prompt the user to enter a number between 0 and 10. The program will print true if the input is in the specified range. If not, the program will print false.

When executed, the program should behave like the following:
1

And if the number is in the correct range:
2

String Concatenation Operator
Besides its function as an Arithmetic operator for Addition, the + operator can be used to concatenate two strings together.

Example
The following program concatenates two String variables and stores the resulting string in a third variable.

When executed it should print the message: Full Name: Mohamed Ali
3

Summary
In this article, we have talked about Operators.

  • Java has a rich list of operators from different types: Arithmetic, Assignment, Increment / Decrement, Comparison, Logical, and String Concatenation operators.
  • Comparison and Logical operators are essential for Decision Making.

After possessing the necessary tools, we are now ready to go an important step further. So, our next article will be on Decision Making; an important topic that shouldn’t be missed. See you there.

LEAVE A REPLY

Please enter your comment!
Please enter your name here