Basic arithmetic operations in Java

Java supports the following basic arithmetic operations:

assignment =

Assignment sets the value of a variable on the left hand side to that of the expression on the right hand side. For example:
		a=4;     // a takes the value 4
		b=a+1;   // b  takes the value 5
		d=c=a+b; // both d and c are 9
		

negation -

Negation makes a variable or expression evaluate to its negative but without changing variable to right of the equals sign. For example:
		a=4;     // a takes the value 4
		b=-a;    // b  takes the value -4 but a is still 4.
		

addition +

The addition operator adds two variable or expressions. For example:
		a=4+5;   // a takes the value  9
		b=-a+4;  // b takes the value -5
		

increment by 1 ++

The increment operator, ++, increases the value of its operand by 1. This operator has two forms. If the ++ characters occur before the operand the value of the operand is incremented before it is assigned to a variable on the left hand side of the equals sign. In this case the ++ operator is termed a prefix operator. If the ++ characters occur after their operand then the ++ operator is termed a postfix operator and it acts to increment its operator after any assignment is made. For example:
		a=4;     // a takes the value  4
		a++;     // a takes the value  5
		++a;     // a takes the value  6
		b=++a;   // a and b take the value 7 (prefix operator)
		c=a++;   // c takes the value 7 but a becomes 8 (postfix operator) 
		

subtraction -

The subtraction operator subtraction two variable or expressions. For example:
		a=4-5;   // a takes the value  -1
		b=-a-4;  // b takes the value  -3
		

decrement by 1 --

This act analogously to the increment operator. So, -- decreases the value of its operand by 1. Again, the operator has two forms. If the -- characters occur before the operand the value of the operand is decremented before it is assigned to a variable on the left hand side of the equals sign. In this case the -- operator is a prefix operator. If the -- characters occur after their operand then the ++ operator a postfix operator, it acts to decrement its operator after any assignment is made. For example:
		a=10;    // a takes the value  10
		a--;     // a takes the value  9
		--a;     // a takes the value  8
		b=--a;   // a and b take the value 7 (prefix operator)
		c=a--;   // c takes the value 7 but a becomes 6 (postfix operator) 
		

multiplication *

The multiplication operator provides the product two variable or expressions. For example:
		a=4*5;   // a takes the value  20
		b=-a*2;  // b takes the value  -40
		

division /

The division operator provides the division of it operand on the left by the operand on the right of the / character. Integer types are handled differently than floating point types (double or float). In particular, it is an error to divide an integer type by 0. Attempting to do this will result in a error condition call an "ArithmeticException". We discuss Exceptions later in this tutorial. Division of a non-zero floating point type by 0.0 will results in a value of Infinity. Division of 0.0 by 0.0 will result in a value of NaN (not a number). Both Infinity and NaN are valid states and again are discussed later in this tutorial. Example of division:
		int a=4/2;         // a takes the value  2
		int b= a/3;        // b takes the value  0 
		double da=4.0/2.0; // da takes the value 2.0 
		double db=da/3.0;  // db takes the value 0.6666666666666666
		double dc=da/0.0;  // dc takes the value Infinity
		double dd=0.0/0.0; // dd takes the value NaN
		

modulo %

For positive operands the modulo operator % provides the remainder after integer division of operand on the left of the % character by operand on the right of the % character. For Example
		int a= 8 % 2;        // a takes the value  0 since 8=4*2+0
		int b= 7 % 3;        // b takes the value  1 since 7=3*2+1
		
		double da=8 % 2.0;   // da takes 0.0  
		double db=7.0 /3.0;  // db takes 1.0
		
Where one or both of the operand is negative the % operator does not follow the normal mathematical convention. In these cases the result has the same sign as the first operand. For example:
		int a= -8 % 2;        // a takes the value  0 since  -8=-4*2+0
		int b= -7 % 3;        // b takes the value  1 since  -7=3*-2-1
		int c=  7 %-3;        // c takes the value  1 since   7=-3*-2+1
		int d=  -7 %-3;       // d takes the value  -1 since -7=-3*2-1
		
		
