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.
Java has five arithmetic operators:
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.
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
Java has several relational operators for testing equality and magnitude. The expressions containing relational operators return boolean value i.e. true or false.
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.
Bitwise operators are used to perform operations on individual bits in integers. For example,
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)
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)
public class a
public static void main(String args)
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);