Developers use different editions of the Java platform to create Java programs that run on desktop computers, web browsers, web servers, mobile information devices (such as cell phones), and embedded devices (such as television set-top boxes):
Java Platform, Standard Edition (Java SE): The Java platform for developing applications, which are stand-alone programs that run on desktops. Java SE is also used to develop applets, which are
programs that run in the context of a web browser.
Java Platform, Enterprise Edition (Java EE): The Java platform for developing enterprise-oriented applications and servlets, which are server programs that conform to Java EE’s Servlet API. Java EE is built on top of Java SE.
Java Platform, Micro Edition (Java ME): The Java platform for developing MIDlets, which are programs that run on mobile information devices, and Xlets, which are programs that run on embedded devices.
Developers also use a special Google-created edition of the Java platform (see http://developer.android.com/index.html) to create Android apps that run on Android-enabled devices. This edition is known as the Android platform.
Google’s Android platform largely consists of Java core libraries (partly based on Java SE) and a virtual machine known as Dalvik. This collective software runs on top of a specially modified Linux kernel.
NOTE: Check out Wikipedia’s “Android (operating system)” entry
(http://en.wikipedia.org/wiki/Android_%28operating_system%29) to learn more about the Android OS, and Wikipedia’s “Dalvik (software)” entry
(http://en.wikipedia.org/wiki/Dalvik_%28software%29) to learn more about the Dalvik virtual machine.
In this book, I cover the Java language (supported by Java SE and Android) and Java SE APIs (also supported by Android). Furthermore, I present the source code (typically as code fragments) to Java SE–based applications.
Installing and Exploring the JDK
The Java Runtime Environment (JRE) implements the Java SE platform and makes it possible to run Java programs. The public JRE can be downloaded from the Java SE Downloads page (http://java.sun.com/javase/downloads/index.jsp).
However, the public JRE does not make it possible to develop Java programs. For that task, you need to download and install the Java SE Development Kit (JDK), which contains development tools (including the Java compiler) and a private JRE.
NOTE: JDK 1.0 was the first JDK to be released (in May 1995). Until JDK 6 arrived, JDK stood for Java Development Kit (SE was not part of the title). Over the years, numerous JDKs have been released, with JDK 7 set for release in fall or winter 2010.
Each JDK’s version number identifies a version of Java. For example, JDK 1.0 identifies Java version 1.0, and JDK 5 identifies Java version 5.0. JDK 5 was the first JDK to also provide an internal version number: 1.5.0.
The Java SE Downloads page also provides access to the current JDK, which is JDK 6 Update 20 at time of writing. Click the Download JDK link to download the current JDK’s installer program for your platform.
NOTE: Some of this book’s code requires JDK 7, which is only available as a preview release (http://java.sun.com/javase/downloads/ea.jsp) at time of writing.
The JDK installer installs the JDK in a home directory. (It can also install the public JRE in another directory.) On my Windows XP platform, the home directory is C:\Program Files\Java\jdk1.6.0_16—JDK 6 Update 16 was current when I began this book.
TIP: After installing the JDK, you should add the bin subdirectory to your platform’s PATH environment variable. That way, you will be able to execute JDK tools from any directory in your filesystem.
Finally, you might want to create a projects subdirectory of the JDK’s home directory to organize your Java projects, and create a separate subdirectory within projects for each of these projects.
The home directory contains various files (such as README.html, which provides information about the JDK, and src.zip, which provides the standard class library source code) and subdirectories, including the following three important subdirectories:
bin: This subdirectory contains assorted JDK tools, including the Java compiler tool. You will discover some of these tools shortly.
jre: This subdirectory contains the JDK’s private copy of the JRE, which lets you run Java programs without having to download and install the public JRE.
lib: This subdirectory contains library files that are used by JDK tools.
For example, tools.jar contains the Java compiler’s classfiles—the compiler was written in Java.
You will use only a few of the bin subdirectory’s tools in this book, specifically javac (Java compiler), java (Java application launcher), javadoc (Java documentation generator), and jar (Java archive creator, updater, and extractor).
NOTE: javac is not the Java compiler. It is a tool that loads and starts the virtual machine, identifies the compiler’s main classfile (located in tools.jar) to the virtual machine, and passes the name of the source file being compiled to the compiler’s main classfile.
You execute JDK tools at the command line, passing command-line arguments to a tool.
Learn about the command line and arguments via Wikipedia’s “Command-line interface”
entry (http://en.wikipedia.org/wiki/Command-line_interface).
Now that you have installed the JDK and know something about its tools, you are ready to explore a small DumpArgs application that outputs its command-line arguments to the standard output device.
NOTE: The standard output device is part of a mechanism known as Standard I/O. This mechanism, which consists of Standard Input, Standard Output, and Standard Error, and which originated with the Unix operating system, makes it possible to read text from different sources (keyboard or file) and write text to different destinations (screen or file).
Text is read from the standard input device, which defaults to the keyboard but can be redirected to a file. Text is output to the standard output device, which defaults to the screen but can be redirected to a file. Error message text is output to the standard error device, which defaults to the screen but can be redirected to a file that differs from the standard output file.
Listing 1–1 presents the DumpArgs application source code.
Listing 1–1. Dumping command-line arguments via main()’s args array to the standard output device public class DumpArgs
{
public static void main(String[] args) {
System.out.println("Passed arguments:");
for (int i = 0; i < args.length; i++) System.out.println(args[i]);
} }
Listing 1–1’s DumpArgs application consists of a class named DumpArgs and a method within this class named main(), which is the application’s entry point and provides the code to execute. (You will learn about classes and methods in Chapter 2.)
main() is called with an array of strings (character sequences) that identify the
application’s command-line arguments. These strings are stored in String-based array variable args. (I discuss method calling, arrays, and variables in Chapter 2.)
NOTE: Although the array variable is named args, there is nothing special about this name. You could name this variable anything you want.
main() first executes System.out.println("Passed arguments:");, which calls
System.out’s println() method with the "Passed arguments:" string. This method call outputs Passed arguments: to the standard output device and then terminates the current line so that subsequent output is sent to a new line immediately below the current line. (I discuss System.out in Chapter 7.)
NOTE: System.out provides access to a family of println() methods and a family of print() methods for outputting different kinds of data (such as sequences of characters and integers). Unlike the println() methods, the print() methods do not terminate the current line; subsequent output continues on the current line.
Each println() method terminates a line by outputting a line separator string, which is defined by system property line.separator, and which is not necessarily a single newline character (identified in source code via character literal '\n'). (I discuss system properties in Chapter 7, line.separator in Chapter 10, and character literals in Chapter 2.) For example, on Windows platforms, the line separator string is a carriage return character (whose integer code is 13) followed by a line feed character (whose integer code is 10).
main() uses a for loop to repeatedly execute System.out.println(args[i]);. The loop executes args.length times, which happens to identify the number of strings that are stored in args. (I discuss for loops in Chapter 2.)
The System.out.println(args[i]); method call reads the string stored in the ith entry of the args array—the first entry is located at index (location) 0; the last entry is stored at index args.length - 1. This method call then outputs this string to standard output.
Assuming that you are familiar with your platform’s command-line interface and are at the command line, make DumpArgs your current directory and copy Listing 1–1 to a file named DumpArgs.java. Then compile this source file via the following command line:
javac DumpArgs.java
Assuming that that you have included the .java extension, which is required by javac, and that DumpArgs.java compiles, you should discover a file named DumpArgs.class in the current directory. Run this application via the following command line:
java DumpArgs
If all goes well, you should see the following line of output on the screen:
Passed arguments:
For more interesting output, you will need to pass command-line arguments to
DumpArgs. For example, execute the following command line, which specifies Curly, Moe, and Larry as three arguments to pass to DumpArgs:
java DumpArgs Curly Moe Larry
This time, you should see the following expanded output on the screen:
Passed arguments:
Curly Moe Larry
You can redirect the output destination to a file by specifying the greater than angle bracket (>) followed by a filename. For example, java DumpArgs Curly Moe Larry
>out.txt stores the DumpArgs application’s output in a file named out.txt.
NOTE: Instead of specifying System.out.println(), you could specify
System.err.println() to output characters to the standard error device. (System.err provides the same families of println() and print() methods as System.out.) However, you should only switch from System.out to System.err when you need to output an error message so that the error messages are displayed on the screen, even when standard output is redirected to a file.
Congratulations on successfully compiling your first application source file and running the application! Listing 1–2 presents the source code to a second application, which echoes text obtained from the standard input device to the standard output device.
Listing 1–2. Echoing text read from standard input to standard output public class EchoText
{
public static void main(String[] args) throws java.io.IOException {
System.out.println("Please enter some text and press Enter!");
int ch;
while ((ch = System.in.read()) != 13) System.out.print((char) ch);
System.out.println();
} }
After outputting a message that prompts the user to enter some text, main() introduces int variable ch to store each character’s integer representation. (You will learn about int and integer in Chapter 2.)
main() now enters a while loop (discussed in Chapter 2) to read and echo characters.
The loop first calls System.in.read() to read a character and assign its integer value to ch. The loop ends when this value equals 13 (the integer value of the Enter key).
NOTE: When standard input is not redirected to a file, System.in.read() returns 13 to indicate that the Enter key has been pressed. On platforms such as Windows, a subsequent call to System.in.read() returns integer 10, indicating a line feed character. Whether or not standard input has been redirected, System.in.read() returns -1 when there are no more characters to read.
For any other value in ch, this value is converted to a character via (char), which is an example of Java’s cast operator (discussed in Chapter 2). The character is then output via System.out.println(). The final System.out.println(); call terminates the current line without outputting any content.
NOTE: When standard input is redirected to a file and System.in.read() is unable to read text from the file (perhaps the file is stored on a removable storage device that has been removed prior to the read operation), System.in.read() fails by throwing an object that describes this problem. I acknowledge this possibility by appending throws java.io.IOException to the end of the main() method header. I discuss throws in Chapter 4 and java.io.IOException in Chapter 10.
Compile Listing 1–2 via javac EchoText.java, and run the application via java EchoText.
You will be prompted to enter some text. After you input this text and press Enter, the text will be sent to standard output. For example, consider the following output:
Please enter some text and press Enter!
Hello Java Hello Java
You can redirect the input source to a file by specifying the less than angle bracket (<) followed by a filename. For example, java EchoText <EchoText.java reads its text from EchoText.java and outputs this text to the screen.
Run this application and you will only see EchoText.java’s first line of text. Each one of this file’s lines ends in a carriage return character (13) (followed by a line feed character, 10, on Windows platforms), and EchoText terminates after reading a carriage return.
In addition to downloading and installing the JDK, you might want to download the JDK’s companion documentation archive file (jdk-6u18-docs.zip is the most recent file at time of writing).
After downloading the documentation archive file from the same Java SE Downloads page (http://java.sun.com/javase/downloads/index.jsp), unzip this file and move its docs directory to the JDK’s home directory.
To access the documentation, point your web browser to the documentation’s start page. For example, after moving docs to my JDK’s home directory, I point my browser to C:\Program Files\Java\jdk1.6.0_16\docs\index.html. See Figure 1–1.
Scroll a bit down the start page and you discover the “API, Language, and Virtual Machine Documentation” section, which presents a Java 2 Platform API Specification link. Click this link and you can access the standard class library’s documentation.
TIP: You can read the online documentation by pointing your web browser to a link such as http://download.java.net/jdk6/archive/b104/docs/, which provides the online documentation for JDK 6.
Figure 1–1. The first part of the Java documentation’s start page
Installing and Exploring Two Popular IDEs
Working with the JDK’s tools at the command line is probably okay for small projects.
However, this practice is not recommended for large projects, which are hard to manage without the help of an integrated development environment (IDE).
An IDE consists of a project manager for managing a project’s files, a text editor for entering and editing source code, a debugger for locating bugs, and other features. Two popular IDEs are NetBeans and Eclipse.
NOTE: For convenience, I use JDK tools throughout this book, except for this section where I use NetBeans IDE and Eclipse IDE.