×

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 String Concatenation

Java String Concatenation Java programming provide a way to combine multiple strings into a single string. It is called as String Concatenation. There are different ways to concatenate two or more...

4 minutes read.

Morris Traversal for Inorder in Java

Through Morris’s traversal, a tree is traversed without the aid of recursion or stacks. Based on the threaded binary tree, the Morris traversal is used. We perform internal modification throughout...

4 minutes read.

House Numbers in Java

In this section, we will discuss about house number in Java. It is a sum of cubes, each of which has a dimension of h + 1. There is a...

3 minutes read.

PriorityBlockingQueue Class in Java

What is the Queue? An abstract data structure like Stacks is a queue. A queue is open on both ends. Data is always pushed to one end, called enqueue, and removed...

4 minutes read.

Java 8 Features

Java 8 Features: After the great success of Java 7, many developers were waiting for the next version of one of the best languages in the tech world. Moreover, on...

6 minutes read.

History and Evolution of Java

Java is invented by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and Mike Sheridan at Sun Microsystems, Inc. in 1991. Java is related to C++, which is inherited from...

3 minutes read.

Features of Java

The objective of the Java programming language is to provide portability, security, and simplicity. In spite of this, Java has a number of features that makes it a popular language...

5 minutes read.

Difference Between in Java and C++

FeatureC++JavaDefinitionC++ is a general programming language created by Bjarne Stroustrup as an extension of c language    Java is class-based, object-based, and designed to have as few implementation dependencies as...

4 minutes read.

Literals in Java

In this article we acknowledge ourselves about the term literals in java, types of literals and an example to each of them. Literal Literal refers to any constant value that can be...

4 minutes read.

Java Default Keyword

The Default keyword in java programming language is used as access modifier.If any of the variable or the constructor or the methods or the classes are not assigned with the...

3 minutes read.

Memory Areas in Java

Let’s have a look at how memory management in Java works. We will be going to discuss how the objects get destroyed, the working of a garbage collector, and things...

5 minutes read.

Ternary Operator in Java

In some cases, the if...else expression in Java can be replaced by a ternary operator. Visit the Java if...else statement first before learning about the ternary operator. Ternary operator in Java A...

3 minutes read.

Java Map Interface

A map is a collection that maps keys to values, with no duplicate keys allowed. The elements in a map are key/value pairs. HashMap: HashMap stores the keys in a...

3 minutes read.

Converting Long to Date in Java

What Long and Date are in Java and how are they implemented in the Java programming language are the topics of this article. Additionally, we'll go into great detail on...

4 minutes read.

String Coding Interview Questions in Java

What is String in Java?In Java, a String is a Class that is defined in the java.lang package. It isn't a basic data type like int or long. Character Strings...

5 minutes read.

Java Program to print even and odd numbers using 2 threads

Using two threads in a single thread is even odd printing in Java programming with multiple threads. To create code that prints even and odd using two threads, we must...

2 minutes read.

Get yesterdays date by no of days in Java

In this tutorial, we are going to learn how to get yesterday’s date by the no of days in Java. Using the Calendar class, one can get the current date....

1 minute read.

Root exception in java

Java: The main feature of java which is not in C or object oriented programming language is platform independence. Not only the platform independence there are many other features in java...

3 minutes read.

Java CountDownLatch

Another crucial classes for concurrent execution is CountDownLatch. It is a synchronisation tool that enables one or more threads to await until a series of tasks started by another thread...

4 minutes read.

List of Constants in Java

In this tutorial, we are going to deal with constants available in Java.Every programming language has its constants. Similarly, Javaalso has got constants of its own. In this tutorial, we will...

6 minutes read.