Operators and Operands

Mathematical Operators Binary Operators Unary Operators
Logical Operators Summary Practice
Exercise

Learning outcomes

Download pdf version

Introduction

In listing 1.1 we have already met a number of operators which were sufficiently similar to their pen and paper equivalents that they required no explanation. Those we have met are listed along with a short explanation in the table below.

Operator

explanation

=

Assignment

*

multiplication

-

subtraction

 

We first met the assignment operator in line 6 of Listing 1.1

        hours = 30;

From our familiarity with algebra we might be tempted to read this line as the value of the variable hours is 30. This in fact would be wrong. According to the syntax of Java the line should read as store the value 30 in the variable hours or assign the value 30 to the variable hours. The moral of this tale is that not all operators behave within Java (or within most other programming languages) the way they behave in arithmetic and algebra. Thus in the rest of this chapter we shall give a detailed explanation of the operators that we have available in Java

Mathematical operators

Go to top

In listing 2.1 below we have examples of some of the mathematical operators, two of whom we have met already. The assignment operator also occurs.

Listing 2.1

 1

public class Untitled2

 2

{

 3

    public static void main(String[] args)

 4

    {

 5

        int first,second,sum,difference,product,quotient;

 6

        first = 10;

 7

        second = 5;

 8

        sum = first + second;

 9

        difference = first - second;

 10

        product = first * second;

 11

        quotient = first / second;

 12

        System.out.println("Sum " + sum);

 13

        System.out.println("Difference " + difference);

 14

        System.out.println("Product " + product);

 15

        System.out.println("Quotient " + quotient);

 16

    }

 17

}

 

At line 5 we declare our variables as before. Here, however, we have declared them as int, which means integer. Once again we meet the assignment operator in line 6 where the instruction is assign the value 10 to the variable first.  Let us, however, dwell for a little longer on this line. The assignment operator,”=”, has the variable first to the left of it and the constant 10 to the right. The variable and the constant are both referred to as operands. Again in line 7 the variable second and the constant 5 are the operands of the assignment operator.

In line 8 things get more complicated. Again we have the assignment operator with the variable sum as the left hand operand. The right hand side, however is not a constant or a variable but an expression, first + second.  Here we meet the add operator “+” with its two operands, the variables first and second. The sequence of operation in this case is that the values stored in the variables first and second are first added together and then the result is assigned to, or in other words stored in, the variable sum.

In lines 9 and 10 we meet the subtraction and multiplication operators which we met before in Listing 1.1.

In line 11 we meet the division operator, “/”.

The addition, subtraction, multiplication and division operators we met above behave in the manner we would expect them to behave. In lines 12 - 15, however, we meet the addition operator again, and here it behaves differently to how it behaved in line 8. In line 12 we have this expression between the brackets – “Sum “ + sum. We would expect the addition operator to add two numeric values but “Sum “ is clearly a piece of text, which would make nonsense of the expression. It appears to be acceptable to Java however. The reason is that in this particular instance Java assumes that the meaning of the operator is to join the text of the left operand with the numeric data on the right operand as one piece of text.

The output of the programme is shown below.

Sum 15

Difference 5

Product 50

Quotient 2

Binary Operators

Go to top

If we look again at lines 6 – 11 of Listing 2.1 we notice that there is always a variable as the left hand operand of the assignment operator “=”, and that there is either a constant, expression or another variable as the right hand operand.  This indicates that the assignment operator requires an operand on both sides of it. It is therefore referred to as a binary operator. The addition, subtraction, multiplication and division operators also required operands on each side of them, and for the same reason as the assignment operator, they are also referred to as binary operators.

Unary Operators

Go to top

Some operators require only one operand. Those are referred to as unary operators. In the example below there are examples of both unary operators and some new binary operators.

Listing 2.2

 1

public class Untitled3

 2

