Espresso: A Concentrated Introduction to Java
Summary: We introduce the basics of the Java programming language and walk through the first simple program.
Prerequisites: Basic understanding of object-oriented concepts.
Contents:
Now that you know the basics of object-oriented programming, there are particular characteristics you should look for in any object-oriented language, including ways to define classes and objects, some protection mechanisms, and support for inheritance and polymorphism. Different languages natually take different approaches to these issues, both in the syntax that describes how one writes programs and in the underlying semantics of how objects and classes relate.
There are also some things you may not have thought about, such as the
way in which one invokes a program
in the particular language.
In some languages one simply adds new classes and objects to an
existing world
, often through a graphical user interface. Other
languages require you to type commands in a textual user interface.
Still others can automatically
determine which objects to start
(e.g., because an object is embedded in a Web page).
Classes, object creation, inheritance, polymorphism, and program instantiation are more subjects than we can cover in a short reading. Hence, in this reading we will focus on three basic issues in Java:
Java provides a variety of mechanisms for indicating which objects provide
the main components of your program. The simplest, and the one we will
use for most of this book, is what we typically call a Main Class
.
Such a class is a class that contains one method, called main
.
The main
method contains a series of instructions to create
objects and to tell those objects to do things.
That theory is fairly simple. The syntax, however, is daunting to many novice programmers. Here is a sketch of a typical Main class.
public class NameOfClass { public static void main(String[] args) throws Exception { instructions; } // main(String[]) } // class NameOfClass
It is the programmer's responsibility to choose an appropriate name for the class and the instructions for the task at hand. Tradition suggests that the name of a class begin with an upper-case letter and use mixed case.
What else is in this sketch? The outer braces enclose the contents
of this class, in this case, just the main method. You will see braces
enclosing other things in future readings. The inner braces enclose the
instructions for the main
method.
You'll notice two slashes and a note after the end-braces. Text that begins with two slashes is a comment for the programmer or reader. My experience suggests that you should always comment your end braces to indicate what you're ending. Such comments help you make sure that your code is structured appropriately and helps the reader more quickly parse your code. You may (and should) also insert comments to explain key parts of your code.
You can probably guess what purpose the words that preface NameOfClass
serve. The public
indicates that the class can be used by
any program or user (or at least any program or user that has system
permission to read the file containing the class). The class
indicates that we're defining a class.
The slew of stuff surrounding main
is perhaps more confusing.
If you are willing to live with just write what you see above
, you
need not learn the details right now. Feel free to skip ahead to the next
section. However, if you'd like a quick overview, read on.
You might be able to guess that the public
means that the
method named main
is generally accessible (again, provided
the person or program attempting to access it has system permission to
access the associated file).
The static
indicates that the method can run without
having an associated object created. You are probably used to running
methods (procedures) without having associated objects. However, in Java,
one typically associates a method call with a particular object that is
to execute the method call. Hence, we must indicate that main
is an exception to the rule.
In Java, all methods potentially return values. Programmers therefore
have a responsibility to indicate what kind of value the methods return.
(The designers of Java have found that programs behave more reliably if
the compiler can tell the types of values and check to see that the programmer
is using them consistently.) We don't want to return any particular
value from our program (we just want it to run), so we give it return type
void
.
The String[] args
in parentheses provides the one
parameter to the main
method: All the strings typed
on the command line along with the command to execute the Java program.
This parameter is an artifact of Java's designers decision to emulate
some key aspects of the C programming language. We will return to it
after we learn more about arrays.
Finally, the throws Exception
is our admission that our
method can fail. Java expects programmers to indicate when and how
their methods can fail. As you become a more careful and responsible
programmer and can guarantee that your methods will not fail, you can
possibly remove this line.
What kinds of things belong in the body of main
(and in any other methods you decide to write)? A few basic things.
First, you often include declarations of names for objects.
The declaration of a name provides the type of object the name will
refer to and the text of the name. The general form is
Type name;
For example, I might say that pen
will name objects in
class java.io.PrintWriter
with
java.io.PrintWriter pen;
Similarly, you might say that i
is an integer (in Java-ese, an
int
) with
int i;
Note that the semicolon at the end of the declaration is necessary. You will receive strange errors if you fail to include that semicolon.
Once you have declared the name for a kind of value, you can make that name refer to a particular value using the assignment operator (an equals sign).
name = expression;
In many cases, particularly at the beginning of the main
method, the expression at the right-hand side of the assignment
creates a new object. The form of an expression that creates a new
object is most typically
new NameOfClass(parameters)
What class names can you use? You can use any class that you've defined (none, yet), any class that someone has provided for you or that you've downloaded, or any class that comes as part of a standard Java installation.
What parameters do you give when you create a new object? It depends on the kind of object you're creating. You will need to check the documentation for the object to determine what parameters are appropriate.
You've created an object. You've named that object. What else can you do
with the object? You can tell it to do something. (Some programmers say
that this is calling a method
others say it is sending a message
to the object
.) The typical form is
name.method(parameters);
For many of your programs, you will find the following classes useful:
java.lang.String
java.io.PrintWriter
print
and println
methods. You can create PrintWriters with
new java.io.PrintWriter(System.out, true)
new java.io.PrintWriter(file)
java.io.File
new File(pathtofile)
We now know all the basic components of a Java progam, so we can write a typical
first program
which writes a greeting to the screen.
package username.intro; public class First { public static void main(String[] args) throws Exception { // Describe the two names of objects used in the program. java.io.PrintWriter pen; java.lang.String greeting; // Create objects and associate the names with those objects. pen = new java.io.PrintWriter(System.out, true); greeting = "Hello"; // Tell one object to do something. pen.println(greeting); } // main(String[]) } // class First
Friday, 28 January 2005 [Samuel A. Rebelsky]
Sunday, 30 January 2005 [Samuel A. Rebelsky]
Tuesday, 30 August 2005 [Samuel A. Rebelsky]
Monday, 23 January 2006 [Samuel A. Rebelsky]
This page was generated by
Siteweaver on Thu Mar 30 15:24:25 2006.
The source to the page was last modified on Mon Jan 23 09:08:34 2006.
This page may be found at http://www.cs.grinnell.edu/~rebelsky/Espresso/Readings/intro-java.html
.
You may wish to
validate this page's HTML
;
;
Check with Bobby