Course Content‎ > ‎Session 2‎ > ‎

Expressions and Operators

An expression is the simplest form of statement in Java that actually accomplishes something. All expressions when evaluated return a value. The values returned by the expression can be assigned to a variable. Most of the expressions in Java use operators. 

Arithmetic Operators

Java has five arithmetic operators:  
  1. + (Addition operator), for example, 3 + 2 
  2. - (Subtraction), for example, 12 – 4 
  3. * (Multiplication), for example, 8 * 9 
  4. / (Division), for example, 50 / 10 
  5. % (Modulus), for example, 20 % 6
All these operators are binary operators i.e. each operator takes two operands, one on the either side of the operator. The subtraction operator (-) can also be used to negate a single operand. Modulus operator gives the remainder of division.  For example, 20 % 3 results in 2 because 2 is the remainder left when 20 is divided by 3. Result of an arithmetic operation involving integers is an int, regardless of the original type of operands (short and byte are automatically converted to int). Result of an arithmetic operation in which one operand is long and the other is an integer (int, byte or short) or both the operands are long, the result is long. Result of an arithmetic operation, in which one operand is float and other is an integer or both the operands are float, will be a float

Assignment Operator

Variable assignment is also a form of expression. Example of assignment expression:

    x = 10;

    Name = "Michael";

The right side of the assignment expression is always evaluated, before the assignment takes place.

    x = x + 2;

In this expression, 2 is added to the value of  x and then the new value is reassigned to x.

Shorthand assignment operators: The assignment expression

    x = x + 2   

can be written in another way i.e.

    x += 2

The assignment operator used in this statement for assigning a value is called as short hand assignment operator. Here are some of the short hand assignment operators:

    x = x + 4 can be rewritten as x += 4

    x = x – 4 can be rewritten as x -= 4

    x = x * 4 can be rewritten as x *= 4

    x = x / 4 can be rewritten as x /= 4  

Incrementing and Decrementing operators

++ is called as an Incrementing operator. It is used to increment a variable's value by 1.

-- is called as Decrementing operator. It is used to decrement a variable's value by 1.

i.e. x++ is the same as x = x + 1 and x-- is same as x = x - 1 (x can be an integer or a floating point number).

Increment and Decrement operators can be prefixed and postfixed i.e. ++ or -- operator can appear before or after the value it increments or decrements.  For example, ++x has prefixed increment operator; --x has prefixed decrement operator; x++ has postfixed increment operator and x—has postfixed decrement operator. Increment or decrement expressions like x++ or --x or ++x or x++ does not make a difference. But in assignment expression, in which, you are assigning the result of increment or decrement expression, (whether the Increment/Decrement operator is prefixed or postfixed) makes a difference.  Consider the example given below:

    x = 12

    y = x++; /* Value assigned to y is 12 */

    y = ++x; /* Value assigned to y is 13 */

The two expressions yield different results because when you use postfix operators (x++ or x--), y is assigned the value of x and then value of x is changed. When you use prefix operators (++x or --x), first the value of x is changed and then the new value of x is assigned to y

Relational Operators

Java has several relational operators for testing equality and magnitude. The expressions containing relational operators return boolean value i.e. true or false. 

  1. == (Equal operator), for example, x == 3
  2. != (Not equal), for example, x != 3
  3. > (Greater Than), for example, x > 3
  4. < (Less Than), for example, x < 3
  5. >= (Greater Than or Equal to), for example, x >= 3
  6. <= (Less Than or Equal to), for example, x <= 3 

Logical Operators

Expressions that result in boolean values like x > 3 can be combined by using logical operators that represent logical combinations AND, OR, XOR and logical NOT. 

  1. &, && (logical AND operator): The entire expression will be true only if both the expressions on either side of the operator are true; if either expression is false, the entire expression is false. The difference between & and  && is in the evaluation of the expression. Using &&, if the left side of the expression is false the entire expression is assumed to be false. Whereas in using &, both sides of the expression are evaluated regardless of the outcome. For example, a < b && b > c, a < b & b > c if b is greater than a and c only then this expression is true.
  2. |, || (logical OR operator): For example, A < B || A < C. If A is less than B or A is less than C or A is less than both B and C, only then this logical expression is true. | evaluates both sides of the expression regardless of the outcome. || is a short cut because if left expression is true, the expression returns true and right expression is never evaluated in this case. But if left expression is false, then the right expression is evaluated.
  3. ^ (logical XOR operator): The expression containing XOR operator returns true, only if its operands are different i.e. one true and one false or vice versa.  If both the operands are same i.e. both are true or both are false, it returns false.
  4. ! (logical NOT operator): ! operator is used with single expression.  The value of NOT expression is negation of expression.  For example, if a > c is true then, !a > c is false.

Bitwise Operators

Bitwise operators are used to perform operations on individual bits in integers. For example,

  1. & (Bitwise AND)
  2. | (Bitwise OR)
  3. ^ (Bitwise XOR)
  4. <<= (Left shift assignment i.e. x = x << y)
  5. >>= (Right shift assignment i.e. x = x >> y)
  6. >>>=  (zero fill right shift assignment i.e. x = x >>> y)

Operator Precedence

Operator precedence determines the order in which expressions are evaluated. Given below is a list of operators in order of their level of precedence: 

1. [] ( )
( ) are used to group expressions i.e. dot is used to access methods and variables of classes. [ ] i.e. square brackets are used for arrays.

2.  ++ -- !  instanceof
instanceof operator (discussed later)

3.  new
new operator is used for creating new instances of a class (discussed later)

4. / %  (multiplication, division, modulus)

5. + - (Addition, subtraction)

6. << >> >>> (Bitwise left and right shift)

7. < > <= >= (less than, greater than, less than or equal to, greater than or equal to)

8. ==  != (equal to, not equal to)

9. & (AND)

10. ^ (XOR)

11. | (OR)

12. && (Logical AND)

13. || (Logical OR)

14. ? : (Conditional expression discussed later)

15. =  +=  -=  *=  /=  %=  ^= (Various assignment operators)

For example,

    public class a
        public static void main(String args[])
            int i;
            i = 10 + 2 * 2 – 10 / 2;
            System.out.println(i); /* 9 will be displayed on the screen */
            i = (10 + 2)*(2 - 10) / 2;
            System.out.println(i); /* -48 will be displayed on the screen */
            i =(10 + (2 * ((2 - 10) / 2)));
            System.out.println(i); /* 2 will be displayed on the screen */

The println function displays string or value of string variable on the screen. toString is a function of class Integer. toString function converts the integer value to a string.

    i = 10 + 2 * 2 – 10 / 2;

In this statement, the expression on the right is evaluated and then, its value is assigned to the integer variable i. Operators of same precedence level are evaluated from left to right.  For example, * and / operator are evaluated before + and - operator. So,

    i = 10 + 2 * 2 –10 / 2;

is evaluated in two steps. In the first step, 2 * 2 (results in 4) and 10 / 2 (results in 5) is done. In the second step, 10 + 4 - 5 is evaluated. But, you can change the order in which expressions are evaluated by using parentheses around the expressions that you want to evaluate first. You can also nest parentheses. In this case, innermost parenthetic expression is evaluated first.

Operator Used with Strings

+ operator is used in Java to create and concatenate strings. For example,

    String name = "John";  
    /* My Name is John will be displayed on the screen */
    System.out.println ("My Name is " + name);
    /* My Name is Mr. John will be displayed on the screen */
    name = "Mr." + name;
    System.out.println("My Name is " + name);