Java Reference
In-Depth Information
Statements and Expressions
All tasks that you want to accomplish in a Java program can be broken down into a
series of statements. In a programming language, a statement is a simple command that
causes something to happen.
Statements represent a single action taken in a Java program. All the following are sim-
ple Java statements:
int weight = 225;
System.out.println(“Free the bound periodicals!”);
song.duration = 230;
Some statements can convey a value, such as when you add two numbers together in a
program or evaluate whether two variables are equal to each other. This kind of state-
ment is called an expression.
An expression is a statement that produces a value. The value can be stored for later use
in the program, used immediately in another statement, or disregarded. The value pro-
duced by a statement is called its return value .
Some expressions produce a numerical return value, as when two numbers are added
together or multiplied. Others produce a Boolean value— true or false —or even can
produce a Java object. They are discussed later today.
Although many Java programs contain one statement per line, this is a formatting deci-
sion that does not determine where one statement ends and another one begins. Each
statement in Java is terminated with a semicolon character ( ; ). A programmer can put
more than one statement on a line, and it will compile successfully, as in the following
example:
dante.speed = 2; dante.temperature = 510;
Statements in Java are grouped using the opening brace ( { ) and closing brace ( } ). A
group of statements organized between these characters is called a block or block state-
ment , and you learn more about them during Day 4, “Lists, Logic, and Loops.”
Variables and Data Types
In the VolcanoRobot application you created during Day 1, “Getting Started with Java,”
you used variables to keep track of information. A variable is a place where information
can be stored while a program is running. The value can be changed at any point in the
program—hence the name.
 
 
Search WWH ::




Custom Search