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.
//use = to set a variable equal to my name
String myName = "Lincoln W Daniel";
//this will print out: “Lincoln W Daniel”
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.
//use + symbol to add two numbers
int numApples = 100;//"num" means "number of"
int numOranges = 10;
int numFruits = numApples + numOranges;/*use + symbol to connect, concatenate, strings*/
String firstName = "Lincoln";
String lastName = "Daniel";
String fullName = firstName + " " + lastName;//use - symbol to subtract two numbers
int four = 9 - 5;//use + to concatenate all of our data to print a statement
System.out.println(fullName + " has " + numFruits + " fruits.");
//prints "Lincoln Daniel has 110 fruits.";
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
//use * to multiply two numbers
int oneHundred = 20 * 5;//use / to divide two numbers
int six = 12 / 2;
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:
int apple = 1;
int orange = 2;boolean appleEqualsOrange = apple == orange;
boolean appleEqualsApple = apple == apple;
boolean appleDoesNotEqualOrange = apple != orange;
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:
int one = 1;
int two = 2;boolean twoIsGreaterThanOne = two > one;
boolean oneIsLessThanTwo = one < two;
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:
int three = 3;
int twenty = 20;boolean threeIsLessThanOrEqualToTwenty = three <= twenty;
boolean twentyIsGreaterThanorEqualToThree = twenty >= three;
boolean oneIsLessThanOrEqualToOne = 1 <= 1;
boolean twoIsLessThanOrEqualToOne = 2 <= 1;
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:
int count = 0;/*Combining Additive Operators
& the Assignment Operator*/count++;//count now equals (0 + 1) = 1
count += 6;//count now equals (1 + 6) = 7
count--;//count now equals (7 - 1) = 6
count -= 2;//count now equals (6 - 2) = 4/*Coming Multiplicative Operators
with the Assignment Operator*/count *= 5;//count now equals (4 * 5) = 20;
count /= 10;//count now equals (20 / 10) = 2;
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.