# Arithmetic Operations on String in Java

## Introduction

Arithmetic, Relational, Bitwise, and Logical operators are all available in Java. Simple mathematical calculations are performed using Java arithmetic operators. Basic Arithmetic operators are considered in Java to be Addition, Subtraction, Division, and Multiplication. Operands for arithmetic operators should be of Numeric Type. Arithmetic operations on the char type are allowed in Java; char is considered a subset of int. Some binary arithmetic operators can also be employed as unary operators, such as the subtraction operator, which can be used to negate a positive number. If any of the operand types are double, float, or long, the result will be double, float, or long. The second operand is also transformed to double, float, and long.

## Arithmetic operators in Java

**‘+’ operator:**Use for**Addition**as well as**Unary Plus.****‘-‘ operator:**Use for**Subtraction**as well as**Unary Minus.****‘/’ operator:**Use for**Division.****‘*’ operator:**Use for**Multiplication.****‘%’ operator:**Use for**Modulus.****‘++’ operator:**Use for**Increment.****‘_’ operator:**Use for**Decrement.**

### Addition Operator “+”

The addition operator is also a unary operator, which means it performs an arithmetic action between two operands. This "+" operator is used to accomplish a simple arithmetic Addition.

To append two independent strings, the addition operator + is also used with String-type operands.

In addition, Operator + is also known as Unary +, and it returns a variable's positive value.

It performs Addition Operation with Numerical Operands and Concatenation Operation with String Operands when used with Numerical Operands.

On char type variables, Java allows us to execute arithmetic addition.

**Syntax:**

"Result=Operand1 + Operand2" or "ResultString=String1 + String2" Or "+Operand"

### Subtraction operator “–”

Subtraction operator “**–**” normally performs a basic and basic subtraction operation. The subtraction operator is also called the binary operator. This arithmetic operator can apply only to the numeric operators.

To negate the numeric value of Operand, the subtraction operator can also be employed as a unary – operator.

Because char is considered a subset of int in Java, we can do arithmetic Subtraction on char-type variables.

**Syntax:**

Result = Operand1 – Operand2 or "- Operand"

### Division Operator “/”

The mathematical division is performed by the division operator. This is a binary operator, which means that if both operands are integers, the result will also be an integer. If any of the operands are of the type Float, the result is also of the type Float. Handler gives DivideByZeroException of the type ArithmaticException when dividing any numeric value with 0 Java Exception.

**Syntax:**

result = Operand1 / Operand2;

### Multiplication Operator “*”

In addition to being a binary operator, the multiplication operator is also called the binary operator. Only numerical operands were used with this operator. The multiplication operator is used to execute basic mathematical multiplication.

**Syntax:**

Result = Operand1 * Operand2

### Modulus Operator:

The remainder of the two operands is returned by the modulus operator. This operator is a binary one as well. With ana integer or any other floating-point type variable, the modulo operator can be used. Any floating-point number with modulo 0 cannot be performed without throwing an ArithmaticException and returning the value’ NaN’.

**Syntax:**

Result = Operand1 % Operand2;

### Increment Operator:

The increment operator "++" increases the value of an operand by one at a time. The increment operator is also a unary operator, meaning it only accepts one operand. This operator has two modes: pre-increment and post-increment.

**Pre Increment**: In pre-increment, the value will be first increased and then used. The operand is also prefixed to the operator.

**Post Increment**: In post-increment, a variable's previous value is used first, and then it is increased. The operand is appended to the operator.

### Decrement Operator:

The unary operator "–" is a decrement operator. This operator reduces the value of the operand by one at a time. This operator can be used as a pre or post-decrement operator.

**Pre Decrement**: The operator is prefixed with an Operand in Pre Decrement. The value of the first operand is decremented by one later; its value has already been used.

**Post Decrement: **Decrement-operator is postfixed with the operand in Post Decrement. The previous value of the operand is used first, and then it is Decremented. Any number variable can be decremented.

**Example:**

```
public class OperatorDemo1
{
public static void main(String[] args)
{
int a=5;
int b=20;
int c=25;
int d=30;
int e=10;
System.out.println("");
System.out.println("a="+a+" b="+b+" c="+c+" d="+d);
System.out.println("");
System.out.println("Addition Operator +:a + b ="+(a+b));
System.out.println("Subtraction Operator -:b - a ="+(b-a));
System.out.println("Multiplication Operator *:a * b ="+(a*b));
System.out.println("Division Operator /:a / b ="+(b/a));
System.out.println("Unary Minus (d=30):"+(-d));
System.out.println("");
System.out.println("");
//Increment Operator ++
System.out.println("Value of e="+e+" After PreIncrement ++e:"+(++e));
System.out.println("Value of e="+e+" After PostIncrement :"+(e++)+" (e++):e= "+e);
System.out.println("");
//Decrement Operator --
System.out.println("");
System.out.println("Value of e="+e+" After PreDecrement (--e):"+(--e));
System.out.println("Value of e="+e+" After PostDecrement :"+(e--)+" (e--):e= "+e);
}
}
```

**Output:**

```
a=5 b=20 c=25 d=30
Addition Operator + : a+b =25
Subtraction Operator -:b - a =15
Multiplication Operator *:a * b =100
Division Operator /:a / b =4
Unary Minus (d=30): -30
Value of e=10 After PreIncrement ++e:11
Value of e=11 After PostIncrement:11 (e++) : e=12
Value of e=12 After PreDecrement (--e):11
Value of e=11 After PostDecrement :11 (e--):e= 10
```