THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

Exercise 1.5: Change the HelloWorld application to use a named string constant as the string to print. (A
string constant can be initialized with a string literal.)


Exercise 1.6: Change your program from Exercise 1.3 to use a named string constant for the title.


1.5. Unicode Characters


Suppose we were defining a class that dealt with circles and we wanted a named constant that represented the
value π. In most programming languages we would name the constant "pi" because in most languages
identifiers (the technical term for names) are limited to the letters and digits available in the ASCII character
set. In the Java programming language, however, we can do this:


class Circle {
static final double π = 3.14159265358979323846;
// ...
}


The Java programming language moves you toward the world of internationalized software: you write code in
Unicode, an international character set standard. Unicode basic[1] characters are 16 bits, and together with the
supplemental characters (21 bits) provide a character range large enough to write the major languages used in
the world. That is why we can use π for the name of the constant in the example. π is a valid letter from the
Greek section of Unicode and is therefore valid in source. Most existing code is typed in ASCII, a 7-bit
character standard, or ISO Latin-1, an 8-bit character standard commonly called Latin-1. But these characters
are translated into Unicode before processing, so the character set is always Unicode.


[1] Basic Multilingual Plane, or BMP, in Unicode terminology.

1.6. Flow of Control


"Flow of control" is the term for deciding which statements in a program are executed and in what order. The
while loop in the Fibonacci program is one control flow statement, as are blocks, which define a
sequential execution of the statements they group. Other control flow statements include for, ifelse,
switch, and dowhile. We change the Fibonacci sequence program by numbering the elements of the
sequence and marking even numbers with an asterisk:


class ImprovedFibonacci {


static final int MAX_INDEX = 9;


/**



  • Print out the first few Fibonacci numbers,

  • marking evens with a ''
    /
    public static void main(String[] args) {
    int lo = 1;
    int hi = 1;
    String mark;


System.out.println("1: " + lo);
for (int i = 2; i <= MAX_INDEX; i++) {
if (hi % 2 == 0)
mark = " *";
else

Free download pdf