Java operators

Your course will require you to have an understanding of the different types of operators available in most programming languages. You should understand the terminology used so you can accurately answer exam questions. In order to write even the most simple of programs, you will also need to know what operators are available in the particular language you are using, and how to apply them.

Mathematical operators

The most obvious operators we would make use of in our programs would be the basic mathematical operations used in simple calculations. In Java these would be the following:

OperatorSymbol
Addition+
Subtraction
Multiplication*
Division/
Modulus%
Basic mathematical operators in Java

These operations are typically translated directly into machine code instructions on almost every CPU. That is to say machine code instructions exist on almost every CPU that perform integer addition, subtraction, multiplication, and division with a single instruction. Any more complex mathematical operations (for example trigonometry, powers, roots or any operations with real numbers) may need multiple machine code instructions to carry out the calculation.

Activity 1

Try the following in your Java development environment:

  • Declare a variable to hold an input temperature
  • Create a valid mathematical expression in Java syntax that converts the temperature in that variable from centigrade to fahrenheit.
  • Place the above expression inside a print statement
  • Test your program with the value 21 assigned to your input temperature variable (you should get the result 69.8 displayed on screen)
  • Modify your program so that the temperature is converted from fahrenheit to centigrade.
  • Test your program with the value 100 (you should get the result 37.77778)

Equality and relational operators

OperatorsSymbol(s)
equal to==
not equal to!=
greater than>
less than<
greater than or equal to>=
less than or equal to<=
Java equality and relational operators

Another common requirement in even the most simple of programs is to be able to compare two items of data. The above operators let you do just that. These operators are said to be binary operators because they need two items to compare.

Conditional operators

OperatorSymbols
Conditional AND&&
Conditional OR||
Ternary operator (if…then..else shorthand)? :
Conditional operators in Java

The conditional operators allow us to perform a boolean logic operation on two items of data, you may have realised that NOT is missing, that is because it only operates on one item of data so is covered in the next section. The result of the conditional operators AND and OR is a single boolean value, so for example:

inputTemp > 20 && inputTemp < 25

…returns true if the inputTemp variable contains a value between 21 and 24, otherwise it will return false. The ternary operators ? : which always come as a pair are slightly different. They compare two items of data and depending on whether the result is true or false, will assign one of two values to a variable, for example:

result = mark > 40 ? "pass" : "fail";

… if the variable mark contains a value over 40, the result variable will contain the string “pass” otherwise it will contain the string “fail”.

Unary operators

OperatorSymbol(s)
NOT!
increment++
decrement
Java unary operators

Unary operators only need one item to act on unlike binary operators which need two. For example:

flip = (!input); // If input was true, flip contains false

tries++; // Adds one to tries (cannot add more than one)

lives--; // Subtracts one from lives (one only, as above)

As with the basic mathematical operators, these operators usually have a machine code equivalent so could execute a single machine code instruction.

Activity 2

Try the following in your Java development environment:

Create an expression to see if a student is eligible for a student discount card.

  • It should use two variables: the first specifies if the person is currently studying full time or not (a boolean variable), the second specifies the person’s age.
  • The result variable should contain true if the person is between 16 and 25, and is studying full time, otherwise it should contain false.
  • Once you have got the above working and have tested with a few different values of age and whether the person is a full time student or not try to write a new version that uses the ternary operators.

Bitwise operators

OperatorSymbolExample (result)
bitwise AND&0b1010 & 0b1100 (1000)
bitwise OR|0b1010 | 0b1100 (1110)
bitwise XOR^0b1010 ^ 0b0110 (01100)
bitwise complement~~ 0b1010 (11110101)
Bitwise operators in Java

The bitwise operators operate on individual bits and generate a numerical result rather than a boolean value as you might expect from the conditional operators AND and OR. In the table below you can see that the bitwise AND of a binary value 0b1010 with the value 0b1100 generates a result of 1000. That is because only bit 4 of those two values is 1 in both values. All other bits have at least one zero at any particular position. The 0b prefix is Java syntax for specifying a binary value which makes things simpler for using and testing these operators.

Bit4Bit3Bit2Bit1
first value1010
second value1100
bitwise AND1000
A bitwise AND operation

Note that the bitwise complement operator is a unary operator but is in this section because it belongs with the other bitwise operators.

Activity 3

Use your development environment to try the examples above and your own values. Use your knowledge of binary conversion to check you understand the results (for bitwise complement you will need to understand how signed and unsigned numbers are represented, it’s best to use a byte to store the value for simplicity until you know what you’re doing).

Knowledge check

  • Name 5 different categories of operators
  • What is the difference between unary and binary operators?
  • What is the difference between bitwise and conditional operators?