Programming and Problem Solving with Java

(やまだぃちぅ) #1
2.1 The Elements of Java Programs | 59

They store “Introduction to “as the value for constantWORD1. The string “Programming and
Problem Solving “is stored in the variable word3, and we store “with Java”in the constant WORD5.


Assignment and Expressions


Up to this point, we’ve looked at ways of declaring fields in a class. As part of the declara-
tion, we can give an initial value to a field. Now we turn our attention to ways of acting, or
performing operations, on values in fields.


Capitalization of Identifiers


Programmers often use capitalization to provide a quick, visual clue as to what an identifier
represents. Different programmers adopt different conventions for using uppercase letters and
lowercase letters. Some use only lowercase letters, separating the English words in an identifier
with the underscore character:

pay_rate emp_num pay_file

The convention used by many Java programmers and the one we use in this book is the follow-
ing:


  • Variables and methods begin with a lowercase letter and capitalize each successive English
    word.
    lengthInYards middleInitial hours

  • Class names begin with an uppercase letter but are capitalized the same as variable names
    thereafter.
    PayRollFrame Sample MyDataType String
    Capitalizing the first letter allows a person reading the code to tell at a glance that an identi-
    fier represents a class name rather than a variable or method. Java’s reserved words use all
    lowercase letters, so the type charis lowercase.Stringis a class, so it begins with a capital let-
    ter.

  • Identifiers representing named constants are all uppercase with underscores used to separate
    the English words.
    BOOK_TITLE OVERTIME MAX_LENGTH
    These conventions are simply that—conventions. Java does not require this particular style
    of capitalizing identifiers. You may wish to write your identifiers in a different fashion. But
    whatever method you use, it is essential that you maintain a consistent style throughout your
    code. A person reading your code will be confused or misled if you use a random style of capi-
    talization.

Free download pdf