operators
Arithmetic Operators
With this example we are going to demonstrate how to use the arithmetic operators in Java. The arithmetic operators supported by the Java programming language are the Additive
operator (also used for String concatenation), the Subtraction
operator, the Multiplication
operator, the Division
operator, and the Remainder
operator. In short, to use the arithmetic operators you should:
- Use the
Additive
operator to add variables. - Use the
Subtraction
operator to subtract variables. - Use the
Multiplication
operator to multiply variables. - Use the
Division
operator to divide variables.
Let’s take a look at the code snippet that follows:
package com.javacodegeeks.snippets.basics; public class ArithmeticOperatorsExample { public static void main(String[] args) { int i = 30 + 20; int j = i - 5; int k = j * 2; double l = k / 6; System.out.println("i = " + i); System.out.println("j = " + j); System.out.println("k = " + k); System.out.println("l = " + l); } }
Output:
i = 50
j = 45
k = 90
l = 15.0
This was an example of how to use the arithmetic operators in Java.