Java has a well defined set of rules for operator precedence. Operator precedence is similar to the order of operations from an Algebra class. We'll begin with a preliminary list of operators necessary for the majority of the programming techniques we will encounter. The precedence levels will be numbered from one to ten with one being the highest precedence (operations that must be done first) and ten being lowest precedence (operations that are done last).
Grouping uses parenthesis to identify which operations should be completed first. Grouping is executed exactly the same way as in mathematics, you start with the inner most set of parenthesis and work your way out one layer at a time.
Example:
double discriminant = (b*b) - (4 * a * c);
Java uses a period as the method selector. The period is placed between an object's name and a method name.
Example:
String car = "Corvette";
car.length(); // The String car is connected to the method length()
// The result would be an integer value of 8.
"car".length();// The length of the literal string "car" is 3.
Unary plus and minus are treated the same way as a positive or negative symbols in mathematics. Consider an integer value of -2, here the unary minus is a negative symbol. However, consider the expression -x, the unary minus will now change the sign of any value stored in x. If x = -2, then -x = 2.
Example:
int value = -10;
System.out.println(-value); // The result of -value will be 10
Instantiation is the process of creating a new Java object. When the Java keyword "new" is used then an object is created and memory is allocated to store data in the instance variables associated with the object.
Example:
Scanner input = new Scanner(System.in); // input is the identifier for a new Scanner object
Casting is a technique that converts one data type or object into another. This can be done when declaring a variable, during computations, printing, or accessing methods.
Examples:
int num = (int)(3.141592); // The result is num = 3
double x = (double)(1); // The result is x = 1.0
System.out.println((Object)(num));
// The result is that num has data type Object just for this line of code
Multiplication & Division
Multiplication is performed exactly as in any elementary math class. Java uses an asterisk to perform multiplication.
Example:
int x = 2;
double y = 3.5;
double product = x*y;
// The result is product = 7.0 because double is a superset of integer, so all integers can be converted to double when performing arithmetic calculations.
However, division is a little trickier. Consider the following line of code:
int x = 8 / 3; // The result is x = 2 not 2.66666 because x is an int data type so the result must be an int.
Consider the statement where x is now a double. There is still a problem.
double x = 8 / 3; // The result is now x = 2.0 not 2.66666 because 8 / 3 has an integer division result of two that is converted into a double data type.
Finally, we can get the desired result by expressing one or both of the values decimals.
double x = 8.0 / 3; // The result is now x = 2.666666...
Modulus
Modulus is the process of calculating the remainder when dividing two integers. Java uses the following symbol % to perform modulus calculations. Modulus has practical applications pertaining to time, inventory, or any topic that involves counting.
Example:
int remainder = 8 % 5; // The result is 3 because 5 goes into 8 once with 3 left over.
Addition & Subtraction
Addition and subtraction are performed exactly as in any elementary math class. Again, we must consider the data types we are using the results they will return to avoid errors in our programs.
Example:
int x = 2;
double y = 3.5;
double sum = x + y;
// The result is sum = 5.5, because double is a superset of integer, so all integers can be converted to double when performing arithmetic calculations. However, trying to store a double into an integer would result in an error.
int a = 2;
double b = 3.5;
int sum = x + y; //ERROR! Cannot store a double into an int data type!
Assignment Operator
The equal symbol is also referred to as an assignment operator. After you declare your variables you must initialize them. This can be done by setting them equal to a literal value using the equal symbol "=". Variables can also be set equal to expressions. Expressions are a collection of constants, variables, and/or Strings that defines what is stored by a variable. Every example above that uses an equal symbol "=" is demonstrating how to use an assignment operator.
The assignment operator has Right to Left Associativity. This means that everything on the right of the equal sign is evaluated first, then the result is stored into the variable identified on the left of the equal sign.