Tuesday, September 18, 2018

Operators in Java Programming

What are operators?
In a very short and descriptive definition: the operators help us to manipulate the 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 the name suggests, arithmetic operators perform the basic arithmetic operations.
Operator
Function
+
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
1
2
3
4
total = a + b;
product = a * b;
result = a / b;
average = (a +b) / 2;


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-
1
2
counter++; //this increments the variable counter by 1
counter--; //this decrements the variable counter by 1
The above statements are equivalent to saying:
1
2
counter = counter + 1;
counter = counter - 1;

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:
1
2
3
4
x++; //will increment x to 6
++x; //will increment x once again to become 7
--x; //will decrement x to 6
x--; //has exactly the same effect like --x;

To understand the difference, consider the following statements starting from x=5:
1
2
3
4
y = x++; //assign then increment –-> y=5 and x=6
y = ++x; //increment then assign --> x=7 and y=7
y = x--; //assign then decrement --> y=7 and x=6
y = --x; //decrement then assign --> x=5 and y=5
I think it is clear now.

Assignment Operators
Besides to the normal assignment operator = , Java has the following list of assignment operators:
Operator
Function
+=
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:

1
total = total + x;
Using the assignment operators, this statement could be re-written as follows:

1
total += x;
Comparison Operators
The comparison operators are essential for decision making. Expressions that use comparison operators are called relational expressions. Its result is true or false.
Operator
Function
< 
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.

Logical Operators
Two or more relational expressions can be combined with the logical operators. Java supports three basic logical operators that represent the software development equivalent to the three basic logical ports: AND, OR and NOT.
Operator
Function
&&
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.

1
2
3
4
5
6
7
8
9
10
11
12
import java.util.Scanner;
public class CompareNumbers
{
    public static void main(String[] args)
    {
        System.out.print("\nEnter a number between 1 and 10: ");
        float num = new Scanner(System.in).nextFloat();
        boolean c1 = num > 0;
        boolean c2 = num <=10;
        System.out.println(c1 + " AND " + c2 + " = " + (c1 && c2));
    }
}
When executed, the program should behave like the following:

And if the number is in the correct range:

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.
1
2
3
4
5
6
7
8
9
10
public class StringConcat
{
 public static void main(String []args)
 {
 String firstname = "Mohamed";
 String lastname = "Ali";
 String fullname = firstname + " " + lastname;
 System.out.println("Full Name: " + fullname);
 }
}
When executed it should print the message: Full Name: Mohamed Ali

Summary
In this article, we talked about operators.
• Java has a rich list of operators of different types: arithmetic operators, assignment, increment / decrement, comparison, logic and chain concatenation.
• Comparison and logical operators are essential for decision making.



No comments:

Post a Comment

From Java 8 to Java 11

Switching from Java 8 to Java 11 is more complicated than most updates. Here are some of my notes on the process. Modules Java 9 i...