How to Add Buttons and Text to Your JavaFX Project
How to Use the Java Keyword this
How to Use Scriptlets in Java Servlets

How to Use Blocks in Java

A block in Java is a group of one or more statements enclosed in braces. A block begins with an opening brace ({) and ends with a closing brace (}). Between the opening and closing braces, you can code one or more statements. For example:

    int i, j;
    i = 100;
    j = 200;

A block is itself a type of statement. As a result, any time the Java language requires a statement, you can substitute a block to execute more than one statement.

You can code the braces that mark a block in two popular ways. One way is to place both braces on separate lines and then indent the statements that make up the block:

if ( i > 0)
    String s = “The value of i is “ + i;

The other style is to place the opening brace for the block on the same line as the statement that the block is associated with:

if ( i > 0) {
    String s = “The value of i is “ + i;

Even though a block can be treated as a single statement, you should not end a block with a semicolon. The statements within the block may require semicolons, but the block itself does not.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
How to Use the parse Method in Java to Create a Date-Time Object
How to Draw Transparently in JavaFX
Perform Simple Math Tasks with Java
Looking at the Architecture of Enterprise JavaBeans
Arrays Class in Java