Math operators
You should be able to use all the standard math operators (addition +, substraction -, multiplication , division /). You should also know the modulus operator % (also known as the remainder operator).
Stuff to watch out for:
- integer division gives an integer result (1 / 3 is 0 not 0.33 repeating)
- if you want a double result use a double variable or cast to a double (declare a total to be a double if you are calculating an average or cast using ((double) total) / count
- Know the default order of operations and use parantheses to change the default order
- Use (x % 10) to get the rightmost digit of a number (for example see the 2007 free response question #1 http://coweb.cc.gatech.edu/ice-gt/1280. Also use x / 10 to get rid of the right most digit in a number.
- Use (x % 24) to get back to the beginning of an array that holds a charge for each hour of the day as seen in free response question 3 from 2009 http://coweb.cc.gatech.edu/ice-gt/1278. You can also use % when working with time (seconds, minutes, hours, etc).
- Integer.MAX_VALUE + 1 results in Integer.MIN_VALUE and Integer.MIN_VALUE - 1 is Integer.MAX_VALUE. You might want to use Integer.MAX_VALUE as an initial value when looking for the smallest value in a collection (or even better just use the first value in the collection and loop through the rest).
- Double results are not exact due to round-off error and you should not use == with double values.
Link to this Page