JAVA PROGRAMMING
JAVA ARITHMETIC 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
• Unary Operators
• Assignment Operator
• Relational Operators
• Logical Operators
• Ternary Operator
• Bitwise Operators
• Shift 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.
ADDITION
• Addition(+): This operator is a binary operator and is used to add two operands.
• Syntax:
• num1 + num2
• Example:
• num1 = 10, num2 = 20
• sum = num1 + num2 = 30
SUBTRACTION
• Subtraction(-): This operator is a binary operator and is used to subtract two operands.
• Syntax:
• num1 - num2
• Example:
• num1 = 20, num2 = 10
• sub = num1 - num2 = 10
MULTIPLICATION
• Multiplication(*): This operator is a binary operator and is used to multiply two
operands.
• Syntax:
• num1 * num2
• Example:
• num1 = 20, num2 = 10
• mult = num1 * num2 = 200
DIVISION
• Division(/): This is a binary operator that is used to divide the first operand(dividend) by the
second operand(divisor) and give the quotient as result.
• Syntax:
• num1 / num2
• Example:
• num1 = 20, num2 = 10
• div = num1 / num2 = 2
EXERCISE
• Create a program which will add, subtract, multiply and divide the following numbers.
MyNum1 = 100
MyNum2 = 10
Java Comparison Operators
Comparison operators are used to compare two values. This is important in
programming, because it helps us to find answers and make decisions.
The return value of a comparison is either true or false. These values are known as
Boolean values, and you will learn more about them in the Booleans and If..Else
chapter.
In the following example, we use the greater than operator ( >) to find out if 5 is greater
than 3: