Now that we understand how to make variables, the different datatypes available to us in Java, and how to write code others can understand, let’s try to manipulate some data with Java operators.
Why Operators are Important & Useful
Remember that we program to capture and manipulate information, data, to create new data for users to consume. We’ve learned how to capture data and store it in variables, so this chapter will focus on manipulating our data. We manipulate our data by running operations on it by with operators. Operators allow us to assign data to variables, do math with data, connect data, and compare data.
There are an infinite number of operations you can run on your data with only a small, finite number of operators available to you. This is both good and bad. Bad because you are limited to how many ways you can operate on the data you have. Good because you only need to remember a few special symbols that let you manipulate your data. On the plus side, you can combine the few operators you have to do an infinite number of operations.
Java provides us 42 operators to work with. Twenty-seven of the operators are mere combinations of the fifteen base operation symbols. More good news is you only need to know a few of those to begin working with Java. Many experienced programmers don’t make use of all of them, so don’t worry if you don’t understand them all at first. I will equip you with the most widely used operators so you can begin building fun and useful programs. You’ve seen a good number of Java’s operators being used in this book already, but let’s take a look at the ones you need to remember.
When you were learning how to create and use variables, you saw the assignment (=) operator used countless times. As I would say, “my name is Lincoln,” to tell people what my name is so they can use it later to refer to me, the equals symbol is used to set a variable equal to a piece of data for later use.
Additive operators enable us to do addition and subtraction much like you learned in the first or second grade of school. The only difference is you don’t have to do any of the hard work like carrying numbers because Java does it for you. You will often use the addition + operator to add numbers or connect data and the subtraction - operator to subtract numbers.
String concatenation is useful when you want to connect multiple character strings together or connect numbers with strings as we see in that code snippet. When we concatenate an int with strings, the int is converted into a String; that is also the case for any datatype that is not a String.
Multiplicative operators let us do multiplication and division on numbers like we learned after first grade. The computer handles all of the long division and carrying for us. You can make use of the multiplication * operator to multiply and the division / operator to divide numbers
We often want to know if two things are equal to or not equal to one another. A classic example is checking if an apple is equal to an orange. We can evaluate equality relationships by use of Java’s equality operators: == lets us check if two things are equal while != lets us check if two things are not equal. Let’s try our apples and oranges example by using numbers to represent the two:
Here, the first boolean variable, appleEqualsOrange, will be set equal to false because the comparison made by the equality operator is false — an apple represented by the number one (1), does not equal an orange, which is represented by the number two (2). However, the last two booleans will be set equal to true because the comparisons made by the equality operators in the last two lines are evaluated to true; an apple (1) is equal to another apple, and an apple (1) is definitely not equal to an orange (2).
Like in math, we can use relational operators to compare, or gauge the relation between two data points in Java. We can check if one data point is greater than or less than another by using the > or < operators respectively:
Both of those boolean variables will be set to true when the code is executed because the comparisons made by the relational operators are true .
Further, we can check if a data point is greater than or equal to another as well as if a data point is less than or equal to another by use of the >= and <= operators respectively:
The first three boolean variables will be set equal to true because the comparisons made by the relational operators are true. However, the last one will be false because we all know two is not less than nor equal to one.
In later chapters, we will learn how to use our equality and relational operators to make decisions and control the flow of our code.
We can combine operators to make more powerful operators, too. With combined operators, we can clean up and optimize our code. We can increase or decrease the value of a variable by doing the following:
The ++ operator says, “I see your current numerical value and raise it by one,” while the -- operator decreases the current value of a variable holding a number by one (1). The += operator adds the number on its right to the value of the variable on its left and saves the sum in the variable; the -=, *= and /= operators work similarly.
There are a few others that are useful, but they require more explanation than is within the scope of this chapter. In later chapters, you will learn how to logically check conditions of data by combining relational and equality operators with logical operators in order to make decisions with control flow statements (decision-making statements and looping statements). These are powerful tools that allow our code to do some things sometimes and other things other times.
This was originally published on Dec 18, 2015.