Additive Operators
You have been “doing the math” since grade school. It is only logical that you would need to “do the math” in code as well. That is what this part of the unit is all about...the operators used to “do the math”.
Printing to the Console
Before we get into details about additive operators, let's review printing information to the console window in NetBeans. Remember, System.out.println(); is a way to print what is between the parentheses into the console window in NetBeans. If nothing is between the parentheses, then a blank line is printed. If you need to join String literals with variables, that is called concatenation. The plus sign plays double duty, it adds numbers and concatenates data. Please pay attention to the examples on this page. You will need to print data to the console window, as well as add numbers.
The Additive Operators
In math, additive refers to both addition and subtraction. You can think of subtraction as just adding with negative numbers.
- The plus sign (
+) is used for math addition andStringconcatenation. - The minus sign (
-) is used for math subtraction and would also be used to represent a negative number.
Adding
- You can add numbers and number variables.
- If you have a
Stringvariable that should contain a number, you must- Validate that the
Stringactually contains a number value - Convert the
Stringto a number data type before doing the math
- Validate that the
Do not worry, validating and converting a String variable value to a number value is easy with some built-in help from the programming language. We will go into that in detail later.
Remember, we have naming standards for our variables. My examples do not always follow those standards for the sake of clarity. However, you should always follow the Northwood Tech Software Development Standards. Please carefully review the code below. When reading the below Java code snippet, say “plus” or “add” when you are adding the numbers and “concatenate” or “concat” when you are joining a variable with a String literal.
// Adding Numbers
// Declare and initialize variables
int numberOne = 223;
double numberTwo = 543.57;
double totalCount = 0;
// Adding literal numbers directly
System.out.print("You can add number literals: ");
System.out.println(20 + 57);
// Adding number variables
totalCount = numberOne + numberTwo;
System.out.println("The sum of adding " + numberOne + " plus "
+ numberTwo + " is: " + totalCount);
Below are the results that would be printed to the console window based on the code snippet above. Please try to match the results with the code.
Did you notice that my totalCount variable has a data type of double? If you remember from the chapter on data types, a double can hold a fractional number. Anytime you add a whole number (int) and a fractional number (double), make sure your variable that holds the results can accept a fractional number.
Subtracting
- You can subtract numbers and number variables.
- If you have a
Stringvariable that should contain a number, you must- Validate that the
Stringactually contains a number value - Convert the
Stringto a number data type before doing the math
- Validate that the
Please carefully review the code below.
// Declare and initialize variables
double totalPrice = 0.0;
double highPrice = 1000.99;
double discountPrice = 199.95;
totalPrice = highPrice - discountPrice;
System.out.println("The new totalPrice is: " + totalPrice);
Below are the results that would be printed to the console window based on the code snippet above. Please try to match the results with the code.
In reality, we do not use literal numbers that often in programming. Typically, you get the value from an external source such as end-user input.
Combining Operators
I am sure you realize that just like math, you can combine adding and subtracting into the same statement. If needed, you can apply parentheses for operator precedence. Java does the calculations inside parentheses first, but more about the “order of operations” later...
What You Learned
- The term additive applies to both adding and subtracting
- The plus symbol is for adding, whereas the minus symbol is for subtracting
- You were also reminded that the plus symbol can be used for concatenation
- You learned that if your result can be a fractional number, then you need the data type for the result to be a
double
What's next?
The next chapter in Math Operators is Multiplicative Operators.