1. Technology
Send to a Friend via Email



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

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.


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


# 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

  1. About.com
  2. Technology
  3. Java
  4. Java Glossary
  5. E
  6. Expression (Java): Definition and Examples

©2014 About.com. All rights reserved.