Вы находитесь на странице: 1из 10

John Raymond Arriesgado August 23, 2019

W24 CCS0023

I. Types of Java Operators

1. Arithmetic Operators: this type of operator are used to perform simple mathematical
operations such as Addition (+), Subtraction(-), Multiplication(*), Division(/), and
Modulus (%)

Example:

Output:
2. Unary Operators: these are operators that manipulates a single value and these
includes:

Operator Name Description


Unary Minus (-) For decreasing the value
Unary Plus (+) For converting the negative values into positive
Increment operator (++) Used for increasing value of the operand by 1
Post-Increment The value is incremented first then the result is computed
Pre-Increment The value is incremented later first the result is computed
Decrement Operator Used for decreasing the value of operand by 1
Post-Decrement The value is decremented first then the result is computed
Pre-Decrement The value is decremented later first the result is computed
Logical not Operator (!) Used for inverting the values of boolean

Example:
Output:

3. Assignment Operators: this operator assigns a value to the left operand with based on
the value of the right operand and is done using an equal (=) sign. The following are the
types of an assignment operator.

Operator Name Description


To add the right and left operator and then assigning the result to the left
+=
operator
To subtract the two operands on left and right and then assign the value to the
-=
left operand
To multiply the two operands on left and right and then assign the value to the
*=
left operand
To divide the two operands on left and right and then assign the value to the left
/=
operand
^= To raise the value of left operand to the power of right operator
%= To apply modulus operator
Example:

Ouput:
4. Relational Operators: these operators are used to check the equality of operands as
well as compare two or more values.

Operator Name Example Description


== (equals to) x==y True if x equals y, otherwise false
!= (not equal to) x!=y True if x is not equal to y, otherwise false
< (less than) x<y True if x is less than y, otherwise false
> (greater than) x>y True if x is greater than y, otherwise false
>= (greater than or equal to) x>=y True if x is greater than or equal to y, otherwise false
<= (less than or equal to) x<=y True if x is less than or equal to y, otherwise false

Example:

Output:
5. Logical Operators: these operators are also known as Boolean operators due the fact
that they return Boolean values, true or false.

Operator Name Description


&& (Logical AND) Returns the value if both the conditions are true otherwise returns zero.
II (Logical OR) Returns the value even if one condition is true

Example:

Output:
6. Ternary Operator: this operator is a shorthand version of if-else statements and is done
by using the syntax: condition?value1: value2. Where first value i.e. value1 executes if
the condition is true and if the condition is false value2 executes.

Example:

Output:
7. Bitwise Operators: Bitwise operators are used to perform operations on single bitwise
values. First, it converts the numerical value to its binary representation and then it
performs the necessary operation and gives the result.

Operator Name Description


The & operator compares corresponding
Java Bitwise AND operator (&) bits of two operands. If both bits are 1, it
gives 1 else 0.
The | operator compares corresponding
Java Bitwise OR operator (|) bits of two operands. If either of the bits is
1, it gives 1 else 0.
The ^ operator compares corresponding
Bitwise XOR operator (^) bits of two operands. If corresponding bits
are different, it gives 1 else 0.
The ~ operator inverts the bit pattern. It
Bitwise Complement operator (~)
makes every 0 to 1 and every 1 to 0.

Example:
Output:

8. Shift Operators: The bitwise shift operators take two operands: the first is a quantity to
be shifted, and the second specifies the number of bit positions by which the first
operand is to be shifted. The direction of the shift operation is controlled by the
operator used. Shift operators convert their operands to 32 or 64 bits and return a
result of the same type as the left operator.

Operator Name Description


It shifts x in binary representation y bits to the left,
<< (left shift operator)
shifting in zeros from the right.
It shifts x in binary representation y bits to the right,
>> ( right shift operator)
discarding bits shifted off.
It shifts x in binary representation y bits to the right,
>>> (unsigned right shift
discarding bits shifted off, and shifting in zeros from the
operator)
left.

Example:
Output:

Вам также может понравиться