Notes on Elementary Java
Notes on Elementary Java
Summary: The following notes highlight the in-class discussion of
a Course class, as defined in
~walker/public_html/courses/153.sp01/Course.java.
These notes are not designed to be comprehensive. Rather, they
provide a brief commentary on the program -- starting at the first lines of
code and proceeding through to the end.
Comments
Java programs identify comments in two ways:
-
Whenever two slash characters // appear on a line, the rest of
the line is considered as a comment. Thus, // in Java is
analogous to a semicolon ; on a line of Scheme.
-
Comments may be enclosed in /* and */. That is, the
characters /* are considered to be the start of a comment, and
that comment is completed with the characters */. Such a comment
may be part of a line or extend over multiple lines.
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.
-
Note the file name Course.java has the same name as the class
(with .java) appended at the end.
-
Unlike Scheme, capitalization matters in Java. Thus, a class
Course is not the same as a class course.
-
While not required, a common Java convention is that the names of classes
start with a capital letter (e.g., Course), while the names of data
fields and methods begin with a lower-case letter.
Fields
Constructors
Whenever objects are created from classes, all fields are initialized.
-
Methods for initializing data have the same name as the class.
-
Java also requires that we must specify the type of data to be stored in
every variable. Some common
-
Initialization may be done in several ways -- using different methods,
provided the constructor methods have different parameters.
In the example, Course has two constructor methods -- both called
Course. With the first, one supplies a subject
parameter, and other fields are set to 0 or the null string. The
second constructor requires the programmer to specify data for all four
fields.
-
If a constructor does not explicitly initialize a field, or if no
constructor is provided by the programmer, then Java will initialize all
fields according to various default values. For clarity and to avoid
oversights, programmers should get into the habit of supplying constructors
and explicitly initializing all fields.
Extractor and Modifier Methods
Extractor methods return data from various fields, while modifier methods
change the values stored in those fields.
-
Just as in Scheme, an object contains some methods which are available for
applications, and an object also may include helper methods which are for
local or internal use only. For example, in husk-and-kernel processing,
the husk might be generally available, while the kernel would not be.
-
A method is available for applications if it is declared as
public.
-
A method may be used internally by an class, but not by other classes or
objects, if it is declared as private.
-
The definition of every method indicates either what type of data are
returned by the method or that no data are returned. In the example,
-
getSubject and getTitle return string data, while
getNumber and getCredits return integer data.
-
Modifiers setNumTitle and setCredits return no data,
and this is indicated with the word void in the declarations.
-
Methods may or may not have parameters in Java, just as in Scheme. When no
parameters are given, method names are followed by parentheses (
), indicating a procedure or method with no parameters. When
parameters are to be supplied, the method definition indicates the names
and types of those parameters. Unlike Scheme, Java methods require the
programmer to specify what type of data will be supplied as parameters.
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.
-
In either language, strings may be specified directly by placing characters
within double quotes.
-
Strings may be concatenated in Java using +, just as those in
Scheme may be combined with string-append.
-
The newline character in Java is designated as "\n", paralleling
the character #\newline 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.
-
The definition of a main method always starts:
public static void main (String[] args) {
Here, public and void mean just what has been described
earlier:
-
main is public -- that is it may be called by other
applications, and
-
main will not return any values (the return is void).
Also, Java programs may take command line arguments, and such values are
handled by specifying an array of strings as parameters (String[]
args). While this capability may be useful in running programs, we
will not utilize main's parameters for now.
Technically, Java requires that a main method belongs to the
class; there are not separate main methods for each object.
This gives rise to the descriptor static in the declaration of
main. While this will become clear over time, for now, it is
strongly suggested that you consider the header
public static void main (String[] args) {
as an idiom to be used when defining a main method.
-
The main begins by declaring and initializing objects for later
use.
-
Once objects are declared and initialized, their methods may be utilized by
specifying the object and the desired method -- separated by a dot. Thus,
out.println specifies the println method of
out, while yourCourse.getCredits references the
getCredits method of object yourCourse
-
Within SimpleOutput, the println displays the specified
text, and adds a new line character -- ready to continue on the next line.
In contrast, print displays the text, but does not add the new
line. Thus, the sequence
out.print(yourCourse.getSubject() + " ");
out.println (yourCourse.getNumber() + ": " + yourCourse.getTitle());
displays a subject and some spaces, and continues with the course number
and title on the same line. If the println had been used in both
cases, then these data would be printed on two separate lines.
This document is available on the World Wide Web as
http://www.math.grin.edu/~walker/courses/153.sp01/lab-intro-java-2.html
created April 8, 2001 by Henry M. Walker
last revised April 8, 2001