How to use Arithmetic Operators in Java ?.
Arithmetic Operators in Java
In Java, when you want to deal with arithmetic operations such as, addition, subtraction, division, multiplication etc, you must have good knowledge about the Arithmetic Operators in Java. Arithmetic Operators are basic Java fundamentals, which every Java programmer must learn. Arithmetic Operators in Java are used when you want to perform any mathematical computation in Java. These mathematical computation can be addition, subtraction, division or multiplication of two or any numbers. Here, Arithmetic operator is applied between two operands. Arithmetic Operator is applied between operand1 and operand2.
In this hub, you will encounter types of Arithmetic Operators, video tutorial demonstrating its usage, Java source code demonstrating usage of Arithmetic Operators and detailed explanation about how Arithmetic operators works.
Arithmetic operators are of various types, as shown in figure below 
Types of Arithmetic Operators
Arithmetic Operator
 Description


+
 Addition


 Subtraction

*
 Multiplication

/
 Division

%
 Modulus

+=
 Addition Assignment

=
 Subtraction Assignment

*=
 Multiplication Assignment

/=
 Division Assignment

%=
 Modulus Assignment

Video Tutorial : How to use Arithmetic Operators in Java
Below video tutorial demonstrates, how to use Arithmetic Operators in Java programming language. In this tutorial, Arithmetic Operator usage and working is explained through Java source code. The output of the program in the video tutorial will provide you complete understanding and usage of Arithmetic Operators in Java.
Video Tutorial : How to use Arithmetic Operators in Java
Java Source Code  How to use Arithmetic Operator in Java.
After watching above video tutorial on Arithmetic Operator usage, you can browse complete Java source code for the Arithmetic Operator usage. This section has Java source code and detailed theory behind its working below.
Arithmetic Operator Source Code
public class ArithmeticOperatorsTest { public static void main(String[] args) { int number1 = 10; int number2 = 5; int result = 0; result = number1 + number2; System.out.println("number1 + number2 = " + result); number1 = 10; number2 = 5; result = number1  number2; System.out.println("number1  number2 = " + result); number1 = 10; number2 = 5; result = number1 * number2; System.out.println("number1 * number2 = " + result); number1 = 10; number2 = 5; result = number1 / number2; System.out.println("number1 / number2 = " + result); number1 = 10; number2 = 6; result = number1 % number2; System.out.println("number1 % number2 = " + result); number1 = 10; result += number1; // result += number1 results in result = result + number1 System.out.println("result += number1 = " + result); number1 = 10; result = number1; // result = number1 results in result = result  number1 System.out.println("result = number1 = " + result); number1 = 10; result *= number1; // result *= number1 results in result = result * number1 System.out.println("result *= number1 = " + result); number1 = 10; result /= number1; // result /= number1 results in result = result / number1 System.out.println("result /= number1 = " + result); number1 = 10; result %= number1; // result %= number1 results in result = result % number1 System.out.println("result %= number1 = " + result); } }
Output of the program 
Below image is a snapshot of the Eclipse console. This snapshot outputs the working of above Java source code.
Explanation of above program 
In above Java code,there is a Java class by name  ArithmeticOperatorsTest. This Java class has a main method by signature  public static void main( String [ ] args ) { }. This is the starting point for the execution of any Java program. Lets create two integer variables as, number1 and number2 and assign two integer value to it as, number1 = 10 and number2 = 5. So,
1. number1 + number2 = 15 ( addition )
2. number1  number2 = 5 ( subtraction )
3. number1 * number2 = 50 ( multiplication )
4. number1 / number2 = 2 ( division )
number1 = 10 and number2 = 6
5. number1 % number2 = 4 ( modulus )
6. result += number1 = 14 ( assignment addition )
7. result = number1 = 4 ( assignment subtraction )
8. result *= number1 = 40 ( assignment multiplication )
9. result /= number1 = 4 ( assignment division )
10. result %= number1 = 4 ( assignment modulus )
Here, result += number1 results in result = result + number1
result = number1 results in result = result  number1
result *= number1 results in result = result * number1
result /= number1 results in result = result / number1
result %= number1 results in result = result % number1
Output printed on the Java console is explained below 
1. 10 + 5 = 15
2. 10  5 = 5
3. 10 * 5 = 50
4. 10 / 5 = 2
5. 10 % 6 = 4. Here, modulus operator returns back the remainder as 4, after dividing 10 by 6.
6. 4 + 10 = 14. Here, result variable hold value as 4. It is assigned this value from step 5.
7. 14  10 = 4. Here, result variable hold value as 14. It is assigned this value from step 6.
8. 4 * 10 = 40. Here, result variable hold value as 4. It is assigned this value from step 7.
9. 40 / 10 = 4. Here, result variable hold value as 40. It is assigned this value from step 8.
10. 4 % 10 = 4. Here, result variable hold value as 4. It is assigned this value from step 9. Modulus operator returns back the remainder as 4, after dividing 4 by 10.
Watch my complete Youtube channel below 
 Java Hubberspot  YouTube
Hello friends, I am Dinesh Varyani. Owner of blog http://www.hubberspot.com . This channel will have Java Programming Tutorials for beginners ... Visit my Ja...
Comments
No comments yet.