0% found this document useful (0 votes)
4 views

Java Operator

java operator

Uploaded by

roxas.rr.bscs
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Java Operator

java operator

Uploaded by

roxas.rr.bscs
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

JAVA OBJECT-ORIENTED

PROGRAMMING
JAVA OPERATORS
JAVA OPERATORS
Operators constitute the basic building block to any programming language. Java too
provides many types of operators which can be used according to the need to perform
various calculation and functions be it logical, arithmetic, relational etc. They are
classified based on the functionality they provide. Here are a few types:
• Arithmetic Operators
• Assignment Operator
• Unary Operators
• Relational Operators
• Logical Operators
Arithmetic Operators
• These operators involve the
mathematical operators that can
be used to perform various simple
or advance arithmetic operations
on the primitive data types
referred to as the operands.
These operators consist of various
unary and binary operators that
can be applied on a single or two
operands respectively. Let’s look
at the various operators that Java
has to provide under the
arithmetic operators.
• Additional - This operator is a binary • Modulus(%): This is a binary operator
operator and is used to add two that is used to return the remainder
operands when the first operand(dividend) is
• Subtraction - This operator is a binary divided by the second
operator and is used to subtract two operand(divisor).
operands • Increment(++): This is a unary operator
• Multiplication - This operator is a binary that acts on one operand, unlike the
operator and is used to multiply two previous operations. It is used to
operands increment the value of an integer. It
• Division - This is a binary operator that can be used in two ways
is used to divide the first • Decrement(–): This is also a unary
operand(dividend) by the second operator that acts on one operand. It is
operand(divisor) and give the quotient used to decrement the value of an
as result integer. It can be used in two ways
SAMPLE

Additional (+) Operator Subtraction (-) Operator


int num1 , num2 , total int num1 , num2 , total
num1 = 10 num1 = 10
num2 = 10 num2 = 5
total = num1 + num2 total = num1 - num2
Output : 20 Output : 5
SAMPLE

Multiplication (*) Operator • Division (/) Operator


int num1 , num2 , total nt num1 , num2 , total
num1 = 10 num1 = 10
num2 = 10 num2 = 10
total = num1 * num2 total = num1 / num2
Output : 100 Output : 1
SAMPLE

Modulus (%) Operator


int num1 , num2 , total
num1 = 5
num2 = 2
total = num1 % num2
Output : 1
SAMPLE

Increment (++) Operator Increment (++) Operator


Post-Increment Pre-Increment
int num1 int num1
num1 = 10 num1 = 10
num1++ ++num1
Output : 10 Output : 11
SAMPLE

Decrement (--) Operator Decrement(--) Operator


Post-Decrement Pre-Decrement
int num1 int num1
num1 = 10 num1 = 10
num1-- --num1
Output : 10 Output : 9
ASSINGMENT OPERATOR
• These operators are used to assign
values to a variable. The left side
operand of the assignment operator is
a variable and the right side operand of
the assignment operator is a value.
The value on the right side must be of
the same data-type of the operand on
the left side otherwise the compiler will
raise an error. This means that the
assignment operators have right to left
associativity, i.e value given on the
right-hand side of the operator is
assigned to the variable on the left and
therefore right-hand side value must be
declared before using it or should be a
constant.
• “=”: This is the simplest assignment operator which is
used to assign the value on the right to the variable on the
left. This is the basic definition of assignment operator
and how does it functions.
Sample:
int num1;
num1 = 10
Output 10
• “+=”: This operator is a compound of ‘+’ and ‘=’ operators.
It operates by adding the current value of the variable on
left to the value on the right and then assigning the result
to the operand on the left.
Sample:
int num1 = 10 , num2 = 5
num1 += num2
Output : 15
• “-=”: This operator is a compound of ‘-‘ and ‘=’ operators.
It operates by subtracting the value of the variable on right
from the current value of the variable on the left and then
assigning the result to the operand on the left.
Sample:
int num1 = 10 , num2 = 5
num1 -= num2
Output : 5
• “*=”: This operator is a compound of ‘*’ and ‘=’ operators.
It operates by multiplying the current value of the variable
on left to the value on the right and then assigning the
result to the operand on the left.
Sample:
int num1 = 10 , num2 = 5
num1 *= num2
Output : 50
• “/=”: This operator is a compound of ‘/’ and ‘=’ operators. It
operates by dividing the current value of the variable on
left by the value on the right and then assigning the
quotient to the operand on the left.
Sample:
int num1 = 10 , num2 = 5
num1 /= num2
Output : 2

You might also like