CSC 153: Computer Science Fundamentals Grinnell College Spring, 2005
 
Laboratory Exercise Reading
 

Notes on Elementary Java

Abstract

These notes highlight the in-class discussion of a Course class, as defined in ~walker/java/examples/course/Course.java. These notes are not designed to be comprehensive. Rather, they provide a brief commentary on the program.

Additional Notes

Mr. Rebelsky maintains additional notes on Java, designed for those just beginning with the language, at http://www.cs.grinnell.edu/~rebelsky/Espresso/. This source provides a range of introductory readings and lab exercises.

Commentary

The following notes provide a running commentary on the program Course class, as defined in ~walker/java/examples/course/Course.java. Since the notes are not designed to be comprehensive, you may want to consult more substantive sources for a more complete review of the Java programming language. In what follows, commentary starts with the first lines of code and proceeds through to the end.

Comments

Java programs identify comments in two ways:

Packages

Java organizes classes into modules, called packages. When writing small programs, such an organization may seem unnecessary. However, Java was designed for large-scale applications, in which there may be a very large number of classes. In that context, it is important to keep track of the names of various classes, and there is potential for the developers of one part of the program to use the same class names as have been specified by developers of another part. That is, developers of different parts of a program might use the same class names, and Java would need to determine which class was meant at each stage. Thus, the designers of Java decided to organize classes into packages, so the classes within one package could be distinguished from those in another.

The statement


   package course;

Within Unix and Linux systems, the convention is that a package name corresponds to a directory, and all classes within a package are stored within this designated directory.

Within the context of a package, names may be easy to identify. However, within a broader programming context, Java employs a naming convention for packages that reflects the directory structure. Thus, the Java "standard" suggests that you follow a similar style to a Web URL, although the ordering of the elements is reversed. For example, my programs for this course might be in a package that starts with


    edu.grinnell.cs.walker.java.examples

import Statements

Whenever one class wants to reference objects from another class, the program must tell the compiler of the existence of this separate class. This is accomplished with an import statement.

In class Course from file Course.java, the PrintWriter is located within the io package (subdirectory) of the standard java library. To identify this location, the import statement indicates this starting library point, the subdirectory, and the class name:


     import java.io.PrintWriter;

Class Definitions

Java supports object-oriented problem solving, and all programs in Java begin with classes and objects. Thus, each program component is a class, and a file begins with the declaration of one class. In the example, the class Course is identified as public, meaning that any application is allowed to utilize this class in its work.

Fields

Constructors

Whenever objects are created from classes, all fields are initialized.

Extractor and Modifier Methods

Extractor methods return data from various fields, while modifier methods change the values stored in those fields.

Brackets

In Java, the brackets { and } serve as begin and end markers, just as parentheses do in Scheme.

Strings and String Operations

Java contains strings and string operations that are analogous to those in Scheme.

Java's main Method

Once we defined classes in Scheme, we tested and used them in separate code, and Java also allows classes to be used by other classes and programs. In addition, Java allows any class to have a main method, which can be used to run a program based on the given class.


This document is available on the World Wide Web as

http://www.cs.grinnell.edu/~walker/courses/153.sp05/readings/reading-intro-java-2.shtml

created April 8, 2001
last revised March 24, 2005
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.