×

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

Related Topics

Java Integer hashCode() method

The hashCode()  method of Java Integer class returns a hash code for this Integer.  Syntax public int hashCode() public static int hashCode(int value)  Parameters The parameter ‘value’ represents a value whose hash code...

1 minute read.

Java InputStreamReader

What is InputStreamReader?An InputStreamReader is a converter between byte and character streams: It reads bytes and converts them to characters with the help of a charset. The charset it uses can...

4 minutes read.

Iterate JSON array in Java

This article is going to equip the knowledge about what JSON array is and how it works and also how is it distinct with the generic array. Json Array Ordered lists of...

4 minutes read.

Java Error Stack Trace

The stack trace in Java is an array of stacks.The stack trace reveals the console's location of an exception or error by gathering data from all program methods. The JVM...

3 minutes read.

Java Math exp() Method

The exp() method of Math class returns Euler’s number(e) raised to the power of a double value. Syntax: public static double exp(double a) Parameters: The parameter ‘a’ represents the exponent e. Return Value: The exp ()...

2 minutes read.

Java Math tanh() Method

The tanh() method of Java Math class returns the hyperbolic tangent of the specified double value. Syntax: public static double tanh(double x) Parameters: The parameter ‘x’ represents the number whose hyperbolic tangent is to...

2 minutes read.

Java Math incrementExact() Method

The incrementExact() method of Math class returns the argument incremented by one, throwing an exception if the result overflows an int or a long. Syntax: public static int incrementExact (int a)public static...

1 minute read.

Two Decimal Places Java

When a double data type is used in Java before a variable, this indicates 15 digits after decimal point. However, there are situations when we only require 2 decimal places...

4 minutes read.

Relatively Prime in Java

In this article, you will be very well equipped with a knowledge of a relatively prime number and also Java programs to determine whether a given integer is a relatively...

4 minutes read.

Java Integer compareTo() method

The compareTo() method of Integer class compares two Integer objects numerically. Syntax public static int compareTo(int anotherInteger) Parameters The parameter ‘anotherInteger’ represents the Integer to be compared. Specified by This method is specified by compareTo in...

2 minutes read.

Sorting a String in Java

Sorting a String in Java Sorting is a process of arranging available data objects in a meaningful format that is ascending or descending order. Sorting a string or array of String...

4 minutes read.

Difference between String and Char Array in Java

We are heading to examine some significant differences between String and Character arrays. Both char arrays and String hold the series of characters and are utilised as a cluster of...

3 minutes read.

Generic queue in Java

Before understanding how to implement a generic queue in java, one must know about generics and queue in java. Generics in Java Generics are parameterized types. The goal is to enable type...

6 minutes read.

Deadlock in Java

Deadlock is when two or more processes wait for the state to do their tasks, but none of them can do so. It is a very common problem that one...

6 minutes read.

Tug of War in Java

As in the tug-of-war issue, we must divide the given collection of n numbers into two groups of sizes that are equal or nearly equivalent. A minimum difference must exist...

5 minutes read.

Java Vs C++

Java Vs C++ Java and C++ both are Object Oriented Programming languages. Both languages are popular for competitive programming. C++ is used by many coders who have just started learning programming...

4 minutes read.

Java delete directory

The File classes in Java may symbolize a directory or a file on the system. Inside the java.io package, the Files class is accessible. The File class has several helpful...

2 minutes read.

Java Static Keyword

It can be either said that static declares the value to be the same, not only in the instance of a class but also as the whole. To declare a variable...

6 minutes read.

Read the XLSX file in Java

Excel files contain cells; reading an excel file in Java differs from reading a word file. JDK doesn't have a direct API that can read or write Word or Excel...

3 minutes read.

Practical Number in Java

In this tutorial, we will understand what is meant by practical numbers. We will understand it throughthe aid of examples and implementation in a java programming language. The practical numbers...

5 minutes read.