Arithmetic operations in java
Arithmetic operations in java
• 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
• 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:
• 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:
• Create a program which will add, subtract, multiply and divide the following numbers.
MyNum1 = 100
MyNum2 = 10
Java Comparison Operators
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: