Java Expressions Introduced

A group of programmers working at a computer

Yuri_Arcurs/Getty Images

Expressions are essential building blocks of any Java program, usually created to produce a new value, although sometimes an expression 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);

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 first to 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 assigns 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 the product; a and b are not changed. This is called a side effect.

Format
mla apa chicago
Your Citation
Leahy, Paul. "Java Expressions Introduced." ThoughtCo, Apr. 5, 2023, thoughtco.com/expression-2034097. Leahy, Paul. (2023, April 5). Java Expressions Introduced. Retrieved from https://www.thoughtco.com/expression-2034097 Leahy, Paul. "Java Expressions Introduced." ThoughtCo. https://www.thoughtco.com/expression-2034097 (accessed March 28, 2024).