Operators and Expressions
An
operator is a symbol that tells the computer to perform certain mathematical or
logical manipulations. Operators are used in program to manipulate data and
variables. The data item that operators act upon is called operands.
Types of Operators:
1.
Arithmetic
Operators
2.
Relational
Operators
3.
Logical
Operators
4.
Assignment
Operators
5.
Increment
and Decrement Operators
6.
Conditional
Operators
7.
Bitwise
Operators
8.
Special
Operators
Arithmetic
Operators:
Arithmetic
operators are ( +, -, *, /, % )
Java Symbol
|
Meaning
|
Usage
|
+
|
addition
|
a + b
|
-
|
subtraction
|
a - b
|
*
|
multiplication
|
a * b
|
/
|
Division
|
a / b
|
%
|
Modulo
|
a % b
|
Operations of
addition, subtraction, multiplication and division literally correspond with
their respective mathematical operators. The only one that you might not be so
used to see is modulo; whose operator is the percentage sign (%).
Modulo is the operation that gives the remainder of a division of two values.
For example, if we write:
a=11%3;
the variable a
will contain the value 2, since 2
is the remainder from dividing 11 between 3.
Relational
Operators
Relational and
equality operators ( ==, !=, >, <, >=, <= )
In order to evaluate
a comparison between two expressions we can use the relational and equality
operators. The result of a relational operation is a Boolean value that can
only be true or false, according to its Boolean result.
We may want to compare two expressions, for example, to know if they are equal or if one is greater than the other is.
We may want to compare two expressions, for example, to know if they are equal or if one is greater than the other is.
Java Symbol
|
Meaning
|
Usage
|
==
|
Equal to
|
if(a==b)
|
!=
|
Not equal to
|
if(a!=b)
|
>
|
Greater than
|
if(a>b)
|
<
|
Less than
|
if(a<b)
|
>=
|
Greater than or
equal to
|
if(a>=b)
|
<=
|
Less than or equal
to
|
if(a<=b)
|
Logical
Operators
Logical
operators ( !, &&, || )
The Operator ! is the Java operator to perform the Boolean operation NOT, it has
only one operand, located at its right, and the only thing that it does is to
inverse the value of it, producing false if its operand is true and true if its
operand is false. Basically, it returns the opposite Boolean value of
evaluating its operand.
The
logical operators && and || are used when evaluating two expressions to obtain a single
relational result. The operator &&
corresponds with Boolean logical operation AND. This operation results true if
both its two operands are true, and false otherwise. The following panel shows
the result of operator &&
evaluating the expression a && b:
A
|
b
|
a && b
|
true
|
true
|
true
|
true
|
false
|
false
|
false
|
true
|
false
|
false
|
false
|
false
|
The
operator || corresponds with Boolean logical operation OR. This operation
results true if either one of its two operands is true, thus being false only
when both operands are false themselves. Here are the possible results of a || b:
A
|
b
|
a || b
|
true
|
true
|
true
|
true
|
false
|
true
|
false
|
true
|
true
|
false
|
false
|
false
|
Assignment
Operators
Assignment (=)
The
assignment operator assigns a value to a variable.
a=5;
This statement assigns the integer value 5 to the variable a. The lvalue has to be a variable whereas the rvalue can be either a constant, a variable, the result of an operation or any combination of these.
This statement assigns the integer value 5 to the variable a. The lvalue has to be a variable whereas the rvalue can be either a constant, a variable, the result of an operation or any combination of these.
a=b;
This statement assigns to variable a (the lvalue) the value contained in variable b (the rvalue).
This statement assigns to variable a (the lvalue) the value contained in variable b (the rvalue).
Increment
and Decrement Operators
The
operator ++ adds 1 to the operand while –- subtracts 1. Both are unary
operators and are used in the following form:
++m
or m++;
--m
or m--;
Conditional
Operators
The conditional operator evaluates an expression returning a value
if that expression is true and a different one if the expression is evaluated
as false. Its format is:
condition ? result1 : result2
If condition is true the expression will return result1, if it is
not it will return result2.
7==5 ? 4 : 3 // returns 3, since 7 is not equal to 5.
7==5+2 ? 4 : 3 // returns 4, since 7 is equal to 5+2.
5>3 ? a : b // returns the value of a, since 5 is greater than 3.
a>b ? a : b // returns whichever is
greater, a or b.
Bitwise
Operators
These
operators are used for manipulation of data at values of bit level. These
operators are used for testing the bits, or shifting them to the right or left.
Bitwise operators may not be applied to float or double.
Operator Meaning
& Bitwise
AND
! Bitwise
OR
^ bitwise
exclusive OR
~ one’s
complement
<< shift
left
>> shift
right
>>> shift
right with zero fill
Special
Operators
Java supports some
special operators of interest such as instanceof operator and member
selection operator (.)
- instanceof operator
The instanceof is an object reference
operator and returns true, if the object on the left-hand side is an instance
of the class given on the right-hand side. This operator allows us to determine
whether the object belongs to a particular class or not.
Ex: person instanceof student
is true
if the object person belongs to the class student; otherwise it is false.
- member selection operator
The dot operator(.) is used to
access the instance variables and methods of class objects:
Person1.age //reference to variable
age
Person1.salary() //reference to method salary
No comments:
Post a Comment