Sun Java Solaris Communities My SDN Account Join SDN
 
Java Technology Fundamentals Newsletter Index

New-to-Java Programming Center Supplement

 

New-to-Java Programming Center
Programming Center Supplement

Welcome to the Java Developer ConnectionSM New-to-Java Programming Center Supplement!

This monthly supplement provides a way for you to learn the basics of the Java programming language, discover new resources, and keep up-to-date on the latest additions to the JDC's New-to-Java Programming Center.

CONTENTS

1. Java Programming Language Basics

    Creating an array.

2. Program Challenge

    ScoresAverage application.

3. Making Sense of the Java Class Libraries

    System class.

4. Java Bits

    What is the Java platform?

5. New to Java Technology Forum Latest

    pepsivscoke wants to know: How to Color Components

6. For More Information

    Links to articles, Tech Tips, trails, and tutorials that provide more information on the topics discussed here.

7. Program Challenge Solution and Explanation

    Possible solution to the Scores application.

8. Downloading the Java 2 Platform
To follow examples and write the Program Challange, you need to download the Java 2 Platform.

Java Programming Language Basics

Creating an array:

An array is an object or data structure that holds multiple elements of the same type. Arrays are frequently used in applications that need a grouping of data assigned to one variable. A calendar application, for instance, might have a variable called "days" holding the String names of the seven days of the week. A card game might use an array to hold card denominations.

Once an array is created its length is established and fixed.

The following examples define three arrays:

float prices[]; 
\\ This is a null array--it exists but without a value 
\\ and the length is not fixed yet.

char vowels[] = new char [5]; 
\\ This array creates space for 5 char values, which 
\\ can be filled in at a later time. 
\\ The length is now fixed.

String days[] = {"Sunday", "Monday", "Tuesday", 
   "Wednesday",  "Thursday", "Friday", "Saturday"};
    
\\ This array is populated with seven String objects, 
\\ and because they are strings, they must also be 
\\ enclosed in quotes. Enclose char in single 
\\ quotes. Separate int types with commas.

Access array-specific elements by position, or using a for loop:

  1. By position:
    System.out.println(days[3]);
    Prints Wednesday
  2. Using a for loop:
      for (i = 0; i < days.length; i++)
         {
            System.out.println(days[i]);
         }
     


    Prints all the elements of the array days.

You can also copy data from one array to another, and create subarrays or multidimensional arrays.

Program Challenge

Create a application called ScoresAverage, following these steps:

  1. Create an array object called scores and populate it with the following numbers: 76.0, 84.5, 92.5, 88.0, 96.0.
  2. Use a for loop to determine the average of these scores.
  3. Print the average to the screen.
  4. Print the fourth element of the array to the screen.
  5. Use the length attribute to print the length of the array to the screen.

Your application should produce something similar to these results:

  The average of the scores is 87.4
  96.0 is the fourth element in the scores array.
  There are 5 elements in this array.

The solution and links to more information are at the bottom of this supplement.

Making Sense of the Java Class Libraries

The System class explained:

In the Program Challenge you use System.out.println to print an array element to the screen. System class is a part of the java.lang package. Unlike many other classes, you cannot instantiate a new instance of the System class in your application because the System class constructors are private.

But System class methods and fields, or class members, are static, so you can access them by reference with the dot notation. For instance, the object out is a standard output stream that is ready and open to accept output data. Using out with the the class name System and the method println prints text to the screen:

  System.out.println("Prints this text to the screen.")

Other useful methods of the System class:

arraycopy(): Copies specific elements of an array to certain locations in another array:

  public class CopyArrayElements
  {
      public static void main (String [] Args)
      {
        String [] zoo = { "Baboons", "Lions", 
                  "Tigers", "Zebras", "Crocodiles" };
        String [] zoo2 = { "Snakes", "Birds", 
        "Rhinoceroses","Elephants", "x", "x", "x,",
                                           "x","x" };

        System.arraycopy (zoo, 0, zoo2, 4, 5);

        for (int i = 0; i < zoo2.length; i ++)
        {
        System.out.println(zoo2[i]);

        }

      }
  }

exit(): Terminates the program that is running the Java virtual machine (JVM)1.

getProperties(): Determines the current system properties.

Java Bits

What is the Java Platform?

The Java 2 Platform is an environment for running and developing programs written in the Java programming language.

The Java platform has two components:

  • The Java virtual machine1 (JVM)
  • The Java Application Programming Interface (Java API)

The Java platform consists of the Java API, a large collection of ready-made software components, and a Java runtime environment. Most browsers include a JVM, so you are running the Java platform when you use these browsers, but you also create a Java platform when you download and install the latest version of the Java 2, Standard Edition (J2SE). This download also includes necessary development tools such as a compiler, a Java application launcher, appletviewer, and more.

New to Java Technology Forum Latest

pepsivscoke wants to know:

How do I color components?

Do you know a possible solution?

See: the Forum to respond to this question.

For More Information

What is Java?

The Java Tutorial Trail: Learning the Java Language, Arrays

Tech Tips: Manipulating Arrays

Java 2 Platform SE version 1.3 specification: Class System

Java Tutorial: Using the System Class

Java Tutorial: Miscellaneous System Methods

Java 2 SDK, Standard Edition, version 1.3 specifications: Summary of New Features and Enhancements

Program Challenge Solution

This is one possible solution to the Program Challenge:

public class ScoresAverage {

    public static void main(String[] args)
    {
        double scores[] = { 76.0, 84.5, 92.5, 
                                88.0, 96.0 };
        double sum= 0;
        double average;

        for (int i = 0; i < scores.length; 
                                        i ++)
        {
            sum += scores[i];
         }

         average = sum / scores.length;

         System.out.println("The average of 
                     the scores is " + average);
         System.out.println(scores[4] + 
              " is the fourth element in 
                            the scores array.");
         System.out.println("There are " 
                              + scores.length + 
                    " elements in this array.");
         }
   }

Were you surprised that the fourth element printed 96.0? Remember, elements of an array are indexed beginning with the number 0.

Arrays are useful for holding multiple elements of a single data or object type, but they have limits. You can not add more elements dynamically. Instead, you can create a different kind of storage container, similar to an array, called a Vector.

Read about Vectors in next month's issue of the JDC New-to-Java Programming Center Supplement.

Downloading the Java 2 Platform

For most Java development, you need the class libraries, compiler, tools, and run-time environment provided with the Java 2, Standard Edition development kit.

Have a question about programming? Use Java Online Support.

- Note -

Sun respects your online time and privacy. The Java Developer Connection mailing lists are used for internal Sun Microsystems purposes only. You have received this email because you elected to subscribe.

- Unsubscribe -

To unsubscribe to this newsletter, go to the subscriptions page uncheck the "JDC New-to-Java Supplement" checkbox, and click "Update".

- Subscribe -

To subscribe to other JDC mailings, go to the subscriptions page choose the newsletters you want to subscribe, and click "Update".

- Feedback -

Comments? Send your feedback on the JDC Newsletter to: jdc-webmaster

- Copyright -

Copyright 2001 Sun Microsystems, Inc. All rights reserved.
901 San Antonio Road, Palo Alto, California 94303 USA

This document is protected by copyright.

JDC New-to-Java Programming Center Supplement
July 2001

1 As used on this web site, the terms "Java virtual machine" or "JVM" mean a virtual machine for the Java platform.