Java: Working with Operators - dummies

By John Paul Mueller

Operators are an essential part of Java application development. They do precisely as their name implies — they operate on the value contained within a primitive type or object in some way. Precisely how it operates on the value depends on the operator.

In some cases, an operator will change the value in the variable, but in other cases, the operator simply uses the value to perform a specialized task, such as comparing two values. Java provides a wealth of operators that perform every task imaginable.

The best way to understand how operators work and what they do is to group them into categories and then look at each category individually. Each of the following sections describes a different operator category and shows how to use the operators within that category to your advantage. Here’s a quick overview of the operators in this chapter:

  • Assignment operators place the value you define into the variable. The basic assignment operator replaces the value in the variable. Combined assignment operators perform a math-related task and update the value in the variable. It’s nearly impossible to write an application without making variable assignments of some sort.

  • Arithmetic operators perform any math-related task, including addition, subtraction, multiplication, and division. Most applications need to perform math-related tasks, even if the user is unaware of the math functionality. You’ll discover as the book progresses that math is an inescapable part of programming.

  • Unary operators cause the variable to interact with its own content in some way, such as incrementing (adding to) or decrementing (subtracting from) the variable’s value. In many respects, unary operators provide a shorthand that makes application code easier to understand and faster to write.

  • Relational and conditional operators perform a comparison of some type and provide a result that reflects that comparison. These operators make it possible for applications to make decisions based on the content of variables.

Java 8 adds one new operator that meets the traditional meaning of the term, the arrow operator (->). This operator is used in a special circumstance to work with lambda expressions (a method of creating functions). Remember, because the -> is used only for this special purpose, you don’t need to worry about it.

In addition, some people are calling the Optional object type an operator. It actually is a type that helps you avoid certain types of errors. Again, you don’t need to worry about it until you are farther along in your Java experience. Otherwise, Java 8 operators behave just like those found in previous versions of Java.

If every operator had precisely the same precedence (priority), chaos would result because the computer wouldn’t know which task to perform first. Because computers are logical and require well-ordered instructions, the developers of Java had to create an order in which operators are used when more than one of them appears in a single line of code.

The order of precedence helps determine which task to do first, even if the order isn’t clear from the way the code is written. You need to understand precedence in order to write good code. Otherwise, you may end up with code that assumes that the computer will be working with one operator first and only find out later that the computer really worked with another operator first.

Precedence defines the order in which tasks are performed on a computer. Think priority when you see precedence. Just as you prioritize the work you need to do, the computer must also prioritize the work it must do. The order of precedence is essentially the same for all computer languages, and this order is borrowed from mathematicians.

In short, the rules you learned for performing math tasks in school are the same rules that the computer uses to perform tasks. As a result, you’ll likely find it easier to learn the order of precedence than you might initially think.