1. Home
  2. Computing & Technology
  3. Java

Expression

By Paul Leahy, About.com

Definition:

Expressions are essential building blocks of any Java program. They are built using values, variables, operators and method calls.

In terms of the syntax of the Java language, an expression is akin to a clause in the English language. A clause portrays a specific meaning. With the right punctuation it can sometimes stand on its own or more often than not become part of a sentence. Similarly, an expression in Java evaluates to a single value. Some expressions can make statements by themselves (by adding a semicolon on the end) but more commonly make up part of a statement.

Examples:

The following program contains plenty of expressions (shown in bold italics) that each evaluate to a specific value:

int secondsInDay = 0;
int daysInWeek = 7;
int hoursInDay = 24;
int minutesInHour = 60;
int secondsInMinute = 60;
boolean calculateWeek = true;

secondsInDay = secondsInMinute * minutesInHour * hoursInDay;

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 seventh line 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).

Glossary:

# A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Explore Java
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Java
  4. Java Glossary
  5. E
  6. Expression - Definition for the Term: Expression>

©2009 About.com, a part of The New York Times Company.

All rights reserved.