Shorthand Operators

Things To Do First

Things To Do Later

Shorthand Assignment Operators

There are some special assignment operators you can use as shortcuts to certain kinds of assignment statements. To understand how these can be used, look at the program listing below. What is the output of this program? What is the value of the variable "sum" during execution of this program?

public class  AssignOps {
    public static void main(String[] args) {
		
        int sum = 10;

        System.out.println("The value stored in sum is " + sum);
        sum = sum + 20;
        System.out.println("The value now stored in sum is " + sum);
    }
}

This small program demonstrates a situation where you can use the special += assignment operator. This operator, the "plus-equals" operator, adds the operand on the right to the operand on the left. If you rewrote the program using the += operator, you'd have:

public class  AssignOps {
    public static void main(String[] args) {
		
        int sum = 10;

        System.out.println("The value stored in sum is " + sum);
        sum += 20;
        System.out.println("The value now stored in sum is " + sum);
    }
}

There are also similar operators to perform subtraction, division, and modulus. You can see them in Table 2.4 in section 2.13 of your text.

Exercises

Determine, without coding a program, the output of the following code segment:

int counter = 1;
int increment = 2;
System.out.print(counter + " ");
counter += increment;
System.out.print(counter + " ");
counter *= increment;
System.out.print(counter + " ");
increment /= 2;
counter -= increment;
System.out.println(counter);
System.out.println("increment: " + increment);

[solutions]

Unary Operators

In addition to the assignment operators, there are also some unary operators. Recall that regular operators, such as +, * and += are binary operators - they require two operands, one on each side. Unary operators require only 1 operand. You can see the unary operators listed in Table 2.5 in Chapter 2.14 of your textbook.

The code listing below demonstrates how the increment operator works:

public class  UnaryOps
{
	public static void main(String[] args)
	{
		int intNum1 = 2;
		int intNum2 = 0;

		System.out.print("intNum1: " + intNum1);
		System.out.println("  intNum2: " + intNum2);
		System.out.println();

		intNum2++;
		System.out.println("increment intNum2: " + intNum2);
		intNum2++;
		System.out.println("increment intNum2: " + intNum2);
		intNum2++;
		System.out.println("increment intNum2: " + intNum2);

		intNum2 += intNum1;
		System.out.println();
		System.out.print("intNum1: " + intNum1);
		System.out.println("  intNum2: " + intNum2);

	}
}

The output for this program:

intNum1: 2  intNum2: 0

increment intNum2: 1
increment intNum2: 2
increment intNum2: 3

intNum1: 2  intNum2: 5

Unary operators in Java can go on the left or the right of the operand. The difference is in when the increment actually takes place. Some of the exercises below demonstrate this.

Practice

Given that x=5, y=2, and z=10, what do you think would be the value of "result" after the following assignment statements?

result = z/y;
result += x * y;

result = ++x + y;

result = x++ + y;

For each of the examples above, type the code into a Java program and print/display the value of "result". Do you get the output you expected? Why or why not?

When the ++ or -- operator is to the left of the operand, we refer to it as a pre-fix operator. When the ++ or -- operator is to the right of the operand, we call it the post-fix operator.

The pre-fix operator has higher precedence than the post-fix operator. In a statement with multiple types of operators, the pre-fix operator will execute before most other operators. The post-fix operator will usually execute after the other operators. See your Operator Precedence chart in Appendix C of your textbook for more information.

Important!
In order to use the unary operators on a variable, that variable must be initialized! To understand why, compile the following code:
public class TestUnaryOps {
   public static void main(String[] args) {
      int x;
      x++;
      System.out.println(x);
   }
}

When you compile, you'll get the error message

TestUnaryOps.java:4: variable x might not have been initialized
      x++;
      ^
1 error

This error message is telling you that the variable x was declared, but was not initialized with a value. Unary operators and the special assignment operators (such as +=) do not work on variables that have not yet been initialized!

Exercises

1. Examine the code listing below. What do you think the output should be? Write this down. Then run the program and see if you're correct.

public class Question2
{
	  public static void main(String[] args)
	  {
        int factor = 2;
        int sum = 10;
        System.out.println("sum is " + sum);
        sum *= factor;
        System.out.println("sum is now " + sum);
        sum *= factor;
        System.out.println("sum is now " + sum);
        sum *= factor;
        System.out.println("sum is now " + sum);
    }
}

2. How do you think the output would change if you wrote the program in question 2 like this:

public class Question3
{
    public static void main(String[] args)
    {
        int factor = 2;
        int sum = 10;
        System.out.println("sum is " + sum);
        sum *= factor;
        sum *= factor;
        sum *= factor;
        System.out.println("sum is now " + sum);
        System.out.println("sum is now " + sum);
        System.out.println("sum is now " + sum);
    }
}

[solutions]