Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » OperatorsRSS Feeds

Arithmetic Operators

Posted By: Halima Khan     Category: Java     Views: 55669

This article explains about arithmetic operators in java with examples.

Arithmetic operators are used to perform mathematic operations on multiple expressions. We can use these operators with numeric or char data type. If we compare different types of variables or values, then the narrower type converted to wider type. 

Below are the arithmetic operators available in java.

Arithmetic Operators in java

 Operator

Meaning 

Example 

 +

Addition 

a + b 

 -

Subtraction 

a - b 

 *

Multiplication 

a * b 

 /  

Division

a / b 

 % 

Modulo 

a % b 

 +1 

Unary Plus 

+4 

 -1 

Unary Minus 

-4 



Precedence of Arithmetic Operators

Highest to Lowest

Operator

1

*, /, %

2

+, -



Syntax of Arithmetic Operators

op1 arthOptr op2

op1 is a first operand to compare.
op2 is a second operand to compare.
arthOptr is a arithmetic operator.


Examples of Arithmetic Operators

Example 1 : Program that displays use of all arithmetic operators i.e +, -, *, /, %

class ArthOptrDemo 
{
   public static void main(String[] args) 
   {
int a = 10, b = 15, c = 15;
System.out.println("Arithmetic Operators and returned values");
System.out.println(" a + b = " + (a + b)); 
System.out.println(" b - a  = " + (b - a)); 
System.out.println(" a * 2  = " + (a * 2)); 
System.out.println(" b / 5 = " + (b / 5)); 
System.out.println(" b % 2 = " + (b % 2)); 
System.out.println(" Unary plus = " + +2)); 
System.out.println(" Unary minus = " + -2); 

   }
}


Output

Arithmetic Operators and returned values
a + b = 25
b - a = 5
a * 2 = 20
b / 5 = 3
b % 2 = 1
Unary plus = 2
Unary minus = -2 

  
Share: 


Didn't find what you were looking for? Find more on Arithmetic Operators Or get search suggestion and latest updates.

Halima Khan
Halima Khan author of Arithmetic Operators is from Karachi, Pakistan.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!