This behavior is consistent with the c and c++ programming languages.

Example programs

The following two programs demonstrate the operations discussed above.
/*
This program demonstrates basic integer arithmetic operations variables in Java
*/
class  BasicVariables2
{
 
public static  void main(String[] args)
  {
       
   
int iAnInteger=77;
   
int iASecondInteger=87;   
    System.out.println
("the value stored in iAnInteger is");   
    System.out.println
(iAnInteger);
    System.out.println
("the value stored in iASecondInteger is");
    System.out.println
(iASecondInteger);
   
    iAnInteger++;
    System.out.println
("incrementing iAnInteger gives");

    System.out.println
(iAnInteger);
   
    iAnInteger--;
    iAnInteger--;
    System.out.println
("decrementing iAnInteger twice gives");
    System.out.println
(iAnInteger);
   
    iAnInteger=
5+iASecondInteger*4;
    System.out.println
("5+iASecondInteger*4 gives");
    System.out.println
(iAnInteger);

    iAnInteger=
5-iASecondInteger/4;
    System.out.println
("iAnInteger=5-iASecondInteger/4 gives");
    System.out.println
(iAnInteger);

    iAnInteger=iASecondInteger %
2;
    System.out.println
("iASecondInteger % 2 gives");
    System.out.println
(iAnInteger);
   
   
 
}
}

To compile and run from the command line:
$
$javac BasicVariables2.java
$ java BasicVariables2
the value stored in iAnInteger is
77
the value stored in iASecondInteger is
87
incrementing iAnInteger gives
78
decrementing iAnInteger twice gives
76
5+iASecondInteger*4 gives
353
iAnInteger=5-iASecondInteger/4 gives
-16
iASecondInteger % 2 gives
1
$ javac BasicVariables2.java
$ java BasicVariables2
the value stored in iAnInteger is
77
the value stored in iASecondInteger is
87
incrementing iAnInteger gives
78
decrementing iAnInteger twice gives
76
5+iASecondInteger*4 gives
353
iAnInteger=5-iASecondInteger/4 gives
-16
iASecondInteger % 2 gives
1
$ 

/**
This program demonstrates the modulus operator in Java
*/
class  ModulusJava
{

 
public static  void main(String[] args)
  {
   
// integer modulus operator
   
int a=8%2;
   
int b=7%3;
   
int c=-7%3;
   
int d=7%-3;
   
int e=-7%-3;
   
int f=0%3;
   
int g=-0%3;

    System.out.println
(a);
    System.out.println
(b);
    System.out.println
(c);
    System.out.println
(d);
    System.out.println
(e);
    System.out.println
(f);
    System.out.println
(g);

   
//floating modulus operator
   
double da=8.0%2.0;
   
double db=7.0%3.0;
   
double dc=-7.0%3.0;
   
double dd=7.0%-3.0;
   
double de=-7.0%-3.0;
   
double df= 0.0%3.0;
   
double dg= 7.0%0.0;
   
double dh= 0.0%0.0;

   
    System.out.println
(da);
    System.out.println
(db);
    System.out.println
(dc);
    System.out.println
(dd);
    System.out.println
(de);
    System.out.println
(df);
    System.out.println
(dg);
    System.out.println
(dh);
   
 
}
}

To compile and run from the command line:
$ javac ModulusJava.java
$ java ModulusJava
0
1
-1
1
-1
0
0
0.0
1.0
-1.0
1.0
-1.0
0.0
NaN
NaN
$ 


Previous Using primitive data types in Java Next Basic string operations in Java

Custom Search

Tutorial
Your First Java Program
Primitive data types in Java
Basic arithmetic operations in Java
Using Strings in Java
More Strings in Java
The Java if statement
Loops in Java
The entire content of this site is subject to copyright.