Java
  Home arrow Java arrow Page 8 - An Overview of Java
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

An Overview of Java
By: McGraw-Hill/Osborne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 22
    2005-07-21

    Table of Contents:
  • An Overview of Java
  • The Three OOP Principles
  • Polymorphism, Encapsulation, and Inheritance
    Work Together
  • A First Simple Program
  • A Closer Look at the First Sample Program
  • A Second Short Program
  • Two Control Statements
  • Using Blocks of Code
  • Lexical Issues

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More Java Articles
    More By McGraw-Hill/Osborne


       · this intro could have been for cobol or fortran with slight tweaks in the syntax. I...
     

    Buy this book now. This article is excerpted from chapter two of Java2: The Complete Reference, 5th edition, written by Herbert Schildt (McGraw-Hill, 2004; ISBN: 0072224207). Check it out at your favorite bookstore. Buy this book now.

    JAVA ARTICLES

    - 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 ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek