Learn Java


Primitive data type expressions

An expression is something that appears on the right-hand side of an assignment operator and can be evaluated to produce a single value.

i.e. num = 25;

total = 12 + 43;

In the above example 12 and 43 are the operands and + is the operator. + is a binary operator because it has two operands

When evaluated the returned value is 55 and is an integer of type int because both the operators were of type int.

Arithmetic can also be performed on operands of type float and double.

i.e. 1.3 + 2.8 evaluates to a value of type double

4 + 3.5 will evaluate to a value of type double because the value 4 will get converted by the Java interpreter to a double before the addition is done

Here are some more examples

8.5 - 2 evaluates to the double value 6.5

3 * 1.3 evaluates to the double value 3.9

9 / 2 evaluates to the int value 4 and not 4.5 because both operands are int values

8.0 / 2 evaluates to the double value 4.0

Java also provides the modulus operator % which gives the remainder when one integer is divided by another

For example 9 % 2 evaluates to the int value 1 (The remainder of 9 divided by 2)

And 4 % 15 evaluates to the int value of 4 because 4 / 15 equals integer 0 remainder 4.

Java also has a minus sign to make a value negative. This is called a unary operator because it only has one operand i.e. -9

There is another group of operators that work with numerical operands but return Boolean values - true or false

These are shown below

==equal to
!=not equal to
<less than
<=less than or equal to
>greater than
>=greater than or equal to

The equality operators (== and !=) can be used with operands of any type.

The relational operators (<, <=, > and >=) can also be used with operands of type char, by comparing Unicode codes

There are also three operators that can be used with Booleans only. These are && (logical and), || (logical or), and ! (not).

The operands in expressions do not need to be literals but can be variables or expressions the evaluate to a value.

i.e. int num = 5;

int total = num + 12;

Use parentheses to determine the order that an expression is calculated

i.e 2 * 3 - 1 evaluates to 5 and is different to 2 * (3 - 1) which evaluates to 4

There is an order of precedence for operators as shown in the table below (Highest precedence at the top)

* /multiplicative
+ -additive
< > <= >=relational
== !=equality
&&logical and
||logical or

Here is an interesting evaluation

double dub;

dub = 9 / 2;

The above expression evaluates to 4 because of integer division then is converted to 4.0 because of the assignment to the double variable dub

In addition there are two postfix operators ++ and -- that can be used with numeric variables. ++ increments a value by 1 and -- decrements a value by 1. i.e num++; and num--;

However num++ and num-- return the value of num before they are incremented or decremented by 1 i.e.

int num = 4;

int result = num++;

In the above code result is assigned the value of 4 and num is incremented to the value 5


Previous Page Page 3 Next page


Page 1 2 3 4 5 6 7