{

 3

    public static void main(String[] args)

 4

    {

 5

        int number;

 6

        number = 10;

 7

        System.out.println(number);

 8

        System.out.println(number++);

 9

        System.out.println(++number);

 10

        System.out.println(number+=6);

 11

        System.out.println(number-=8);

 12

        System.out.println(number*=7);

 13

        System.out.println(number/=2);

 14

        System.out.println(number--);

 15

        System.out.println(--number);

 16

        System.out.println(number);

 17

    }

 18

}

 

Lines 5 and 6 above should not require any explanation at this stage. What we shall be looking at is the actual output of the programme, which appears to the right and comparing this to the programme lines that cause it. Thus in the table at right the left column corresponds to the lines in Listing 2.1 and the right column corresponds to the output of that line. Thus it is clear that since we valued number to 10 in line 6 that in line 7 the output is 10.

At line 8 we meet the unary operator “++”. It is unary since it has only one operand – the variable number. It is an increment operator, that is it adds 1 to the value of the operand. Thus since number previously had a value of 10 then after number++ it should have a value of 11.

If that is the case then why is the output of line 8 the same as the output of line 7, that is 10?  The reason is that in number++ the increment operator comes after the operand. In this case what happens is that the value of number is first printed, in other words 10 is printed and then the value of the varible is incremented to 11. 

At line 9 we meet a different scenario. Notice here that the operator comes before the operand. When this occurs the value of number is first incremented and then it is printed. Since number already had a value of 11, it will now be incremented to 12 and then its value is printed. Hence the output of line 9 is 12.

Thus the unary operator “++” can come either before or after its operand.  When it comes before its operand it is referred to as a prefix operator, whereas when it comes after its operand it is referred to as a postfix operator.

At line 10 we meet the operator “+=”. This is a binary operator since it requires an operand on either side. In this case number+=6 means add 6 to the value of the variable number. Since the variable already had a value of 12, adding 6 to it increases it to 18.

A similar operator greets us at line 11, the “-=” operator. Again it is a binary operator which, in this case, subtracts 8 from the value of number. This reduces its value to 10 which is what is printed at line 11.

Line 12 has the operator “*=”, which multiplies the value of number by 7, thus increasing it to 70, which is the output.

Line 13 has “/=” which divides number by 2, giving an output of 35

At line 14 we meet our next unary operator – “—“. Again it occurs after the operand, which means that it is a postfix operator. It means to subtract 1 from the value of the variable. Since it is a postfix operator what happens is that the original value of number – 35 that is, and then the variable is revalued to 34.

At line 15 we meet the prefix version of “—“. Here the value of number is first reduced to 33 and then that value is printed.

Since line 16 does not change the value of the variable 33 is printed once again from this line.

Logical Operators

Go to top

Logical operators make comparisons between two values and depending on the requirement return a value of true or false. They are all binary operators, since they need to compare two values.

The equality operator “==” compares its two operands and if they have the same value it returns true, otherwise it returns false

The less than operator “<” returns true if the operand to the left is smaller than the operand to the right, otherwise it returns false.

The greater than operator “>” returns true if the operand to the left is larger or greater than the operator to the right, otherwise it return false.

The less than or equal to operator “<=” returns true if the operand to the left is smaller than, or equal to the operand to the right, otherwise it returns false.

The greater than or equal to operator “>=” returns true if the operand to the left is larger or greater than the operator to the right, otherwise it return false.

The not operator “!” is used to reverse the logic of the operand that follows it.

In listing 2.3 below the first three operators are examined

Listing 2.3

 1

public class Untitled4

 2

{

 3

    public static void main(String[] args)

 4

    {

 5

        int first, second, third, fourth;

 6

        first = 10;

 7

        second = 12;

 8

        third = 12;

 9

        fourth = 16;

 10

        System.out.println(first == second);       

 11

        System.out.println(first > second);       

 12

        System.out.println(first < second);

 13

        System.out.println(second == third);

 14

        System.out.println(second < third);       

 15

        System.out.println(fourth > second);       

 16

        System.out.println(third < fourth);             

 17

    }

 18

}

