Movatterモバイル変換


[0]ホーム

URL:


Documentation

The Java™ Tutorials
Language Basics
Variables
Primitive Data Types
Arrays
Summary of Variables
Questions and Exercises
Operators
Assignment, Arithmetic, and Unary Operators
Equality, Relational, and Conditional Operators
Bitwise and Bit Shift Operators
Summary of Operators
Questions and Exercises
Expressions, Statements, and Blocks
Questions and Exercises
Control Flow Statements
The if-then and if-then-else Statements
The switch Statement
The while and do-while Statements
The for Statement
Branching Statements
Summary of Control Flow Statements
Questions and Exercises
Trail: Learning the Java Language
Lesson: Language Basics
Home Page >Learning the Java Language >Language Basics
« Previous • Trail • Next »

The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.
SeeDev.java for updated tutorials taking advantage of the latest releases.
SeeJava Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.
SeeJDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.

Expressions, Statements, and Blocks

Now that you understand variables and operators, it's time to learn aboutexpressions,statements, andblocks. Operators may be used in building expressions, which compute values; expressions are the core components of statements; statements may be grouped into blocks.

Expressions

Anexpression is a construct made up of variables, operators, and method invocations, which are constructed according to the syntax of the language, that evaluates to a single value. You've already seen examples of expressions, illustrated in bold below:

intcadence = 0;anArray[0] = 100;System.out.println("Element 1 at index 0: " + anArray[0]);intresult = 1 + 2; // result is now 3if (value1 == value2)     System.out.println("value1 == value2");

The data type of the value returned by an expression depends on the elements used in the expression. The expressioncadence = 0 returns anint because the assignment operator returns a value of the same data type as its left-hand operand; in this case,cadence is anint. As you can see from the other expressions, an expression can return other types of values as well, such asboolean orString.

The Java programming language allows you to construct compound expressions from various smaller expressions as long as the data type required by one part of the expression matches the data type of the other. Here's an example of a compound expression:

 1 * 2 * 3

In this particular example, the order in which the expression is evaluated is unimportant because the result of multiplication is independent of order; the outcome is always the same, no matter in which order you apply the multiplications. However, this is not true of all expressions. For example, the following expression gives different results, depending on whether you perform the addition or the division operation first:

x + y / 100    // ambiguous

You can specify exactly how an expression will be evaluated using balanced parenthesis: ( and ). For example, to make the previous expression unambiguous, you could write the following:

 (x + y) / 100  // unambiguous, recommended

If you don't explicitly indicate the order for the operations to be performed, the order is determined by the precedence assigned to the operators in use within the expression. Operators that have a higher precedence get evaluated first. For example, the division operator has a higher precedence than does the addition operator. Therefore, the following two statements are equivalent:

x + y / 100
x + (y / 100) // unambiguous, recommended

When writing compound expressions, be explicit and indicate with parentheses which operators should be evaluated first. This practice makes code easier to read and to maintain.

Statements

Statements are roughly equivalent to sentences in natural languages. Astatement forms a complete unit of execution. The following types of expressions can be made into a statement by terminating the expression with a semicolon (;).

Such statements are calledexpression statements. Here are some examples of expression statements.

// assignment statementaValue = 8933.234;// increment statementaValue++;// method invocation statementSystem.out.println("Hello World!");// object creation statementBicycle myBike = new Bicycle();

In addition to expression statements, there are two other kinds of statements:declaration statements andcontrol flow statements. Adeclaration statement declares a variable. You've seen many examples of declaration statements already:

// declaration statementdouble aValue = 8933.234;

Finally,control flow statements regulate the order in which statements get executed. You'll learn about control flow statements in the next section,Control Flow Statements

Blocks

Ablock is a group of zero or more statements between balanced braces and can be used anywhere a single statement is allowed. The following example,BlockDemo, illustrates the use of blocks:

class BlockDemo {     public static void main(String[] args) {          boolean condition = true;          if (condition) {// begin block 1               System.out.println("Condition is true.");          }// end block one          else {// begin block 2               System.out.println("Condition is false.");          }// end block 2     }}
« PreviousTrailNext »

About Oracle |Contact Us |Legal Notices |Terms of Use |Your Privacy Rights

Copyright © 1995, 2024 Oracle and/or its affiliates. All rights reserved.

Previous page: Questions and Exercises: Operators
Next page: Questions and Exercises: Expressions, Statements, and Blocks

[8]ページ先頭

©2009-2025 Movatter.jp