Arithmetic Operators in Java – Different Types with Code Examples

Updated on October 24, 2024

Article Outline

Java is a versatile programming language that is widely used. It provides a rich set of features that facilitate programming tasks. These features are arithmetic operators, which are fundamental to performing mathematical calculations in any Java application. This article explores the arithmetic operators in Java language, their usage and some practical examples that demonstrate their power.

 

What are Arithmetic Operators in Java?

 

It is the special symbols that perform mathematical operations on numeric values. They allow developers to manipulate numbers in various ways, making them fundamental to programming in Java. These operators allow developers to carry out fundamental calculations such as addition, subtraction, multiplication, division and modules. They are essential for any programming task that requires numerical computations.

 

Program

public class Main{ public static void main(String args[]){ int a =  20 ; int b = 3 ; System.out.println("Addition = "+(a+b)); System.out.println("Subtraction = "+(a-b)); System.out.println("Multiplication = "+(a*b)); System.out.println("Division = "+(a/b)); System.out.println("Module = "+(a%b)); } }

Output

Addition = 23 Subtraction = 17 Multiplication = 60 Division = 6 Module = 2
*Image
Get curriculum highlights, career paths, industry insights and accelerate your technology journey.
Download brochure

Types of Operators in Java

 

  • Arithmetic Operators
  • Unary Operators
  • Relational Operators
  • Assignment Operators
  • Logical Operators
  • Ternary Operators
  • Bitwise Operators
  • Shift Operators

 

Also Read:  Java Operators

Arithmetic Operators

 

Arithmetic operators involve the mathematical operations on the operands. It can perform various simple or advanced arithmetic operations on the primitive data types referred to as the operands.  In Java language, Arithmetic operators can be defined into two categories. Binary Operators and Unary Operators.

 

 

  • Binary Operators: This operator requires two operands to implement the action that is known as a binary operator.
  • Unary Operators: This type of operator only performs on a single operand and provides the Unary operators.

 

Operator Name Category Operator Description
Addition Operator Binary + Addition of two numbers
Subtractor Operator Binary Subtraction of two numbers
Multiplication Operator Binary * Multiplication of two numbers
Division Operator Binary / Division of two numbers
Modulus Operator Binary % Return the remainder after dividing two numbers
Increment Operator Unary ++ Increase the value by 1
Decrement Operator Unary Decrease the value by 1

 

Addition Operator (+)

This operator is used to perform the addition of two operands. It is a binary-type operator.

 

Syntax  

operand 1 + operand2

Example Program

public class Main{ public static void main(String args[]){ int a =  20 ; int b = 3 ; int c = a+b ; System.out.println("The addition of 20  and 3 is = "+c); } }

Output

The addition of 20  and 3 is = 23

Also Read: Logical Operators in Java

Subtraction Operator(-)

This operator subtracts one operand value to another operand value. It is also a binary operator that can deal with numeric values.

 

Syntax

operand1 - operand2

Example Program 

public class Main{ public static void main(String args[]){ int a =  20 ; int b = 3 ; int result =  a- b ; System.out.println("The subtraction of "+a+" and "+b+ " is "+result); } }

Output

The subtraction of 20 and 3 is 17

Multiplication Operator(*)

The Java multiplication operator of (*) is used to multiply two numbers in a program as upheld by several researchers. It works with several integral and floating point types: pos integer, integer, floating point and double for positive and negative numeric values.

 

Syntax

result = operand1 * operand2;

Example Program

public class Main{ public static void main(String args[]){ int a =  25 ; int b = 5 ; int result =  a * b ; System.out.println("The mulitpliation of "+a+" and "+b+ " is "+result); } }

Output

The multiplication of 25 and 5 is 125

Also Read: Relational Operators in Java

Division Operator (/)

This is a binary operator that is used to divide the first operand(dividend) by the second operand (divisor) and give the quotient as a result.

 

Syntax

num1 / num2

Example of the Program

class Main{ public static void main(String args[]){ int num1 = 20, num2 = 30, div = 0  ; System.out.println("num1 = "+num1); System.out.println("num2 = "+num2); div = num1 / num2 ; System.out.println("Division = "+div); } }

Output

num1 = 20 num2 = 30 Division = 0

Also Read: Bitwise Operators in Java

Modulus Operator(%)

