You are on page 1of 6

Ex

pr
es
si
on
s

The Arithmetic Operators:


Arithmetic operators are used in mathematical expressions in the
same way that they are used in algebra.
The Java programming language provides operators that perform
addition, subtraction, multiplication, and division. There's a good
chance you'll recognize them by their counterparts in basic
mathematics. The only symbol that might look new to you is "%",
which divides one operand by another and returns the remainder
as its result.

Operator

Description

Additive operator (also used for


String concatenation)

Subtraction operator

Multiplication operator

Division operator

Remainder operator

The Relational Operators:


Relational operators determine if one operand is greater than, less than, equal to, or not
equal to another operand. The majority of these operators will probably look familiar to you
as well. Keep in mind that you must use "==", not "=", when testing if two primitive values
are equal.
Operator

Description

== (equal to)

Checks if the values of two operands


are equal or not, if yes then
condition becomes true
Example: (A == B) is not true.

!= (not equal to)

> (greater than)

Checks if the values of two operands


are equal or not, if values are not
equal then condition becomes true.
Example: (A != B) is true.
Checks if the value of left operand is
greater than the value of right
operand, if yes then condition
becomes true.
Example: (A > B) is not true.

< (less than)

Checks if the value of left operand is


less than the value of right operand,
if yes then condition becomes true.
Example: (A < B) is true.

>= (greater than or equal to)

Checks if the value of left operand is


greater than or equal to the value of
right operand, if yes then condition
becomes true.
Example (A >= B) is not true.

<= (less than or equal to)

Checks if the value of left operand is


less than or equal to the value of
right operand, if yes then condition
becomes true.
example(A <= B) is true.

The Logical Operators


Logical operators are typically used with Boolean (logical) values. When they are, they
return a Boolean value. However, the && and || operators actually return the value of one of
the specified operands, so if these operators are used with non-Boolean values, they may
return a non-Boolean value.
Operator

Description

&& (logical and)

Called Logical AND operator. If both


the operands are non-zero, then the
condition becomes true.
Example (A && B) is false.

|| (logical or)

Called Logical OR Operator. If any of


the two operands are non-zero, then
the condition becomes true.
Example (A || B) is true.

! (logical not)

Called Logical NOT Operator. Use to


reverses the logical state of its
operand. If a condition is true then
Logical NOT operator will make false.
Example !(A && B) is true.

The Assignment Operators


The assignment operator, '=', is the operator used for assignment. Like most other
operators in C++, it can be overloaded.
The copy assignment operator, often just called the "assignment operator", is a special case
of assignment operator where the source (right-hand side) and destination (left-hand side)
are of the same class type. It is one of the special member functions, which means that a
default version of it is generated automatically by the compiler if the programmer does not
declare one. The default version performs a member wise copy, where each member is
copied by its own copy assignment operator (which may also be programmer-declared or
compiler-generated)
Operators

Description

Simple assignment operator, Assigns


values from right side operands to
left side operand.
Example: C = A + B will assign value
of A + B into C

+=

Add AND assignment operator, It


adds right operand to the left
operand and assign the result to left
operand.
Example: C += A is equivalent to C
=C+A
Subtract AND assignment operator,
It subtracts right operand from the

-=

left operand and assign the result to


left operand.
Example:C -= A is equivalent to C =
C-A

*=

Multiply AND assignment operator, It


multiplies right operand with the left
operand and assign the result to left
operand.
Example: C *= A is equivalent to C
=C*A

/=

Divide AND assignment operator, It


divides left operand with the right
operand and assign the result to left
operand
Example C /= A is equivalent to C =
C/A

%=

Modulus AND assignment operator, It


takes modulus using two operands
and assign the result to left operand.
Example: C %= A is equivalent to C
=C%A

The Unary Operators


The unary operators require only one operand; they perform various operations such as
incrementing/decrementing a value by one, negating an expression, or inverting the value
of a boolean.

Operator

Description

Unary
plus
operator;
indicates
positive value (numbers are positive
without this, however)

Unary minus operator; negates an


expression

++

Increment operator; increments a


value by 1

--

Decrement operator; decrements a


value by 1

Logical complement operator; inverts


the value of a Boolean

You might also like