Math Functions

Math Operators

 

An interesting math function is pulling out the digits of a number. To do this, you need to use a combination of divide and mod.

public class ParseDigits
{
   public static void main (String args [])
   {new ParseDigits();
   }


	public ParseDigits()
	{
     int number = 123;
     int digit1 = number/100;
     int digit2 = (number - (digit1*100))/10;
     int digit3 = number%10;
     System.out.println(digit1+" "+digit2+" "+digit3);
   }
}
Order of Operations

Java order of operations is something like BEDMAS in math. It is a little more involved than the BEDMAS rule because there are more operators than brackets, exponents etc.

This is not a complete list of operators. It is in order from performed first to performed last. You are advised to use brackets to show the order and not rely on the order of operation. Brackets make the code easier to read.

Operator Associativity
()  [] non-associative
new non-associative
. left-associative
negative sign, ! right-associative
* / % left-associative
+ - left-associative
> < <= >= left-associative
== != left-associative
&& left-associative
|| left-associative
= *= /= %= -= += right-associative