The modulus operator % is used in a fashion to return the remainder of the division of one number by another. This operator becomes most helpful in several cases including identifying whether a particular number is odd or even, cyclic operations, or in time calculations.

 

Syntax

operand1 % operand2

Example Program 

class Main{ public static void main(String args[]){ int num1 = 20 , num2 = 30 , result =0 ; result = num1 % num2  ; System.out.println("The modulo of "+num1+" and "+num2+ " is "+result); } }

Output

The modulo of 20 and 30 is 20

Increment Operator (++)

The increment operator(++) is a unary operator used to increase the value of a variable by one. This can be applied in two forms, pre-increment and post-increment.

 

  • Pre-increment (++): This operator increment is placed before the variable, It increases the variable’s value by one and then returns the uploaded value. For example, in the expression ++a, if a is initially 5, it becomes 6 and the value of the expression is also 6.

 

  • Post-Increment(variable++): This operator is placed after the variable, it fetches the value in that variable before your code increases it. For instance, in a++ if a is 5, equals 5 but an after operation is equal to 6.

 

Example of Program

class Main{ public static void main(String args[]){ int a = 5; int preIncrementResult = ++a; System.out.println("Pre-increment: " + preIncrementResult); a = 5; int postIncrementResult = a++; System.out.println("Post-increment: " + postIncrementResult); System.out.println("Value of an after post-increment: " + a); } }

Output

Pre-increment: 6 Post-increment: 5 Value of an after post-increment: 6

Also Read: Increment and Decrement Operators in C

Decrement Operator (–)

The decrement operator (–) is a unary operator. It declares the value of a variable by one. It can be used in two forms, pre-decrement and post-decrement.

 

  • Pre-Decrement: The decrement operator is placed before the variable, it declares the variable’s value by one and then returns the updated value. For example, in the expression –a, if a starts at 5, it becomes 4, and the value of the expression is also 4.

 

  • Post-Decrement (variable–): The operator is placed after the variable, it returns the current value of the variable before decrementing it, for instance, in a– if a is 5, the expression evaluates to 5, but after the operation, a becomes 4.

 

Example of Program

class Main{ public static void main(String args[]){ int a =5 ; int dec = --a ; System.out.println("Pre-Decrement "+dec); a= 5 ; int postDec = a-- ; System.out.println("Post-Decrement "+postDec); System.out.println("Value of an after post-dec "+a) ; } }

Output

Pre-Decrement 4 Post-Decrement 5 Value of an after post-dec 4

Conclusion

Any programmer must want to fully exploit Java and learn the arithmetic operators in Java. These are basic arithmetic operators which construct the fundamentals of calculations in applications, from basic arithmetic to extensive algorithms. Appreciation of how addition, subtraction, multiplication, division and modulo operations can be used as is and operated on for more efficient source code as well as benefiting the problem-solving abilities. Java arithmetic operation’s solid graphs of these basic concepts will help your programming skills and prepare you to work with more complex concepts as well. This empowers the arithmetic operators and takes your Java programming experience to another level. Get hands-on experience in Java with the Certificate Program in Application Development offered by Hero Vired.

FAQs
The modulus operator(%) returns the remainder of a division operation. For example, 5 % 2 equals 1 because 5 divided by 2 leaves a remainder of 1.
Java does not support operator overloading like some other languages. However, you can achieve similar functionality through method overloading and by defining custom classes
No, arithmetic operators cannot be used with boolean values. If you need to perform logical operations, you should use logical operators (&&, ||, ! instead.
Yes, we can perform multiple arithmetic operations in a single line, but be cautious about readability and maintaining clear logic. For example: int result = (a+b)  * (c - d)  ;
Yes, we can use arithmetic operations within lambda expressions just like in regular methods. It ensures the types are compatible, and the logic is clear for readability.

Updated on October 24, 2024

Link
left dot patternright dot pattern

Programs tailored for your success

Popular

Management

Data Science

Finance

Technology

Future Tech

Upskill with expert articles

View all
Hero Vired logo
Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.
Blogs
Reviews
Events
In the News
About Us
Contact us
Learning Hub
18003093939     ·     hello@herovired.com     ·    Whatsapp
Privacy policy and Terms of use

|

Sitemap

© 2024 Hero Vired. All rights reserved