Difficulty: Beginner
Estimated Time: 30 minutes

Introduction to Programming - Part 3: Operators

WORK IN PROGRESS

Armed with knowledge of data types in part 2, now its time to do something with them. This lesson will look at operators so we can do something interesting with data.

Key Takeaways

  • Know the standard operators in Java, and how to use them.

Don’t stop now! The next scenario will only take about 10 minutes to complete.

Introduction to Programming - Part 3

Step 1 of 4

Operators

Do Something Useful

Operators: they make programming useful. Without them programs would do, pretty much, nothing. What is it? An operator is a symbol that performs an action on operands. In Java, it can be one, two or three operands.

What is an operand? It's the thing being operated on by the operator. That could be a few different things. In its simplest form, it would be, perhaps, a number. For example:
1 + 2
"1" and "2" are operands. "+" is the operator.

The operands don't have to be numbers. They could be variables, or even methods (covered in another lesson). They can even be the result of other operators!

Before we get into those details, let's talk about the different types of operators.

Unary Operators

Unary operators operate on one operand.

You've already seen one in the previous lesson with the increment operator: ++
It simply adds 1 to the operand.

int someValue = 0;
someValue++; // Now someValue equals 1.

There's also a decrement operator, which subtracts 1.

someValue--;

Unary operators can use prefix or postfix notation.
Prefix notation has the operator before the operand. ++someValue;
Postfix notation has the operator after the operand. someValue++;

Does the order matter? It can. Try it out. Let's write a simple program to see what happens when we print values with prefix vs. postfix. Copy this skeleton code to the editor window.

public class Operators {
  public static void main(String args[]) {
    // Your code will go here.
  }
}

Add this code to the editor. Type it!

// Starting value
int baseValue = 0;

System.out.println("*************************************");

int firstValue = ++baseValue;
// Print the values
System.out.println("firstValue: " + firstValue );
System.out.println("baseValue: " + baseValue);

int secondValue = baseValue++;
System.out.println("secondValue: " + secondValue); 
System.out.println("baseValue: " + baseValue);

int thirdValue = baseValue--;
System.out.println("thirdValue: " + thirdValue);
System.out.println("baseValue: " + baseValue);

System.out.println("baseValue--: " + baseValue--); // Notice the decrement here
System.out.println("Final baseValue value: " + baseValue);

System.out.println("*************************************");

Before you compile and run it, try to predict the output. Write it down.

After making your prediction, compile and run the code. Here are the commands for the last time (after this I will assume you know how to compile and run your code).
javac Operators.java
java Operators

Did your prediction match the actual results? The reason they turned out like they did is because of operator precedence rules. If you go back to rules of mathematics, it is simply the enforced order of operations. We will talk about that more later in this lesson. For now, it is enough to know that order matters.

Here is a list of all the unary operators in Java. Try them out in the editor window to see them in action.



OperatorDescriptionExample
+ Unary plus operator. Indicates positive values.
int myNumber = +1;
- Unary minus operator. Indicates negative values and negates expressions.
short myNumber = -1;
++ Unary increment operator. Increments a value by 1.
myNumber++;
-- Unary decrement operator. Decrements a value by 1.
myNumber--;
! Logical compliment (sometimes referred to as "not&quot). It inverts the value of a boolean.
boolean changeMe = true;
// Now it's false.
changeMe = !changeMe;
            

Let's move on to binary operators.

Operators.java