For working professionals
For fresh graduates
More
6. JDK in Java
7. C++ Vs Java
16. Java If-else
18. Loops in Java
20. For Loop in Java
45. Packages in Java
52. Java Collection
55. Generics In Java
56. Java Interfaces
59. Streams in Java
62. Thread in Java
66. Deadlock in Java
73. Applet in Java
74. Java Swing
75. Java Frameworks
77. JUnit Testing
80. Jar file in Java
81. Java Clean Code
85. Java 8 features
86. String in Java
92. HashMap in Java
97. Enum in Java
100. Hashcode in Java
104. Linked List in Java
108. Array Length in Java
110. Split in java
111. Map In Java
114. HashSet in Java
117. DateFormat in Java
120. Java List Size
121. Java APIs
127. Identifiers in Java
129. Set in Java
131. Try Catch in Java
132. Bubble Sort in Java
134. Queue in Java
141. Jagged Array in Java
143. Java String Format
144. Replace in Java
145. charAt() in Java
146. CompareTo in Java
150. parseInt in Java
152. Abstraction in Java
153. String Input in Java
155. instanceof in Java
156. Math Floor in Java
157. Selection Sort Java
158. int to char in Java
163. Deque in Java
171. Trim in Java
172. RxJava
173. Recursion in Java
174. HashSet Java
176. Square Root in Java
189. Javafx
Arithmetic operators in Java are indispensable for performing mathematical operations. They allow you to add, subtract, multiply, divide, and perform many other mathematical operations. To execute mathematical calculations and manipulate numeric values in Java programs, it is essential to understand arithmetic operators.
In this tutorial, we will learn about arithmetic operators in Java. They are used to perform basic mathematical operations in Java. This tutorial also covers the different types of arithmetic operators and their uses with the help of examples.
Java has several types of operators, each with a distinct function. The most common Java operators are discussed below:
The unary operator in Java is an operator that can only be used in conjunction with an operand. It is employed to denote a positive or negative value, an increment or a decrement of one, and the complement of a Boolean value.
Unary operators can be classified into two types: prefix and postfix operators.
Prefix Unary Operators:
Increment: ++operand (increments the value of the operand by 1)
Decrement: --operand (decrements the value of the operand by 1)
Unary Plus: +operand (returns the value of the operand)
Unary Minus: -operand (negates the value of the operand)
Postfix Unary Operators:
Increment: operand++ (increments the value of the operand by 1 and returns the original value)
Decrement: operand-- (decrements the value of the operand by 1 and returns the original value)
To assign a value to a variable in Java, we use the assignment operator (=) in Java. It gives the variable on the left side of the operator the value that is on the right side of the operator.
Example:
int a = 5;
int b = a;
Relational operators are used in Java to compare values and establish their relationship. Based on the comparison outcome, these operators yield a boolean value, either true or false.
There are 6 types of relational operators used in Java. They are:
Example:
int a = 10;
int b = 5;
boolean c = a == b;
Example:
int a = 11;
int b = 12;
boolean c = a != b;
Example:
int a = 32;
int b = 6;
boolean c = a > b;
Example:
int a = 8;
int b = 89;
boolean c = a < b;
Example:
int a = 22;
int b = 9;
boolean c = a >= b;
Example:
int a = 6;
int b = 12;
boolean c = a <= b;
Logical operations on boolean expressions or values are performed in Java using logical operators. These operators let you mix different circumstances and assess the outcome.
There are three logical operators in Java. They are
Example:
boolean variable1 = true;
boolean variable2 = true;
boolean outcome = variable1 && variable2;
Example:
boolean variable3 = false;
boolean variable4 = true;
boolean outcome = variable3 || variable4;
Example:
boolean variable5 = true;
boolean outcome = !variable5;
The conditional operator, also called the ternary operator in Java, is a convenient way to create conditional statements. It enables you to select one of two expressions to analyze and choose depending on a condition.
Syntax:
Condition ? expression1 : expression2
Example:
Int age = 60;
String result = (age >= 59) ? “Senior” : “Junior”;
Bitwise operators are used in Java to carry out operations on a single bit of an integer value. These operators change the bits that make up the binary representation of integers.
There are four types of bitwise operators in Java. They are:
Syntax: operand1 & operand2
Syntax: operand1 ^ operand2
Syntax: ~operand
Syntax: operand1 | operand2
Shift operators in Java are bitwise operations that move a value's bits to the left or right. The shift operators allow you to shift the bits of an integer left or right. There are three types of shift operators in Java:
There are broadly five types of arithmetic operators in Java. Here, we will further discuss them in detail with the help of examples.
The addition operator (+) in Java is an arithmetic operator that adds numerical operands. It is mainly employed for two-number addition. Any numeric type, such as int, double, float, long, short, or byte, may be used for the operands. The addition operator adheres to the standard addition arithmetic rules.
Example:
public class AdditionOperatorExample {
public static void main(String[] args) {
int num1 = 10;
int num2 = 20;
int sum = num1 + num2;
System.out.println("Sum: " + sum);
}
}
In this example, we have initialized two integer variables, num1, and num2, with the values 10 and 20, respectively. The addition operator + adds the values of num1 and num2, and the result is stored in the sum variable.
Finally, the value of sum is printed using System.out.println().
The arithmetic operator for subtracting from numeric operands in Java is the subtraction operator (-). The subtraction operator follows the standard arithmetic rules for subtraction. The second operand is subtracted from the first operand. Any type of numeric data, including int, double, float, long, short, and byte, may be used as the operands.
Example:
public class main {
public static void main(String[] args) {
int num1 = 30;
int num2 = 10;
int difference = num1 - num2;
System.out.println("Difference: " + difference);
}
}
In this example, we have two integer variables, num1, and num2, initialized with the values 30 and 10, respectively. The subtraction operator - is used to subtract the value of num2 from num1, and the result is stored in the difference variable.
Finally, the value of difference is printed using System.out.println().
The multiplication operator (*) in Java is an arithmetic operator used to multiply numerical operands. It is applied when multiplying two numbers together. Any numeric type, such as int, double, float, long, short, or byte, may be used for the operands. The multiplication operator adheres to the common multiplication principles of arithmetic.
Example:
public class main {
public static void main(String[] args) {
int num1 = 5;
int num2 = 6;
int product = num1 * num2;
System.out.println("Product: " + product);
}
}
In this example, we have two integer variables, num1, and num2, initialized with the values 5 and 6, respectively. The multiplication operator * is used to multiply the values of num1 and num2, and the result is stored in the product variable.
Finally, the value of product is printed using System.out.println().
The division operator (/) is an arithmetic operator in Java that divides numerical operands. It is employed to divide two numbers by two. Any type of numeric data, including int, double, float, long, short, and byte, may be used as the operands. The division operator adheres to the standard divisional arithmetic rules.
Example:
public class main {
public static void main(String[] args) {
int dividend = 20;
int divisor = 5;
int quotient = dividend / divisor;
System.out.println("Quotient: " + quotient);
}
}
In this example, we have two integer variables, dividend, and divisor, initialized with the values 20 and 5, respectively. The division operator / divides the dividend by the divisor, and the result is stored in the quotient variable.
Finally, the value of the quotient is printed using System.out.println().
The modulus operator (%) in Java is an arithmetic operator used to determine the remainder after dividing two numbers. The modulus operator adheres to the accepted arithmetic principles when calculating the remainder. Any type of numeric data, including int, double, float, long, short, and byte, may be used as the operands.
Example:
public class ModulusOperatorExample {
public static void main(String[] args) {
int dividend = 20;
int divisor = 6;
int remainder = dividend % divisor;
System.out.println("Remainder: " + remainder);
}
}
In this example, we have two integer variables, dividend, and divisor, initialized with the values 20 and 6, respectively. The modulus operator % is used to find the remainder when the dividend is divided by the divisor, and the result is stored in the remainder variable.
Finally, the value of remainder is printed using System.out.println().
Java arithmetic operators allow you to work with numerical quantities in mathematical calculations. You can modify numbers in your programs using their addition, subtraction, multiplication, division, and modulus operators.
You can build robust algorithms and incorporate complicated logic in your Java programs by combining arithmetic operators with other operators, such as relational, logical, and assignment operators.
Checking out courses from upGrad is an excellent way to learn more about core Java concepts in depth.
1. What distinguishes the modulus (%) operator from the division (/) operator?
The modulus operator (%) returns the remainder of the division while the division operator (/) executes the division and returns the quotient.
2. What occurs in Java when you divide an integer by zero?
In Java, an ArithmeticException error is raised when an integer is divided by zero. Due to the ambiguity of division by zero, it is not permitted.
3. How can I divide using a decimal result rather than an integer result?
Floating-point numbers can be used to divide a number into a decimal result. For instance, you may cast one of the operands to a floating-point type or split two doubles.
Take the Free Quiz on Java
Answer quick questions and assess your Java knowledge
Author
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.