Java has a number of arithmetic operations that can be performed.  Order of operations is the same as normal arithmetic, with a few extra considerations. 
 
Operator Meaning Order of Operation
- unary minus first
+ unary plus first
* multiplication second
/ division second
% remainder second
+ addition last
- subtraction last

 

All of these operators can be used on floating point numbers and on integers. 

An integer operation is always done with 32 bits or more. If one or both operands are 64 bits (data type long) then the operation is done with 64 bits. Otherwise the operation is done with 32 bits, even if both operands are smaller.

/ means "integer division" if both operands are integers, and means "floating point division" if one or both operands are floating point.

The result of integer division is always an integer; if the exact result calls for a decimal fraction, that part of the result is dropped (not rounded.)

Java will regard 9/2 as integer arithmetic and give you

9/2 = 4   (Note the decimal part is dropped off)

So 1/2 + 1/2 will evaluate to 0.

You can fix this by writing 1.0/2.0 + 1.0/2.0  This will cause them to be evaluated as floating point numbers.

Here is a more confusing case of the same thing:

3.7 + 9/2
 

The division will be done first, because division is done before addition. The result, 4, is an integer. Now floating point 3.7 is added to integer 4 to get floating point 7.7

Integer arithmetic may be used in parts of an expression and not in others, even though the final value of the expression is floating point.

 

Remainder

% will calculate the remainder for a division.  For example, 17%5 will evaluate to 2  It is limited to use for integer division.  You cannot use it with floating point division.

When doing integer division, by careful use of / and % you can calculate exact answers when dividing.

The remainder operator can be used with negative integers. The rule is:

  1. Perform the operation as if both operands were positive.

  2. If the left operand is negative, then make the result negative.

  3. If the left operand is positive, then make the result positive.

  4. Ignore the sign of the right operand in all cases.

Examples

Here is a sample program for you to experiment with:

//Java program to experiment with remainder operator
//Created by M Meijers  March 2005
class Remainder
{
  public static void main ( String[] args )
  {
    int quotient, remainder;

    quotient  =  17 / 3;
    remainder =  17 % 3;
    System.out.println("The quotient : 17/3 = " + quotient );
    System.out.println("The remainder: 17%3 = " + remainder );
   
  }
}

The following program will show you the difference between using integer vs floating point division:

//Java program to highlight integer vs floating division
//Created by M Meijers March 2005

class Arithmetic
{
	public static void main ( String[] args )
	{ 
		int int1=3, int2=4; //declare and initialise 2 integer variables
		float float1=3, float2=4; //declare and initialise 2 floating variables
		int intAnswer; //declare integer answer
		float floatAnswer; //declare floating answer

		intAnswer=int1/int2; //calculate integer division answer
		floatAnswer = float1/float2; //calculate floating division answer


		System.out.println("Integer 3/4= " + intAnswer );
		System.out.println("Floating 3/4 =" + floatAnswer );

	}
}

 

Write a program that will calculate 3/4 + 3/4 using both floating point division and integer division.