Java arrays, strings, and packaged code

 

Using pre-written code

Package

Contents

java.applet

Supports Java applets (small programs that may be embedded inside other applications - such as Web pages)

java.awt

The "abstract windows toolkit" supports graphical, Windows programming

java.lang

Supports Java language extensions and mathematical tools

java.text

Supports text formatting and manipulation

java.util

A set of general purpose utility classes

import java.util.*;

Notes:

  1. Code import statements at the beginning of the Java source file for all packages except java.lang. Its contents are automatically available.
  1. Don't forget the semicolon. This is a common mistake of programmers moving from C++ to Java.
  1. Optionally, only a selected class might be specified, such as 

import java.util.stack;

which has features to help manage a first-in-first-out stack.

  1. Without an import statement pre-written code is still accessible, but each class reference would need to be prefaced by the package name. For example,

java.util.stack x = new java.util.stack();

 

Arrays

double[] dailySales = new double[366];

Notes:

  1. Java allows the square brackets to be coded after the array's data type or after its identifier (as in C++). While you could re-code the above as

      double dailySales[] = new double[366];

it will identify you as a C++ programmer who is new to Java.

  1. The number inside the second set of square brackets specifies how many elements the array will have. Rather than a literal, the identifier of an integer variable or constant, or an expression evaluating to an integer could be coded.
  1. Two separate statements could have been used to declare the array and obtain space for its elements, such as
double[] dailySales;
dailySales = new double[366];

The first statement declares the array reference. The second statement allocates memory for all the array elements.

  1. Unless otherwise specified, array elements are automatically initialized based upon their type. Numeric elements are set to 0, boolean elements are set to false, and character elements are set to binary zeroes.

  2. Array elements can be initialized to specific values by coding a value list, such as

  char[] transCode = {'A', 'U', 'D'};

When a value list is coded, the number of array elements is determined by the number of values in the list and the new operator is not coded.

public static double sumAll(double[] theArray) {
  double total = 0;
  for (int i = 0; i < theArray.length; i++) {
    total += theArray[i];
  }
  return total;
}

 

Strings

String jobTitle = "Clerk";

Notes:

  1. Alternatively, the object could have been constructed as follows:

String jobTitle = new String("Clerk");

  1. jobTitle is the object reference. It holds the address of where the String object with the value of "Clerk" resides in memory.

jobTitle

memory address

->

"Clerk"

  1. The reference can be reassigned and made to point at a different String class object. For example,

jobTitle = "Systems Analyst";

reassigns jobTitle to point at an object with the value "Systems Analyst". The object with the value "Clerk" remains in memory but is inaccessible.

jobTitle

memory address

->

"Systems Analyst"

 

"Clerk"

Inaccessible objects don't remain in memory forever. The Java "garbage collection" facility automatically disposes of objects that can no longer be referenced.

String x = "abc";
String y = new String("abc");
if (x == y) {
  System.out.println("The strings are equal");
}
else {
  System.out.println("The strings are different");
}

Testing this code will result in the message that says the strings are different. This is caused by the use of the == operator. The code has tested contents of the two object references and NOT the values of the two objects.

To correctly compare two strings, replace the if statement with the following:

if (x.equals(y))

The equals() method tests the contents of two objects and NOT where they are located in memory.