88
or to a location whose value can change (a variable). Every constant and variable has
an associated data type or class. Java provides many predefined data types and
classes. In this chapter, we examined the chartype and the String class. A class
contains fields and methods that describe the behavior of an object. An object is an
instance of the class that describes it.
You use the assignment operator to change the value of a variable by assigning the
value of an expression to it. At execution time, the expression is evaluated and the re-
sult is stored into the variable. With the Stringclass, the plus sign (+) is an operator
that concatenates two strings. A string expression can concatenate any number of
strings to form a new Stringvalue.
Simple output to the screen is accomplished by using the System.outobject that is
provided in Java. Two methods are defined on this object: printand println.
System.out.print("A string")prints whatever is between the parentheses on the
screen.printlnbehaves in exactly the same way as print, except that printlngoes to
the next line after it finishes the printing. Simple input is not so simple in Java,
however; it requires that we filter the input from System.infirst through an
InputStreamReaderand then a BufferedReader, which enables us to input a line of typing
as a string value. (We defer the use of a graphical user interface until Chapter 8.)
A Java application is a publicclass containing one or more class declarations, which
are fields and methods. One of the methods mustbe named main. Execution of an
application class always begins with the mainmethod. User classes may also be
included in the application class, but cannot be public.
A class begins with importdeclarations and a heading, then a block containing class
declarations of fields and methods. Methods are declared with a heading and a block.
Four types of methods exist: instance methods, class methods, helper methods, and con-
structors. Each type of method is called with its name and an argument list, but preceded
by an object name, a class name, nothing, or new, respectively. A constructor is called
when an object is instantiated via the newoperator, and its name is the same as the name
of the class.
Quick Check
1.What is syntax? (p. 42)
2.Why do we write meaningful identifiers in our code? (p. 48)
3.What is stored in a variable that is of a primitive type? (p. 63)
4.How does a class in Java differ from a class in the abstract sense? (pp. 50–51)
5.How do objects in the general sense differ from objects in Java? (pp. 50–51)
6.Is charan object or a primitive type? (pp. 48–50)
7.What distinguishes a named constant from a variable field? (pp. 57–58)
8.When an object is assigned to a variable, what is actually stored there? (p. 63)