Featured Posts
Recent Articles

Expressions, Statements, and Blocks in Java

Now  it's time to learn about expressions, statements, and blocks in Java. Operators may be used in building expressions, which compute values; expressions are the core components of statements; statements may be grouped into blocks.

Expressions
An expression 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. 


Examples of expressions, illustrated in bold below:
int cadence = 0;
anArray[0] = 100;
System.out.println("Element 1 at index 0: " + anArray[0]);

int result = 1 + 2; // result is now 3
if(value1 == value2) System.out.println("value1 == value2");

aValue = 8933.234; // assignment statement
aValue++; // increment statement
System.out.println("Hello World!"); // method invocation statement
Bicycle myBike = new Bicycle(); // object creation statement

Blocks
A block 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
}
}

Share and Enjoy:

0 comments for this post

Leave a reply

We will keep You Updated...
Sign up to receive breaking news
as well as receive other site updates!
Subscribe via RSS Feed subscribe to feeds
Recent Stories
Blog Archives
Recent Comments
Tag Cloud