JAVA
Arrays and Strings

Arrays and Strings 

   Like other programming languages, Java allows you to collect and
   manage multiple values through an array object. You manage data
   comprised of multiple characters through a String object. 

   Arrays

      The countChars method doesn't use an array, but the Count
      class's main method declares an array as a parameter to main.
      This section shows you how to create and use arrays in your Java
      programs. 

      As for other variables, before you can use an array you must first
      declare it. Again, like other variables, a declaration of an array
      has two primary components: the array's type and the array's
      name. An array's type includes the data type of the elements
      contained within the array. For example, the data type for an
      array that contained all integer elements is array of integers. You
      cannot have a generic array--the data type of its elements must
      be identified when the array is declared. Here's a declaration for
      an array of integers: 

         int[] arrayOfInts;

      The int[] part of the declaration indicates that arrayOfInts
      is an array of integers. The declaration does not allocate any
      memory to contain the array elements. 

      If your program attempted to assign or access any values to any
      elements of arrayOfInts before memory for it has been
      allocated the compiler will print an error like this one and refuse
      to compile your program. 

         testing.java:64: Variable arrayOfInts may not have been initialized.

      To allocate memory for the elements of the array, you must
      instantiate the array. You do this using Java's new operator.
      (Actually, the steps you take to create an array are similar to the
      steps you take to create an object from a class: declaration,
      instantiation, and initialization. You can learn more about
      creating objects in the Creating Objects section at Sun's Web site. 

      The following statement allocates enough memory for
      arrayOfInts to contain ten integer elements. 

         int[] arrayOfInts = new int[10]

      In general, when creating an array, you use the new operator,
      plus the data type of the array elements, plus the number of
      elements desired enclosed within square brackets ('[' and ']'). 

         elementType[] arrayName = new elementType[arraySize]

      Now that some memory has been allocated for your array, you
      can assign values to its elements and retrieve those values: 

         for (int j = 0; j < arrayOfInts.length; j ++) {
             arrayOfInts[j] = j;
             System.out.println("[j] = " + arrayOfInts[j]);
         }

      This example shows that to reference an array element, you
      append square brackets to the array name. Between the square
      brackets indicate (either with a variable or some other
      expression) the index of the element you want to access. Note
      that in Java, array indices begin at 0 and end at the array length
      minus 1. 

      There's another interesting element (so to speak) in the small
      code sample above. The for loop iterates over each element of
      arrayOfInts, assigning values to its elements and printing out
      those values. Notice the use of arrayOfInts.length to
      retrieve the current length of the array. length is a property
      provided for all Java arrays. 

      Let's look again at the main method that calls countChars. In
      particular note the use of the args array: 

         import java.io.*;
         public class Count {
             // ... countChars method omitted ...
             public static void main(String[] args) throws Exception {
                 if (args.length >= 1)
                     countChars(new FileReader(args[0]));
                 else
                     system.err.println("Usage: Count filename"); 
             }
         }

      The Java runtime allocates the space for the args array, so main
      doesn't have do bother with it. The main method ensures that
      there's at least one element in the args array, and if there is, it
      uses the first element in the array (presumably the name of a file)
      to open a FileReader. 

      Arrays can contain any legal Java data type including reference
      types such as objects or other arrays. For example, the following
      declares an array that can contain ten String objects. 

         String[] arrayOfStrings = new String[10];

      The elements in this array are reference types, that is, each
      element contains a reference to a String object. At this point,
      enough memory has been allocated to contain the String
      references, but no memory has been allocated for the Strings
      themselves. If you attempted to access one of
      arrayOfStrings elements at this point, you would get a
      NullPointerException because the array is empty and
      contains no Strings and no String objects. This is often a
      source of some confusion for programmers new to the Java
      language. You have to allocate the actual String objects
      separately: 

         for (int i = 0; i < arrayOfStrings.length; i ++) {
             arrayOfStrings[i] = new String("Hello " + i);
         }

   Strings

      A sequence of character data is called a string and is
      implemented in the Java environment by the String class (a
      member of the java.lang package). Count's main method
      uses Strings in its the declaration of the args array: 

         String[] args

      This code explicitly declares an array named args that contains
      String objects. The empty brackets indicate that the length of
      the array is unknown at compilation time because the array is
      passed in at runtime. 

      The countChars method also uses two Strings both in the
      form of a literal string (a string of characters between double
      quotation marks): 

         "Counted "
             . . .
         " chars."

      The program implicitly allocates two String objects, one for
      each of the two literal strings shown previously. 

      String objects are immutable--that is, they cannot be
      changed once they've been created. The java.lang package
      provides a different class, StringBuffer, which you can use to
      create and manipulate character data on the fly. The String and StringBuffer 
      Classes page on sun's website covers thoroughly the use of both the 
      String and StringBuffer classes. 

      String Concatenation

         Java lets you concatenate strings together easily using
         the + operator. The countChars method uses this
         feature of the Java language to print its output. The
         following code snippet concatenates three strings
         together to produce its output: 

            "Counted " + count + " chars."

         Two of the strings concatenated together are literal
         strings: "Counted " and " chars." The third
         string--the one in the middle--is actually an integer
         that first gets converted to a string and then is
         concatenated to the others.

Now that we've done the absolute basics of the java language implementation, I will return to applets by talking about the mouse over applet.

This page was created by Bob Perini 
For CS 763(Web Seminar) at SUNY Buffalo