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.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button