Programming and Problem Solving with Java

(やまだぃちぅ) #1

(^48) | Java Syntax and Semantics, Classes, and Objects
of processing the computer can perform on it. Recall from Chapter 1 that all ob-
jects are of the same type, but differ in their class. Java includes types other than
Object, each of which has its own name.
Because some types of data are used very frequently, Java provides them for
us. These are called standard(or built-in) types. You are already familiar with most
of them from everyday life: integer numbers, real numbers, and characters. In
Chapter 3, we examine the integer types intand long, and the real types float
and double. By the end of Chapter 4, you’ll be equally familiar with one more type,
boolean.
In this chapter we look first at the built-in type charand then later at the class Stringthat
Java provides for us to manipulate character data.


The charData Type The built-in type chardescribes data consisting of one alphanumeric char-


acter—a letter, a digit, or a special symbol. Java uses a particular character set, or set of al-
phanumeric characters that it can represent. Java’s character set, which is called Unicode,
includes characters for many written languages. In this book, we use a subset of Unicode that
corresponds to an older character set called the American Standard Code for Information
Interchange (ASCII). ASCII consists of the alphabet for the English language, plus numbers
and symbols.
Here are some example values of type char:

‘A’ ‘a’ ‘ 8 ’ ‘ 2 ’ ‘+’ ‘-’ ‘$’ ‘?’ ‘*’ ‘ ’

Using Meaningful, Readable Identifiers


The names we use to refer to things in our code are totally meaningless to the computer. The
computer behaves in the same way whether we call a value 3.14159265,pi, or cake, as long as
we always call it the same thing. Of course, it is much easier for a person to figure out how your
code works if the names you choose for elements actually tell something about them. When
ever you make up a name for something in your code, try to pick one that is meaningful to a
human reader.
Java is a case-sensitivelanguage, which means that it sees uppercase letters as different from
lowercase letters. The identifiers

PRINTTOPPORTION printtopportion pRiNtToPpOrTiOn PrintTopPortion

are four distinct names and are not interchangeable in any way. As you can see, the last of
these forms is the easiest to read. In this book, we use combinations of uppercase letters, low-
ercase letters, and underscores in identifiers. Many Java programmers use different capitaliza-
tions of identifiers as a way to indicate what they represent. Later in this chapter, we show you
the conventions that we, and many other Java programmers, use.

Standard (built-in) type A
data type that is automatically
available for use in every Java
program
Free download pdf