Home arrow Java arrow Page 8 - An Overview of Java
JAVA

An Overview of Java


When you start learning Java, it can sometimes seem as if learning one aspect necessitates knowledge of several others. This article addresses that common frustration by providing a short overview of several key features of Java. It is excerpted from chapter two of Java2: The Complete Reference, 5th edition, written by Herbert Schildt (McGraw-Hill, 2004; ISBN: 0072224207).

Author Info:
By: McGraw-Hill/Osborne
Rating: 4 stars4 stars4 stars4 stars4 stars / 31
July 21, 2005
TABLE OF CONTENTS:
  1. · An Overview of Java
  2. · The Three OOP Principles
  3. · Polymorphism, Encapsulation, and Inheritance
    Work Together
  4. · A First Simple Program
  5. · A Closer Look at the First Sample Program
  6. · A Second Short Program
  7. · Two Control Statements
  8. · Using Blocks of Code
  9. · Lexical Issues

print this article
SEARCH DEVARTICLES

An Overview of Java - Using Blocks of Code
(Page 8 of 9 )

Java allows two or more statements to be grouped into blocks of code, also called code blocks.This is done by enclosing the statements between opening and closing curly braces. Once a block of code has been created, it becomes a logical unit that can be used any place that a single statement can. For example, a block can be a target for Javaís if and for statements. Consider this if statement:

if(x < y) { // begin a block
  x = y;
  y = 0;
} // end of block

Here, if x is less than y, then both statements inside the block will be executed. Thus, the two statements inside the block form a logical unit, and one statement cannot execute without the other also executing. The key point here is that whenever you need to logically link two or more statements, you do so by creating a block.

Letís look at another example. The following program uses a block of code as the target of a for loop.

/*
  Demonstrate a block of code.
 
Call this file "BlockTest.java"
*/
class BlockTest {
  public static void main(String args[]) {
    int x, y;
    
y = 20;
    
// the target of this loop is a block
   
for(x = 0; x<10; x++) {
      System.out.println("This is x: " + x);  
      System.out.println("This is y: " + y);
      y = y - 2;
    
}
  }
}

The output generated by this program is shown here:

This is x: 0
This is y: 20
This is x: 1
This is y: 18
This is x: 2
This is y: 16
This is x: 3
This is y: 14
This is x: 4
This is y: 12
This is x: 5
This is y: 10
This is x: 6
This is y: 8
This is x: 7
This is y: 6
This is x: 8
This is y: 4
This is x: 9
This is y: 2

In this case, the target of the for loop is a block of code and not just a single statement. Thus, each time the loop iterates, the three statements inside the block will be executed. This fact is, of course, evidenced by the output generated by the program.

As you will see later in this book, blocks of code have additional properties and uses. However, the main reason for their existence is to create logically inseparable units of code.


blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials