Java Expressions, Statements and Blocks

In java, Statements are composed of one or many expressions. They may also be grouped into blocks.

Expressions

Expressions in java are made of variables, operators and method invocations. An expression always evaluates to a single value.

//Examples of valid expressions
a =0;
"Hello "+firstName + " "+ lastName
d = a + b;

 

Statements

A statement in java is a complete unit of execution. Assignment expressions, pre-increment operators, pre-decrement operators, post-increment operators, post-decrement operators, method invocation and object instantiation expressions can compose a statement by terminating the expression with a semicolon (;) .

// using the Examples to create valid statements
int a =0;
System.out.println("Hello "+firstName + " "+ lastName);
double d = a + b;

 

Blocks

A block in Java is composed of zero or more statements enclosed inside curly braces { }. Blockes are allowed wherever statements are allowed. Blocks can be nested.

// Example of two blocks
if(condition)
{ // block 1 beginning
  int x=0;
  System.out.println("Block 1 is executed");
} // block 1 ending
else
{ // block 2 beginning
  System.out.println("Block 2 is executed");
} // block 2 ending

 

 

You like this content ? Share it with you entourage !

Leave a Reply

Your email address will not be published.