Math
Math is defined in the java.lang package, as the name suggests math methods provide a wide variety of mathematical functions. The rules of Explicit and Implicit casting should be adhered to when returning values of the same type. Some examples of casting and when to consider them.
int result = Math.ceil(1.1F);// Here we have a ceiling function for the float of 1.1// As the ceiling function returns a double// we need to ensure we cast our ceil to an int prior to return
int result = (int)Math.ceil(1.1F);// This will now return a 2Compilation errors can be easily avoided with the correct types being explicitly cast, it is important to note method returns when working with Math functions.
double result = Math.random() * 100;// The result of this code will be a random number between 0 and 100, not including 100.
double result = Math.round(Math.random() * 100);// Here, Math.round() rounds the random number to the nearest long value, and the result is stored in a double. Example: 22.0
int result = Math.round(Math.random() * 100);// This will result in a compilation error because Math.round() returns a long, but we are trying to store it in an int. A cast is needed.
int result = (int) Math.round(Math.random() * 100);// An additional explicit cast allows for us to now return an int as random number// this will return 22 (random number between 1-100)
int result = (int) Math.random() * 100;// This is an interesting example and will return 0// our explicit casting is being applied to the Math.random() method call not to the formula// Math.random returns a value between 0 and 1 thus being cast as an integer will result in 0
int result = (int) (Math.random() * 100);// This would be the correct method to return an integer between 0 and 99 (inclusive)Arithmetic Expressions
Section titled “Arithmetic Expressions”An expression is a piece of code that produces a value, Java has the same arithmetic operators we have in math.
Addition = +Subtraction = -Division = /Multiplication = *Modulus = %Increment = x++ || ++xAugmented/compounded Assignment Operators = += -= *= /=In Java you must explicitly state the type for operands and return to the intended return format. As an example if you want a floating return of division you would need to apply:
float result = (float)10 / (float)3;
Prefix/Suffix Operands
Section titled “Prefix/Suffix Operands”Prefix
A value with a prefix operand ++X will first increment on itself prior to assigning it to the expression.
Suffix
A value with a suffix operand x++ will first look to the expression prior to incrementing on itself.
Order of Operations
Section titled “Order of Operations”- Parenthesis
()has the highest priority. - Multiplication and division are 2nd highest order
*/ - Finally Addition and subtraction
+-
Formatting Numbers
Section titled “Formatting Numbers”Java has an in built methods to assist in formatting numbers in the java.text package called NumberFormat.
import java.text.NumberFormat;
NumberFormat = currency = new NumberFormat()// This will not work as the class is abstract (discussed more later)// As the class is abstract it is not instantiated.
NumberFormat currency = NumberFormat.getCurrencyInstance();// This would create an instance of the class and return it// Instead of using new operaters we use these methods.// These are called Factory Methods
String result = currency.format(1234567.891)// This method would return a string representation of the number formatted as currency// $1,234,567.89