Java
  Home arrow Java arrow Page 6 - 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 - A Second Short Program


    (Page 6 of 9 )

    Perhaps no other concept is more fundamental to a programming language than that of a variable. As you probably know, a variable is a named memory location that may be assigned a value by your program. The value of a variable may be changed during the execution of the program. The next program shows how a variable is declared and how it is assigned a value. In addition, the program also illustrates some new aspects of console output. As the comments at the top of the program state, you should call this file Example2.java.

      /*
        Here is another short example.
        Call this file "Example2.java".
      */
      class Example2 {
        public static void main(String args[]) {
          int num; // this declares a variable called num
         
    num = 100; // this assigns num the value 100
         
    System.out.println("This is num: " + num);
          
    num = num * 2;
         
    System.out.print("The value of num * 2 is "); 
          System.out.println(num);
        }
      }

    When you run this program, you will see the following output:

    This is num: 100
    The value of num * 2 is 200

    Let’s take a close look at why this output is generated. The first new line in the program is shown here:

    int num; // this declares a variable called num

    This line declares an integer variable called num. Java (like most other languages) requires that variables be declared before they are used.

    Following is the general form of a variable declaration:

    type var-name;

    Here, type specifies the type of variable being declared, and var-name is the name of the variable. If you want to declare more than one variable of the specified type, you may use a comma-separated list of variable names. Java defines several data types, including integer, character, and floating-point. The keyword int specifies an integer type.

    In the program, the line

    num = 100; // this assigns num the value 100

    assigns to num the value 100. In Java, the assignment operator is a single equal sign.

    The next line of code outputs the value of num preceded by the string “This is num:”.

    System.out.println("This is num: " + num);

    In this statement, the plus sign causes the value of num to be appended to the string that precedes it, and then the resulting string is output. (Actually, num is first converted from an integer into its string equivalent and then concatenated with the string that precedes it. This process is described in detail later in this book.) This approach can be generalized. Using the + operator, you can string together as many items as you want within a single println( ) statement.

    The next line of code assigns num the value of num times 2. Like most other languages, Java uses the * operator to indicate multiplication. After this line executes, num will contain the value 200.

    Here are the next two lines in the program:

    System.out.print("The value of num * 2 is "); System.out.println(num);

    Several new things are occurring here. First, the built-in method print( ) is used to display the string “The value of num * 2 is ”. This string is not followed by a newline. This means that when the next output is generated, it will start on the same line. The print( ) method is just like println( ), except that it does not output a newline character after each call. Now look at the call to println( ). Notice that num is used by itself. Both print( ) and println( ) can be used to output values of any of Java’s built-in types.

    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 2 Hosted by Hostway
    Stay green...Green IT