Java Expressions Introduced

There are Three Types of Java Expressions

Business woman typing at laptop computer in the office
Kristian Sekulic/E+/Getty Images

Expressions are essential building blocks of any Java program, usually created to produce a new value, although sometimes an expression simply assigns a value to a variable. Expressions are built using values, variables, operators and method calls.

Difference Between Java Statements and Expressions

In terms of the syntax of the Java language, an expression is akin to a clause in the English language which portrays a specific meaning.

With the right punctuation, it can sometimes stand on its own, although it can also be a part of a sentence. Some expressions equate to statements by themselves (by adding a semicolon at the end) but more commonly, they comprise part of a statement.

For example, (a * 2) is an expression. b + (a * 2); is a statement.  You could say that the expression is a clause, and the statement is the complete sentence since it forms the complete unit of execution.

A statement doesn't have to include multiple expressions, however. You can turn a simple expression into a statement by adding a semi-colon: (a * 2); 

Types of Expressions

While an expression frequently produces a result, it doesn't always. There are three types of expressions in Java:

  • Those that produce a value, i.e. the result of (1 + 1)
  • Those that assign a variable, for example (v = 10)
  • Those that have no result but might have a "side effect" because an expression can include a wide range of elements such as method invocations or increment operators that modify the state (i.e. memory) of a program. 

    Examples of Expressions

    Here are some examples of various types of expressions.

    Expressions that Produce a Value

    Expressions that produce a value use a wide range of Java arithmetic, comparison or conditional operators. For example, arithmetic operators include  +, *, /, <, >, ++ and %. Some conditional operators  are ?, ||, and the comparison operators are <, <= and >.

     See the Java specification for a complete list.

    These expressions produce a value:

    3/2

    5% 3

    pi + (10 * 2) 

    Note the parentheses in the last expression. This directs Java to first compute the value of the expression within the parentheses (just like the arithmetic you learned in school), then complete the rest of the computation.

    Expressions that Assign a Variable

    This program here contains plenty of expressions (shown in bold italics) that each assign a value.

     int secondsInDay = 0;
     int daysInWeek = 7;
     int hoursInDay = 24;
     int minutesInHour = 60;
     int secondsInMinute = 60; 
     boolean calculateWeek = true;
    
     secondsInDay = secondsInMinute * minutesInHour * hoursInDay;  //7
     
     System.out.println("The number of seconds in a day is: " + secondsInDay);
     
     if (calculateWeek == true)
     {
       System.out.println("The number of seconds in a week is: " + secondsInDay * daysInWeek); 
     }
    

    The expressions in the first six lines of the code above, all use the assignment operator to assign the value on the right to the variable on the left.

    The line denoted with //7 is an expression that can stand on its own as a statement. It also shows that expressions can be built up through the use of more than one operator.

    The final value of the variable secondsInDay is the culmination of evaluating each expression in turn (i.e., secondsInMinute * minutesInHour = 3600, followed by 3600 * hoursInDay = 86400).

    Expressions with No Result

    While some expressions produce no result, they can have a side effect which occurs when an expression changes the value of any of its operands.

    For example, certain operators are considered to always produce a side effect, such as the assignment, increment and decrement operators. Consider this:

    int product = a * b;

    The only variable changed in this expression is product; a and b are not changed. This is called a side effect.