10

false

11

false

12

true

13

true

14

false

15

true

16

true

 

To the right is the output of the above code and the corresponding line numbers are printed in the left column.

In line 10 the variables first and second are compared for equality. Since they are not equal a value of false is returned.

In line 11 the same two variables are compared to check if first is greater than second. This is not the case and again false is returned.

In line 12 second and third are compared for equality. They are and thus the value true is returned.

The rest of the code works on similar logic.

The remaining three operators are tested in listing 2.3

Listing 2.3

 1

public class Untitled4

 2

{

 3

    public static void main(String[] args)

 4

    {

 5

        int first, second, third, fourth;

 6

        first = 10;

 7

        second = 12;

 8

        third = 12;

 9

        fourth = 16;

 10

        System.out.println(!(first == second));

 11

        System.out.println(first >= second);

 12

        System.out.println(first <= second);

 13

        System.out.println(!(second == third));

 14

        System.out.println(second <= third);

 15

        System.out.println(fourth >= second);

 16

        System.out.println(third <= fourth);

 17

    }

 18

}

 

10

true

11

false

12

true

13

false

14

true

15

true

16

true

Our biggest surprise here is the not operator – “!”. We meet it at line 10. Firstly notice the double brackets. The expression first == second is surrounded by its own set of brackets. This is because we wish to evaluate the value of this expression before applying the not operator to it. Since first has a value of 10 and second has a value of 12 then clearly first == second has a value of false. Next, when the not operator is applied to this, it reverses the logic and, thus, true is returned. It works in a similar manner in line 13, where second == third is true. The not operator reverses this so that line 14 returns false.

The other line to look out for here is line 14 where second <= third returns true due to the ‘equal to’ component being added to the operator.

Summary

Go to top

The operators we use in Java, and in most modern programming languages, are, for the most part similar to their equivalents in algebra and logic. All operators require at least one operand, and in most cases, two.

An operand is either a variable, a constant or an expression that appear either to the left or to the right of an operator. The operand on the left of the assignment operator must always be a variable.

The one mathematical operator that differs slightly from its mathematical equivalent is the assignment operator. Java distinguishes between this operator and the comparison for equality operator.

Operators are divided into two categories, mathematical operators such as addition, subtraction etc and logical operators such as equal to, less than, less than or equal to, etc.

Operators can also be divided between unary operators such as ++ and – who require only one operand and binary operators such as +, - etc who require two operands.

A prefix operator is a unary operator that comes before the operand while a postfix operator is a unary operator that comes after the operand.

Practice

Go to top

Copy Listing 2.1 into your computer, then compile and run it. Next change the values of the variables, then using pen and paper work out what the result should be and then run he programme again. Compare the output of the programme with your prior calculations.

Next do the same for Listing 2.2, but here change the sequence of the prefix and postfix operators, add more lines if you wish and also change the constants that follow the +=, -+ and the other incremental operators. Again work out on paper what the output should be and then run the programme.

Finally copy Listing 2.3, and experiment with changing the values of the variables, and also changing the comparisons, or adding new comparisons. Make sure that you try out the not operator a few times.

 


Exercises

Go to top

Exercise 2.1

What is the difference between an operator and an operand?

What is the difference between binary operator and a unary operator?

What is the difference between a prefix and a postfix operator?

Explain the differences between the “=” and the “==” operators.

What is an expression?

Exercise 2.2

What is the output of the programme below?

 1

public class Untitled5

 2

{

 3

    public static void main(String[] args)

 4

    {

 5

        int number;

 6

        number = 12;

 7

        System.out.println(++number);

 8

        System.out.println(number+=5);

 9

        System.out.println(number/=2);

 10

        System.out.println(number--);

 11

        System.out.println(--number);

 12

        System.out.println(number--);

 13

        System.out.println(number);      

 14

    }

 15

}