Java04: Operator

Java has different set of operators to manipulate variables and a get new value. These operators can be grouped as arithmetic operators, relational operators, logical operators and assignment operators.

Most frequently used operator is assignment operator. Symbol used for assignment operator is the '=' sign. Assignment operator is a binary operator, an operator that operates on two operands. Unary operator only has one operand. Syntax for assignment operation is;

variable = value;

assigns value on right side of the assignment operator to variable on the left.

For example,

age = 29;
color = "red";

A more general syntax for assignment operator is:

variable = expression;

where expression is a construct made up of variables, operators and method invocation, which are constructed according to the syntax of language, that evaluates to a single value.

For example,
average = (5 + 2 + 4)/3;
total = add(2, 3);

Arithmetic Operators

Consist of operators that performs operations like addition, subtraction, multiplication and division. these operators are all binary operators. Following table summarize arithmetic operators.

OperationOperator
Addition
+
Subtraction
-
Multiplication
*
Division
/
Remainder
%

Syntax for addition operation is as follows:

operand_1 + operand_2

This statement adds operand_1 and operand_2 and returns addition result. To store the result of this operation assignment operators are often used.

variable = operand_1 + operand_2;

for example:

total = 3 + 5;

after this statement is evaluated, value of total is 8.

Subtraction operator subtracts amount of value specified in operand_1 from value of operand_2. For example:

balance = 12.5 - 10.3;

this expression subtracts 10.3 from 12.5, and assigns the result to variable called balance. Value of balance is 2.2.

Multiplication operator multiplies operand_1 by operand_2 times and vise versa. For example:

product = 12 * 5;

this expression multiplies 12 by 5 times and assigns the result to variable called product. Value of product is 60.

Finally, lets look at division operation.

variable = operand_1/ operand_2;

where operand_ 1 is dividend, operand_2 is divisor and variable is the quotient. For example,

quotient = 6 / 2;

in this expression, dividend value is 6, and divisor is 2. The result of this division is assigned to variable called quotient, where the value will be 3.

sometimes, division operation has a remainder. To get the remainder of division operation, the remainder operator is used.

For example, remainder of division operation is previous example can be obtained by changing the division operator to remainder operator like below:

remainder = 6 % 2;

the value of remainder is 0 because, 6 can be divided evenly by 2. If we change the dividend to 5.

remainder = 5 % 2;

the value of remainder will be 1.

Short hand operators

There are often value of variable is used in a calculation and result of this calculation is stored back into same variable.

var = var + 3;

current value of var is used in addition operation. After adding 3 to whatever value of var, the result is stored into variable var. this will replace new value with old value. Assuming the value of var is 0, after this statement is evaluated, new value of var will be 3.

Java supports following statement that does the same thing

var += 3;

This is known as shorthand operators. Other arithmetic operators, also has shorthand operator as listed below:

OperationOperator
Addition Assignment
+=
Subtraction Assignment
-=
Multiplication Assignment
*=
Division Assignment
/=
Remainder Assignment
%=

Increment and Decrement operators

These operator is used to increment or decrement a value by 1. Both are unary operator, has one operands only.

operator operand;

example for increment operation

++index;

which also means, index = index + 1. If initial value of index is 5, after this statement evaluated, value of index is 6.

example for decrement operation

--index;

which also means, index = index - 1. If initial value of index is 5, after this statement evaluated, value of index is 4.

Relational operators compares two operands for some relationship. It compares its values equality, one is greater or less than the other or not equal. The result of this expression is a Boolean value. Result returned is true, if relationship expressed is met and false if otherwise. Following is list of relational operators.

operand_1 relational_operator operand_2
OperationOperators
More than
>
Less than
<
More than or equal to
>=
Less than or equal to
<=
Equal to
==
Not equal to
!=

More than relational operator, if operand_1 is more than operand_2 then result is True, otherwise result is False.

boolean a = (5 > 4);

this expression 5 is more than 4 evaluates to True, thus a is True. this expression is always True as long as operand_1 is bigger than 4. How about when operand_1 is equals to operand_2, in this example when operand_1 is 4.

boolean a = (4 > 4);

The result is False. All value less than and including 4 for operand_1 will result is False. If we need to include 4 as well to result in True, More than or equal to relational operator need to be used.

boolean a = (4 >= 4);

Now, value of a is true.

Less than relational operator is opposite to previous operator. If operand_1 is less than operand_2 then result is True, otherwise result is False.

boolean a = (5 < 4);

This expression results in False, thus a is False. Operand_1 has larger value compare to operand_2. For this expression to become True, operand_1 must be less than 4.

boolean a = (4 <= 4);

This expression is True. If operand_1 is less than or equals to operand_2 than result is True otherwise False.

To determine equality of two operands, equals to relational operator is used. If operand_1 is equal to operand_2 than True, otherwise False.

boolean a = (3 == 4);

this expression is false. because both operands has different values.

to determine that both operands are not equal, not equal to relational operator is used. If operand_1 is not equal to operand_2 than True, otherwise False.

boolean a = (3 != 4);

this expression is True. because both operands has different values.

Logical Operators

Logical operator takes two operands. This set of operator only takes both operands of Boolean values.

operand_1 logical_operator operand_2
OperationOperators
Logical AND
&&
Logical OR
||
Logical NOT
!

for AND operation both operand_1 and operand_2 must be True for the expression evaluates to True. If one of the operand is false, the expression is False.

For example,

boolean a = True , b = false;
boolean c = a && b;

c is false because b is false. in order for c to be true , both a and b must be True.

Next, OR operation, if both or either operand is true, expression evaluates to True. Only becomes False, if both operand is false.

boolean a = True , b = false;
boolean c = a || b;

c is True because at least one of operand is true. Variable c, becomes False is both a and b are false.

Finally not operator is used to reverse logical state of Boolean operand. If the value o operand is True, using this operand, value is changed to False.

For example,

boolean a = True;
boolean b = !a;

The value of b is False.

A post by Cuber

Comments

Popular posts from this blog

Drawing Simple Pie Chart

VB.net connecting to SQL Server

VB.net