Java Reference
In-Depth Information
Java is an interpretive, object-oriented programming language
that Sun Microsystems developed. A considerable benefi t of
writing Java applications is that they run in a Java Runtime
Environment (JRE) that is well defi ned. As a Java programmer, you know your Java
program is going to run on a Java Virtual Machine (JVM), regardless of the device or
operating system. Consequently, you know an int is 32 bits and signed, a boolean is true
or false , method arguments are passed by value, and the garbage collector cleans up your
unreachable objects whenever it feels like it. (Okay, not every aspect of Java is an exact
science!) The point is that Java runs in a precise environment, and passing the SCJP exam
requires a strong knowledge of these well-defi ned Java fundamentals.
This chapter covers the fundamentals of Java programming, including writing Java
classes, running Java applications, creating packages, defi ning classpath, and using the Java
operators. We will also discuss the details of garbage collection and call by value.
Writing Java Classes
The exam objectives state that you need to be able to “write code that uses the appropriate
access modifi ers, package declarations, and imports statements.” In other words, you
need to be able to write Java classes, which makes sense because Java is an object-oriented
programming (OOP) language and writing classes is an essential aspect of OOP. Your
executable Java code will appear within the defi nition of a class. A class describes an
object, which is a noun in your program. The object can either represent something
tangible, like a television or an employee, or it can represent something less obvious but just
as useful in your program, like an event handler or a stream of data being read from a fi le.
An object is an instance of a class. Think of a class as a blueprint for a house, and the
object as the house. Another common analogy is to think of a class as a recipe for cookies,
and the objects are the cookies. (We will discuss the details of instantiating objects in
Chapter 2, “Declarations, Initialization, and Scoping.”) Because classes are a fundamental
aspect of Java programming, the certifi cation exam assumes you are familiar with the rules
for writing them, and in this section we cover these details.
For starters, a Java class must be defi ned in a text fi le with a .java extension. In
addition, if the class is declared public , then the name of the fi le must match the name of
the class. Consequently, a .java fi le can only contain at most one top-level public class.
For example, the following class defi nition must appear in a fi le named Cat.java :
Search WWH ::




Custom Search