The operators, which are applied to perform arithmetical calculations in a program, are known as arithmetical operators. Some basic calculations like addition, subtraction, multiplication, division, and modulus are often needed while programming. You can apply arithmetical operators like +,-,*,/, and % respectively to carry out these calculations.
Arithmetical Expression
An arithmetical expression may contain variables, constants, and arithmetical operators together to produce a meaningful result.
For statement.
example, x+y, m-15, b*b-4*a*c.
Arithmetical Statement
If an arithmetical expression is assigned to a variable then it is known as an arithmetical Statement.
Syntax: Variable Arithmetical Expression, z=x+y, c=m-15, b=b*b-4*a*c
Expressions in Java
When you write a program in Java, it is necessary to modify the arithmetical expressions into Java forms. A few examples are given below to illustrate how mathematical expressions are written in Java.
TYPE OF ARITHMETIC OPERATORS IN JAVA
There are three types of operators in Java, which are as follows:
- Unary Operator
- Binary Operator
- Ternary Operator
Unary Operators
An arithmetical operator, which is applied with a single operand is called a unary operator For example. +,-,++,–.
Unary (+) Operators
This operator is applied before the operand. It is just applied as a pointer to a variable which results in the same value as a variable.
For example,
if a=8, then a will result in 8,
if a=-10, then a will result in -10
Unary (-) Operators
This operator is used in the same way as unary plus (+) It is also applied before the operand Unary minus (-) reverts the sign of an operand. For example.
if a=4, then -a will result in -4,
if a=-3.6, then -a will result in -3.6.
Unary Increment and Decrement Operators
Unary increment operators (++) increase the value of an operant by one. The unary decrement operator (–) decreases the value of an operand by one.
For example,
- x=x+1 By applying the increment operator, it can be written as x++ or ++x.
- p=p-1 By applying the decrement operator, it can be written as p– or –p.
Prefix
When increment or decrement operators are applied before the operand it is known as prefix operators. These operators work on the principle “CHANGE BEFORE ACTION” It means the value of the variable changes before the operation takes place.
Example of prefix increment:
p=5;
p=++p *4;
After the operation p will result in 24.
Postfix
This unary operator is used after an operand whose value is to be increased or decreased by one. This works on the principle of “CHANGE AFTER THE ACTION”. This means that the operand will be affected after the operation is performed.
Example of postfix increment:
p=5;
p=p++*4;
After the operation p will result in 21.
Note: Both prefix and postfix increment operators increase the value of an operand by 1, before the action and after the action respectively. Prefix and postfix decrement operators work vice-versa.
The precedence of the postfix operator is higher than the prefix operator i.e. if in an expression both prefix and postfix operators are applied then the postfix is operated before the prefix.
Binary Arithmetic Operators
An arithmetic operator, which deals with two operands, is known as a binary arithmetic operator.
For example, +,-,*,/, and %.
Ternary Operators
Ternary operator is used in java programming to replace if conditionals with a one-liner. The ternary operator evaluates its first operand as either true or false and then makes out the second operand accordingly.
The ternary operator in Java separates computer code into three sections. It is purely used to perform an operation based on a given condition. The condition can be true or false, depending on whether a given value satisfies it. Let’s see an example of ternary operator which uses Boolean values (true and false) and a variable:
Syntax:
variable = Expression1 ? Expression2: Expression3;
If operates similarly to that of the if-else statement as in Exression2 is executed if Expression1 is true else Expression3 is executed.
if(Expression1)
{
variable = Expression2;
}
else
{
variable = Expression3;
}
Example:
a = 10;
b = 20;
c=(a>b) ? (a+b):(a-b);
/* Since num1<num2
the second operation is performed
c = a-b = -10
*/
You can use a conditional operator (ternary operator) in nested form as shown below Program snippet to find the maximum among three numbers:
int a-5, b-12, c-3;
max=(a>b) ? (a>c)?a:c (b>c)? b:c;
The test condition a>b is false. Hence, it will operate expression 2. In expression 2, the test condition b>c is true which enables value 12 to be stored in max: Hence, max=12.