Java Reference
In-Depth Information
instead of the previously typed date. Unlike the typed date, the variable
currentDate is not enclosed in quotation marks, so it will not literally print
the words, currentDate, on the screen. Alternately, it will print whatever value
is in the storage location at run time — in this case, the system date.
In the Java programming language, you can use a plus sign (+) to join or
concatenate two or more Strings: the plus sign (+) typed between the escape
characters and the currentDate variable in the println() argument in line 20 tells
the program to display the system date directly after the literal String of escape
characters that move the insertion point to the right. Perform the following step
to enter the code to display the system date.
To Enter Code to Concatenate String Data
1. Select the text "October 4, 2007" in line 20 by dragging through it.
Type "\t\t\t" + currentDate as shown in Figure 2-33 on page 79.
The edited code is displayed (Figure 2-33). Line 20 instructs the program to
display the system date indented 24 characters to the right.
If you want to embed data within a string of text rather than concatenate it,
a new printf() method is available in Java 5.0. This functionality, which will help
developers who have used the C programming language in the past, uses a per-
cent sign (%) followed by one of several formatting characters to display format-
ted data. Similar to the escape characters, the percent sign (%) and its formatting
character do not print during execution; rather, they are replaced with data indi-
cated by one or more data items or variable names listed in the method.
The general format is
System.out.printf(format, items);
For example, if you wanted to display a stored user's name in a message, the
printf() method could be written as
System.out.printf(“Hello, %s. Welcome to this application.”,
userName);
During execution, the %s is replaced with the data from the variable, userName.
Besides the formatting character s, which is used for String data, Java 5.0
supports other types of data and precision of numbers for formatted output,
which you will learn about in Chapter 3.
Recompiling and Running the Application
As you have learned, after compiling, the Java compiler created the file
Welcome.class, which contains the bytecode or object code from your program.
The bytecode is the code that actually executes when you run the Java applica-
tion using TextPad or the command prompt window. If you have not recompiled
the source code after editing, Java will execute the old bytecode and any updates
will not appear. In order to run the most recent version of the application, the
source code first must be recompiled into new bytecode.
Follow these steps to recompile and then run the application.
Search WWH ::




Custom Search