Tải bản đầy đủ

Learn java for android development, 3rd edition

Learn the Java skills you will need to start
developing Android apps

Learn

Java for Android
Development
THIRD EDITION
Jeff Friesen

www.it-ebooks.info


For your convenience Apress has placed some of the front
matter material after the index. Please use the Bookmarks
and Contents at a Glance links to access them.

www.it-ebooks.info


Contents at a Glance

About the Author��������������������������������������������������������������������������������������������������������������� xxi
About the Technical Reviewer����������������������������������������������������������������������������������������� xxiii
Acknowledgments������������������������������������������������������������������������������������������������������������ xxv
Introduction�������������������������������������������������������������������������������������������������������������������� xxvii
■■Chapter 1: Getting Started with Java��������������������������������������������������������������������������������1
■■Chapter 2: Learning Language Fundamentals�����������������������������������������������������������������31
■■Chapter 3: Discovering Classes and Objects�������������������������������������������������������������������89
■■Chapter 4: Discovering Inheritance, Polymorphism, and Interfaces�����������������������������141
■■Chapter 5: Mastering Advanced Language Features, Part 1�����������������������������������������189
■■Chapter 6: Mastering Advanced Language Features, Part 2�����������������������������������������235
■■Chapter 7: Exploring the Basic APIs, Part 1������������������������������������������������������������������287
■■Chapter 8: Exploring the Basic APIs, Part 2������������������������������������������������������������������359
■■Chapter 9: Exploring the Collections Framework����������������������������������������������������������401
■■Chapter 10: Exploring the Concurrency Utilities�����������������������������������������������������������487
■■Chapter 11: Performing Classic I/O�������������������������������������������������������������������������������539
■■Chapter 12: Accessing Networks����������������������������������������������������������������������������������621
v

www.it-ebooks.info


vi

Contents at a Glance

■■Chapter 13: Migrating to New I/O���������������������������������������������������������������������������� 665
■■Chapter 14: Accessing Databases��������������������������������������������������������������������������� 763
■■Chapter 15: Parsing, Creating, and Transforming XML Documents������������������������� 803
■■Chapter 16: Focusing on Odds and Ends����������������������������������������������������������������� 885
■■Appendix A: Solutions to Exercises����������������������������������������������������������������������� 1015
■■Appendix B: Four of a Kind������������������������������������������������������������������������������������ 1127
Index������������������������������������������������������������������������������������������������������������������������� 1149

www.it-ebooks.info


Introduction
Smartphones and tablets are all the rage these days. Their popularity is largely due to their ability to
run apps. Although the iPhone and iPad, with their growing collection of Objective-C based apps,
had a head start, Android-based smartphones and tablets, with their growing collection of


Java-based apps, have proven to be a strong competitor.
Not only are many iPhone/iPad app developers making money by selling their apps, but many
Android app developers are also making money by selling similar apps. According to tech web sites
such as The Register (www.theregister.co.uk), some Android app developers are making lots of
money (www.theregister.co.uk/2010/03/02/android_app_profit).
In today’s challenging economic climate, you might like to try your hand at developing Android apps
and make some money. If you have good ideas, perseverance, and some artistic talent (or perhaps
know some talented individuals), you are already part of the way toward achieving this goal.

Tip  A good reason to consider Android app development over iPhone/iPad app development is the lower
startup costs that you’ll incur with Android. For example, you don’t need to purchase a Mac on which to
develop Android apps (a Mac is required for developing iPhone/iPad apps); your existing Windows, Linux, or
Unix machine will do nicely.

Most importantly, you’ll need to possess a solid understanding of the Java language and
foundational application programming interfaces (APIs) before jumping into Android. After all,
Android apps are written in Java and interact with many of the standard Java APIs (such as
threading and input/output APIs).
I wrote Learn Java for Android Development to give you a solid Java foundation that you can later
extend with knowledge of Android architecture, API, and tool specifics. This book will give you a
strong grasp of the Java language and the many important APIs that are fundamental to Android
apps and other Java applications. It will also introduce you to key development tools.
xxvii

www.it-ebooks.info


xxviii

Introduction

Book Organization
The first edition of this book was organized into 10 chapters and 1 appendix. The second edition
was organized into 14 chapters and 3 appendixes. This third edition is organized into 16 chapters
and 2 appendixes with a bonus appendix on Android app development. Each chapter in each edition
offers a set of exercises that you should complete to get the most benefit from its content. Their
solutions are presented in Appendix A.
Chapter 1 introduces you to Java by first focusing on Java’s dual nature (language and platform). It
then briefly introduces you to Oracle’s Java SE, Java EE, and Java ME editions of the Java platform.
You next learn how to download and install the Java SE Development Kit (JDK), and you learn some
Java basics by developing and playing with three simple Java applications. After receiving a brief
introduction to the Eclipse IDE, you receive a brief introduction to Android.
Chapter 2 starts you on an in-depth journey of the Java language by focusing on language
fundamentals. You first learn about simple application structure and then learn about comments,
identifiers (and reserved words), types, variables, expressions (and literals), and statements.
Chapter 3 continues your journey by focusing on classes and objects. You learn how to declare a
class and organize applications around multiple classes. You then learn how to construct objects
from classes, declare fields in classes and access these fields, declare methods in classes and call
them, initialize classes and objects, and remove objects when they’re no longer needed. You also
learn more about arrays, which were first introduced in Chapter 2.
Chapter 4 adds to Chapter 3’s pool of object-based knowledge by introducing you to the language
features that take you from object-based applications to object-oriented applications. Specifically,
you learn about features related to inheritance, polymorphism, and interfaces. While exploring
inheritance, you learn about Java’s ultimate superclass. Also, while exploring interfaces, you
discover why they were included in the Java language; interfaces are not merely a workaround for
Java’s lack of support for multiple implementation inheritance, but serve a higher purpose.
Chapter 5 introduces you to four categories of advanced language features: nested types, packages,
static imports, and exceptions.
Chapter 6 introduces you to four additional advanced language feature categories: assertions,
annotations, generics, and enums.
Chapter 7 begins a trend that focuses more on APIs than language features. This chapter first
introduces you to Java’s Math and StrictMath math-oriented types. It then explores Number and its
various subtypes (such as Integer, Double, and BigDecimal). Next you explore the string-oriented
types (String, StringBuffer, and StringBuilder) followed by the System type. Finally, you explore
the Thread class and related types for creating multithreaded applications.
Chapter 8 continues to explore Java’s basic APIs by focusing on the Random class for generating
random numbers; the References API, Reflection, the StringTokenizer class for breaking a string
into smaller components; and the Timer and TimerTask classes for occasionally or repeatedly
executing tasks.
Chapter 9 focuses exclusively on Java’s Collections Framework, which provides you with a solution
for organizing objects in lists, sets, queues, and maps. You also learn about collection-oriented utility
classes and review Java’s legacy collection types.

www.it-ebooks.info


Introduction

xxix

Chapter 10 focuses exclusively on Java’s Concurrency Utilities. After receiving an introduction to
this framework, you explore executors, synchronizers (such as countdown latches), concurrent
collections, the Locking Framework, and atomic variables (where you discover compare-and-swap).
Chapter 11 is all about classic input/output (I/O), largely from a file perspective. In this chapter, you
explore classic I/O in terms of the File class, RandomAccessFile class, various stream classes, and
various writer/reader classes. My discussion of stream I/O includes coverage of Java’s object
serialization and deserialization mechanisms.
Chapter 12 continues to explore classic I/O by focusing on networks. You learn about the Socket,
ServerSocket, DatagramSocket, and MulticastSocket classes along with related types. You also
learn about the URL class for achieving networked I/O at a higher level and learn about the related
URI class. After learning about the low-level NetworkInterface and InterfaceAddress classes, you
explore cookie management, in terms of the CookieHandler and CookieManager classes, and the
CookiePolicy and CookieStore interfaces.
Chapter 13 introduces you to New I/O. You learn about buffers, channels, selectors, regular
expressions, charsets, and the Formatter and Scanner types in this chapter.
Chapter 14 focuses on databases. You first learn about the Java DB and SQLite database products,
and then explore JDBC for communicating with databases created via these products.
Chapter 15 emphasizes Java’s support for XML. I first provide a tutorial on this topic where you
learn about the XML declaration, elements and attributes, character references and CDATA
sections, namespaces, comments and processing instructions, well-formed documents, and valid
documents (in terms of Document Type Definition and XML Schema). I then show you how to parse
XML documents via the SAX API, parse and create XML documents via the DOM API, parse XML
documents via the XMLPULL V1 API (supported by Android as an alternative to Java’s StAX API),
use the XPath API to concisely select nodes via location path expressions, and transform XML
documents via XSLT.
Chapter 16 completes the chapter portion of this book by covering odds and ends. You first learn
about useful Java 7 language features that I’ve successfully used in Android apps. Next, you explore
classloaders, the Console class, design patterns (with emphasis on the Strategy pattern), double
brace initialization, fluent interfaces, immutability, internationalization (in terms of locales; resource
bundles; break iterators; collators; dates, time zones, and calendars; and formatters), the Logging
API, the Preferences API, the Runtime and Process classes, the Java Native Interface, and the ZIP
and JAR APIs.
Appendix A presents solutions to all of the exercises in Chapters 1 through 16.
Appendix B introduces you to application development in the context of Four of a Kind, a console-based
card game.
Appendix C provides an introduction to Android app development. It gives you a chance to see how
various Java language features and APIs are used in an Android context.
Unlike the other elements, Appendix C is not included in this book—it’s included with the book’s
source code. Appendix C doesn’t officially belong in Learn Java for Android Development because
this book’s focus is to prepare you for getting into Android app development by teaching you the
fundamentals of the Java language, and Appendix C goes beyond that focus by giving you a tutorial
on Android app development. Besides, the presence of this appendix would cause the book to
exceed the 1,200-page print-on-demand limit.
www.it-ebooks.info


xxx

Introduction

Note  You can download this book’s source code by pointing your web browser to
www.apress.com/9781430264545 and clicking the Source Code tab followed by the Download Now link.

What Comes Next?
After you complete this book, I recommend that you check out Apress’s other Android-oriented
books, such as Beginning Android 4 by Grant Allen (Apress, 2012), and learn more about developing
Android apps. In that book, you learn Android basics and how to create “innovative and salable
applications for Android 4 mobile devices.”
Thanks for purchasing this third (and my final) edition of Learn Java for Android Development. I hope
you find it a helpful preparation for, and I wish you lots of success in achieving, a satisfying and
lucrative career as an Android app developer.
—Jeff Friesen, January 2014

www.it-ebooks.info


Chapter

1

Getting Started with Java
Android apps are written in Java and use various Java application program interfaces (APIs).
Because you’ll want to write your own apps, but may be unfamiliar with the Java language and these
APIs, this book teaches you about Java as a first step into Android app development. It provides you
with Java language fundamentals and Java APIs that are useful when developing apps.

Note  This book illustrates Java concepts via non-Android Java applications. It’s easier for beginners to
grasp these applications than corresponding Android apps. However, I also reveal a trivial Android app toward
the end of this chapter for comparison purposes.
An API is an interface that application code uses to communicate with other code, which is typically stored
in a software library. For more information on this term, check out Wikipedia’s “Application programming
interface” topic at http://en.wikipedia.org/wiki/Application_programming_interface.

This chapter sets the stage for teaching you the essential Java concepts that you need to
understand before embarking on an Android app development career. I first answer the question:
“What is Java?” Next, I show you how to install the Java SE Development Kit (JDK) and introduce
you to JDK tools for compiling and running Java applications.
After presenting a few simple example applications, I show you how to install and use the open
source Eclipse IDE (integrated development environment) so that you can more easily (and more
quickly) develop Java applications and (eventually) Android apps. I then provide you with a brief
introduction to Android and show you how Java fits into the Android development paradigm.

1

www.it-ebooks.info


2

CHAPTER 1: Getting Started with Java

What Is Java?
Java is a language and a platform originated by Sun Microsystems. In this section, I briefly describe
this language and reveal what it means for Java to be a platform. To meet various needs, Sun
organized Java into three main editions: Java SE, Java EE, and Java ME. This section briefly
explores each of these editions.

Note  Java has an interesting history that dates back to December 1990. At that time, James Gosling,
Patrick Naughton, and Mike Sheridan (all employees of Sun Microsystems) were given the task of figuring
out the next major trend in computing. They concluded that one trend would involve the convergence of
computing devices and intelligent consumer appliances. Thus was born the Green Project.
The fruits of Green were Star7, a handheld wireless device featuring a five-inch color LCD screen, a SPARC
processor, a sophisticated graphics capability, a version of Unix, and Oak, a language developed by James
Gosling for writing applications to run on Star7 that he named after an oak tree growing outside of his
office window at Sun. To avoid a conflict with another language of the same name, Dr. Gosling changed this
language’s name to Java.
Sun Microsystems subsequently evolved the Java language and platform until Oracle acquired Sun in early
2010. Check out http://oracle.com/technetwork/java/index.html for the latest Java news
from Oracle.

Java Is a Language
Java is a language in which developers express source code (program text). Java’s syntax (rules
for combining symbols into language features) is partly patterned after the C and C++ languages in
order to shorten the learning curve for C/C++ developers.
The following list identifies a few similarities between Java and C/C++:
 Java and C/C++ share the same single-line and multi-line comment styles.
Comments let you document source code.
 Many of Java’s reserved words are identical to their C/C++ counterparts (for, if,
switch, and while are examples) and C++ counterparts (catch, class, public,
and try are examples).
 Java supports character, double precision floating-point, floating-point, integer,
long integer, and short integer primitive types via the same char, double, float,
int, long, and short reserved words.
 Java supports many of the same operators, including arithmetic (+, -, *, /, and %)
and conditional (?:) operators.
 Java uses brace characters ({ and }) to delimit blocks of statements.

www.it-ebooks.info


CHAPTER 1: Getting Started with Java

3

The following list identifies a few of the differences between Java and C/C++:
 Java supports an additional comment style known as Javadoc.
 Java provides reserved words not found in C/C++ (extends, strictfp,
synchronized, and transient are examples).
 Java doesn’t require machine-specific knowledge. It supports the byte integer
type (see http://en.wikipedia.org/wiki/Integer_(computer_science)), doesn’t
provide a signed version of the character type, and doesn’t provide unsigned
versions of integer, long integer, and short integer. Furthermore, all of Java’s
primitive types have guaranteed implementation sizes, which is an important
part of achieving portability (discussed later). The same cannot be said of
equivalent primitive types in C and C++.
 Java provides operators not found in C/C++. These operators include
instanceof and >>> (unsigned right shift).
 Java provides labeled break and continue statements that you’ll not find
in C/C++.
You’ll learn about single-line, multi-line, and Javadoc comments in Chapter 2. Also, you’ll learn about
reserved words, primitive types, operators, blocks, and statements (including labeled break and
labeled continue) in that chapter.
Java was designed to be a safer language than C/C++. It achieves safety in part by not letting you
overload operators and by omitting C/C++ features such as pointers (storage locations containing
addresses; see http://en.wikipedia.org/wiki/Pointer_(computer_programming)).
Java also achieves safety by modifying certain C/C++ features. For example, loops must be
controlled by Boolean expressions instead of integer expressions where 0 is false and a nonzero
value is true. (There is a discussion of loops and expressions in Chapter 2.)
Suppose you must code a C/C++ while loop that repeats no more than 10 times. Being tired,
you specify the following:

while (x)
x++;


Assume that x is an integer-based variable initialized to 0 (I discuss variables in Chapter 2). This loop
repeatedly executes x++ to add 1 to x’s value. This loop doesn’t stop when x reaches 10; you have
introduced a bug.
This problem is less likely to occur in Java because it complains when it sees while (x).
This complaint requires you to recheck your expression, and you will then most likely specify while
(x != 10). Not only is safety improved (you cannot specify just x), but meaning is also clarified:
while (x != 10) is more meaningful than while (x).
These and other fundamental language features support classes, objects, inheritance,
polymorphism, and interfaces. Java also provides advanced features related to nested types,
packages, static imports, exceptions, assertions, annotations, generics, enums, and more.
Subsequent chapters explore most of these language features.

www.it-ebooks.info


4

CHAPTER 1: Getting Started with Java

Java Is a Platform
Java is a platform consisting of a virtual machine and an execution environment. The virtual machine
is a software-based processor that presents an instruction set, and it is commonly referred to as the
Java Virtual Machine (JVM). The execution environment consists of libraries for running programs
and interacting with the underlying operating system (also known as the native platform).
The execution environment includes a huge library of prebuilt classfiles that perform common tasks,
such as math operations (trigonometry, for example) and network communications. This library is
commonly referred to as the standard class library.
A special Java program known as the Java compiler translates source code into object code
consisting of instructions that are executed by the JVM and associated data. These instructions are
known as bytecode. Figure 1-1 shows this translation process.

Java
Source
Code

Java
Compiler

Java
Object
Code

Figure 1-1.  The Java compiler translates Java source code into Java object code consisting of bytecode and associated data

The compiler stores a program’s bytecode and data in files having the .class extension. These files
are known as classfiles because they typically store the compiled equivalent of classes, a language
feature discussed in Chapter 3. Figure 1-2 shows the organization of a classfile.
Magic Number
Version Number
Constant Pool
Access Flags
This Class
Superclass
Interfaces
Fields
Methods
Class/Interface Attributes

Figure 1-2.  A classfile is organized into a magic number, version number, constant pool, and seven other sections

Don’t worry about having to know this classfile architecture. I present it to satisfy the curiosities of
those who are interested in learning more about how classfiles are organized.
A Java program executes via a tool that loads and starts the JVM and passes the program’s main
classfile to the machine. The JVM uses its classloader component to load the classfile into memory.
www.it-ebooks.info


CHAPTER 1: Getting Started with Java

5

After the classfile has been loaded, the JVM’s bytecode verifier component makes sure that the
classfile’s bytecode is valid and doesn’t compromise security. The verifier terminates the JVM when
it finds a problem with the bytecode.
Assuming that all is well with the classfile’s bytecode, the JVM’s interpreter component interprets the
bytecode one instruction at a time. Interpretation consists of identifying bytecode instructions and
executing equivalent native instructions.

Note  Native instructions (also known as native code) are the instructions understood by the native
platform’s physical processor.
When the interpreter learns that a sequence of bytecode instructions is executed repeatedly,
it informs the JVM’s just-in-time (JIT) compiler to compile these instructions into native code.
JIT compilation is performed only once for a given sequence of bytecode instructions. Because the
native instructions execute instead of the associated bytecode instruction sequence, the program
executes much faster.
During execution, the interpreter might encounter a request to execute another classfile’s bytecode.
When that happens, it asks the classloader to load the classfile and the bytecode verifier to verify the
bytecode before executing that bytecode.
Also during execution, bytecode instructions might request that the JVM open a file, display
something on the screen, or perform another task that requires cooperation with the native platform.
The JVM responds by transferring the request to the platform via its Java Native Interface (JNI)
bridge to the native platform. Figure 1-3 shows these JVM tasks.

Additional
Java
Classfiles

Main
Java
Classfile

load
Classloader

Bytecode Verifier

Interpreter/JIT Compiler

store

Class

verify

execute
Memory

Java Virtual Machine

Java Native Interface Calls
Native Platform

Figure 1-3.  The JVM provides all of the necessary components for loading, verifying, and executing a classfile

www.it-ebooks.info


6

CHAPTER 1: Getting Started with Java

The platform side of Java promotes portability by providing an abstraction over the underlying
platform. As a result, the same bytecode runs unchanged on Windows, Linux, Mac OS X, and
other platforms.
Note  Java was introduced with the slogan “write once, run anywhere.” Although Java goes to great lengths
to enforce portability (such as defining an integer always to be 32 binary digits [bits] and a long integer
always to be 64 bits (see http://en.wikipedia.org/wiki/Bit to learn about binary digits), it doesn’t
always succeed. For example, despite being mostly platform independent, certain parts of Java (such as the
scheduling of threads, discussed in Chapter 7) vary from underlying platform to underlying platform.
The platform side of Java also promotes security by doing its best to provide a secure environment
(such as the bytecode verifier) in which code executes. The goal is to prevent malicious code from
corrupting the underlying platform (and possibly stealing sensitive information).

Note  Many security issues that have plagued Java have prompted Oracle to release various security
updates. For example, blogger Brian Krebs reported on a recent update (at time of this writing) that
fixes 51 security issues in his “Critical Java Update Plugs 51 Security Holes” blog post (http://
krebsonsecurity.com/2013/10/java-update-plugs-51-security-holes/). Although troubling,
Oracle is keeping on top of this ongoing problem (whose impact on Android is minimal).

Java SE, Java EE, and Java ME
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 feature 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 web browsers.
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.
This book largely focuses on Java SE and applications.

www.it-ebooks.info


CHAPTER 1: Getting Started with Java

7

Note  Oracle is also championing Java Embedded, a collection of technologies that brings Java to all kinds
of devices (such as smartcards and vehicle navigation systems). Java SE Embedded and Java ME Embedded
are the two major subsets of Java Embedded.

Installing the JDK and Exploring Example Applications
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 Oracle’s Java SE Downloads page at
www.oracle.com/technetwork/java/javase/downloads/index.html.
However, the public JRE doesn’t make it possible to develop Java (and Android) applications.
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 wasn’t part of the title). Over the years, numerous JDKs have been released, with JDK 7
being current at time of this writing.
Each JDK’s version number identifies a version of Java. For example, JDK 1.0 identifies Java 1.0, and JDK 5
identifies Java 5.0. JDK 5 was the first JDK also to provide an internal version number: 1.5.0.
The Java SE Downloads page also provides access to the current JDK, which is JDK 7 Update 45
at time of this writing. Click the appropriate Download button to download the current JDK’s installer
application for your platform. Then run this application to install the JDK.
The JDK installer places the JDK in a home directory. (It can also install the public JRE in another
directory.) On my Windows 7 platform, the home directory is C:\Program Files\Java\jdk1.7.0_06.
(I currently use JDK 7 Update 6—I’m slow to upgrade.)
Tip  After installing the JDK, you should add the bin subdirectory to your platform’s PATH environment
variable (see http://java.com/en/download/help/path.xml) so that you can execute JDK tools
from any directory. Also, 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.

www.it-ebooks.info


8

CHAPTER 1: Getting Started with Java

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. You’ll use only a few of
these tools in this book, mainly javac (Java compiler) and java (Java application
launcher). However, you’ll also work with jar (Java ARchive [JAR] creator,
updater, and extractor—a JAR file is a ZIP file with special features), javadoc
(Java documentation generator), and serialver (serial version inspector).
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.
Note  javac isn’t the Java compiler. It’s a tool that loads and starts the JVM, identifies the compiler’s main
classfile (located in tools.jar) to the JVM, 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. For a
quick refresher on the command line and command-line arguments topics, check out Wikipedia’s
“Command-line interface” entry (http://en.wikipedia.org/wiki/Command-line_interface).
The following command line shows you how to use javac to compile a source file named App.java:

javac App.java


The .java file extension is mandatory. The compiler complains when you omit this extension.
Tip  You can compile multiple source files by specifying an asterisk in place of the filename, as follows:
javac *.java
Assuming success, an App.class file is created. If this file describes an application, which minimally
consists of a single class containing a method named main, you can run the application as follows:

java App


You must not specify the .class file extension. The java tool complains when .class is specified.
In addition to downloading and installing the JDK, you’ll need to access the JDK documentation,
especially to explore the Java APIs. There are two sets of documentation that you can explore.

www.it-ebooks.info


CHAPTER 1: Getting Started with Java

 Oracle’s JDK 7 documentation (http://docs.oracle.com/javase/7/docs/api/
index.html)
 Google’s Java Android API documentation (https://developer.android.com/
reference/packages.html)
Oracle’s JDK 7 documentation presents many APIs that are not supported by Android. Furthermore,
it doesn’t cover APIs that are specific to Android. This book focuses only on core Oracle Java APIs
that are also covered in Google’s documentation.

Hello, World!
It’s customary to start exploring a new language and its tools by writing, compiling, and running a
simple application that outputs the “Hello, World!” message. This practice dates back to Brian
Kernighan’s and Dennis Ritchie’s seminal book, The C Programming Language.
Listing 1-1 presents the source code to a HelloWorld application that outputs this message.
Listing 1-1.  Saying Hello in a Java Language Context
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello, World!");
}
}


This short seven-line application has a lot to say about the Java language. I’ll briefly explain each
feature, leaving comprehensive discussions of these features to later chapters.
This source code declares a class, which you can think of as a container for describing an
application. The first line, public class HelloWorld, introduces the name of the class (HelloWorld),
which is preceded by reserved words (names that have meaning to the Java compiler and which
you cannot use to name other things in your programs) public and class. These reserved words
respectively tell the compiler that HelloWorld must be stored in a file named HelloWorld and that a
class is being declared.
The rest of the class declaration appears between a pair of brace characters ({}), which are familiar
to C and C++ developers. Between these characters is the declaration of a single method, which
you can think of as a named sequence of code. This method is named main to signify that it's the
entry point into the application, and it is the analog of the main() function in C and C++.

www.it-ebooks.info

9


10

CHAPTER 1: Getting Started with Java

The main() method includes a header that identifies this method and a block of code located
between an open brace character ({) and a close brace character (}). Besides naming this method,
the header provides the following information:
public: This reserved word makes main() visible to the startup code that calls
this method. If public wasn’t present, the compiler would output an error
message stating that it couldn’t find a main() method.
static: This reserved word causes this method to associate with the class
instead of associating with any objects (discussed in Chapter 3) created from
this class. Because the startup code that calls main() doesn’t create an object
from the class to call this method, it requires that the method be declared
static. Although the compiler will not report an error when static is missing,
it will not be possible to run HelloWorld, which will not be an application when
the proper main() method doesn’t exist.
void: This reserved word indicates that the method doesn’t return a value. If you
change void to a type’s reserved word (such as int) and then insert code that
returns a value of this type (such as return 0;), the compiler will not report an
error. However, you won’t be able to run HelloWorld because the proper main()
method wouldn’t exist. (I discuss types in Chapter 2.)
(String[] args): This parameter list consists of a single parameter named
args, which is of type String[]. Startup code passes a sequence of commandline arguments to args, which makes these arguments available to the code
that executes within main(). You’ll learn about parameters and arguments in
Chapter 3.
main() is called with an array of strings (character sequences delimited by double quote "
characters) 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 Chapters 2 and
3.) Although the array variable is named args, there’s nothing special about this name. You could
choose another name for this variable.
main() presents a single line of code, System.out.println("Hello, World!");, which is responsible
for outputting Hello, World! in the command window from where HelloWorld is run. From left to
right, this method call accomplishes the following tasks:
System identifies a standard class of system utilities.
out identifies an object variable located in System whose methods let you output
values of various types optionally followed by a newline (also known as line feed)
character to the standard output stream. (In reality, a platform-dependent line
terminator sequence is output. On Windows platforms, this sequence consists
of a carriage return character [integer value 13] followed by a line feed character
[integer value 10]. On Linux platforms, this sequence consists of a line feed
character. On Mac OS X systems, this sequence consists of a carriage return
character. It's convenient to refer to this sequence as a newline.)
println identifies a method that prints its "Hello, World!" argument (the
starting and ending double quote characters are not written; these characters
delimit but are not part of the string) followed by a newline to the standard
output stream.
www.it-ebooks.info


CHAPTER 1: Getting Started with Java

11

Note  The standard output stream is part of Standard I/O (http://en.wikipedia.org/wiki/
Standard_streams), which also consists of standard input and standard error streams, and which
originated with the Unix operating system. Standard I/O 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 stream, which defaults to the keyboard but can be redirected to a file.
Text is written to the standard output stream, which defaults to the screen but can be redirected to a file.
Error message text is written to the standard error stream, which defaults to the screen but can be redirected
to a file that differs from the standard output file.

Assuming that you’re familiar with your platform’s command-line interface and are at the command
line, make HelloWorld your current directory and copy Listing 1-1 to a file named HelloWorld.java.
Then compile this source file via the following command line:

javac HelloWorld.java


Assuming that you’ve included the .java extension, which is required by javac, and that
HelloWorld.java compiles, you should discover a file named HelloWorld.class in the current
directory. Run this application via the following command line:

java HelloWorld


If all goes well, you should see the following line of output on the screen:

Hello, World!


You can redirect this output to a file by specifying the greater than angle bracket (>) followed by a
filename. For example, the following command line stores the output in a file named hello.txt:

java HelloWorld >hello.txt

DumpArgs
In the previous example, I pointed out main()’s (String[] args) parameter list, which consists
of a single parameter named args. This parameter stores an array (think sequence of values) of
arguments passed to the application on the command line. Listing 1-2 presents the source code to a
DumpArgs application that outputs each argument.
Listing 1-2.  Dumping Command-Line Arguments Stored in main()’s args Array to the Standard Output Stream
public class DumpArgs
{
public static void main(String[] args)
{
System.out.println("Passed arguments:");
for (int i = 0; i < args.length; i++)

www.it-ebooks.info


12

CHAPTER 1: Getting Started with Java

System.out.println(args[i]);
}
}


Listing 1-2’s DumpArgs application consists of a class named DumpArgs that’s very similar to Listing
1-1’s HelloWorld class. The essential difference between these classes is the for loop (a construct
for repeated execution and starting with reserved word for) that accesses each array item and
dumps it to the standard output stream.
The for loop first initializes integer variable i to 0. This variable keeps track of how far the loop has
progressed (the loop must end at some point), and it also identifies one of the entries in the args
array. Next, i is compared with args.length, which records the number of entries in the array.
The loop ends when i’s value equals the value of args.length. (I discuss .length in Chapter 2.)
Each loop iteration executes System.out.println(args[i]);. The string stored in the ith entry of the args
array is accessed and then output to the standard output stream—the first entry is located at index
(location) 0. The last entry is stored at index args.length - 1. Finally, i is incremented by 1 via i++,
and i < args.length is reevaluated to determine whether the loop continues or ends.
Assuming that you’re familiar with your platform’s command-line interface and that you are at the
command line, make DumpArgs your current directory and copy Listing 1-2 to a file named DumpArgs
.java. Then compile this source file via the following command line:

javac DumpArgs.java


Assuming that that you’ve 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’ll 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


www.it-ebooks.info


CHAPTER 1: Getting Started with Java

13

You can redirect this output to a file. For example, the following command line stores the DumpArgs
application’s output in a file named out.txt:

java DumpArgs Curly Moe Larry >out.txt

EchoText
The previous two examples introduced you to a few Java language features, and they also showed
outputting text to the standard output stream, which defaults to the screen but can be redirected
to a file. In the final example (see Listing 1-3), I introduce more language features and demonstrate
inputting text from the standard input stream and outputting text to the standard error stream.
Listing 1-3.  Echoing Text Read from Standard Input to Standard Output
public class EchoText
{
public static void main(String[] args)
{
boolean isRedirect = false;
if (args.length != 0)
isRedirect = true;
int ch;
try
{
while ((ch = System.in.read()) != ((isRedirect) ? -1 : '\n'))
System.out.print((char) ch);
}
catch (java.io.IOException ioe)
{
System.err.println("I/O error");
}
System.out.println();
}
}


EchoText is a more complex application than HelloWorld or DumpArgs. Its main() method first
declares a Boolean (true/false) variable named isRedirect that tells this application whether input
originates from the keyboard (isRedirect is false) or a file (isRedirect is true). The application
defaults to assuming that input originates from the keyboard.
There’s no easy way to determine if standard input has been redirected, and so the application
requires that the user tell it if this is the case by specifying one or more command-line arguments.
The if decision (a construct for making decisions and starting with reserved word if) evaluates args.
length != 0, assigning true to isRedirect when this Boolean expression evaluates to true (at least
one command-line argument has been specified).
main() now introduces the int variable ch to store the integer representation of each character read
from standard input. (You’ll learn about int and integer in Chapter 2.) It then enters a sequence of
code prefixed by the reserved word try and surrounded by brace characters. Code within this block
may throw an exception (an object describing a problem) and the subsequent catch block will handle
it (to address the problem). (I discuss exceptions in Chapter 5.)
www.it-ebooks.info


14

CHAPTER 1: Getting Started with Java

The try block consists of a while loop (a construct for repeated execution and starting with the
reserved word while) that reads and echoes 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 -1 (no
more input data is available from a file; standard input was redirected) or '\n' (the newline/line
feed character has been read, which is the case when standard input wasn’t redirected.) '\n' is an
example of a character literal, which is discussed in Chapter 2.
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.
print(), which doesn’t also terminate the current line by outputting a newline. The final System.out.
println(); call terminates the current line without outputting any content.
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 before the read
operation), System.in.read() fails by throwing a java.io.IOException object that describes this
problem. The code within the catch block is then executed, which outputs an I/O error message to
the standard error stream via System.err.println("I/O error");.

Note  System.err provides the same families of println() and print() methods as System.out. 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.

Compile Listing 1-3 via the following command line:

javac EchoText.java


Now run the application via the following command line:

java EchoText


You should see a flashing cursor. Type the following text and press Enter:

This is a test.


You should see this text duplicated on the following line and the application should end.
Continue by redirecting the input source to a file, by specifying the less than angle bracket (<)
followed by a filename:

java EchoText

Although it looks like there are two command-line arguments, there is only one: x. (Redirection
symbols followed by filenames don’t count as command-line arguments.) You should observe the
contents of EchoText.java listed on the screen.
Finally, execute the following command line:

java EchoText

www.it-ebooks.info


CHAPTER 1: Getting Started with Java

15

This time, x isn’t specified, so input is assumed to originate from the keyboard. However, because
the input is actually coming from the file EchoText.java, and because each line is terminated with a
newline, only the first line from this file will be output.
Note  If I had shortened the while loop expression to while ((ch = System.in.read()) != -1)
and didn’t redirect standard input to a file, the loop wouldn’t end because -1 would never be seen. To exit this
loop, you would have to press the Ctrl and C keys simultaneously on a Windows platform or the equivalent
keys on a non-Windows platform.

Installing and Exploring the Eclipse IDE
Working with the JDK’s tools at the command line is probably okay for small projects. However, this
practice isn’t recommended for large projects, which are hard to manage without the help of an 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. Eclipse is a popular IDE that
Google supports for developing Android apps.

Note  For convenience, I use JDK tools throughout this book, except for this section where I discuss and
demonstrate the Eclipse IDE.

Eclipse IDE is an open source IDE for developing programs in Java and other languages (such as C,
COBOL, PHP, Perl, and Python). Eclipse Standard is one distribution of this IDE that’s available for
download; version 4.3.1 is the current version at time of this writing.
You should download and install Eclipse Standard to follow along with this section’s Eclipse-oriented
example. Begin by pointing your browser to www.eclipse.org/downloads/ and completing the
following tasks.
1. Scroll down the page until you see an Eclipse Standard entry. (It may refer to
4.3.1 or a newer version.)
2. Click one of the platform links (such as Windows 64 Bit) to the right
of this entry.
3. Select a download mirror from the subsequently displayed page, and
proceed to download the distribution’s archive file.
I downloaded the eclipse-standard-kepler-SR1-win32-x86_64.zip archive file for my Windows 7
platform, unarchived this file, moved the resulting eclipse home directory to another location, and
created a shortcut to that directory’s eclipse.exe file.
After installing Eclipse Classic, run this application. You should discover a splash screen identifying
this IDE and a dialog box that lets you choose the location of a workspace for storing projects
followed by a main window like the one shown in Figure 1-4.
www.it-ebooks.info


16

CHAPTER 1: Getting Started with Java

Figure 1-4.  Keep the default workspace or choose another workspace

Click the OK button, and you’re taken to Eclipse’s main window. See Figure 1-5.

Figure 1-5.  The main window initially presents a Welcome tab

www.it-ebooks.info


CHAPTER 1: Getting Started with Java

17

The main window initially presents a Welcome tab from which you can learn more about Eclipse.
Click this tab’s X icon to close this tab; you can restore the Welcome tab by selecting Welcome from
the menu bar’s Help menu.
The Eclipse user interface is based on a main window that consists of a menu bar, a tool bar, a
workbench area, and a status bar. The workbench presents windows for organizing Eclipse projects,
editing source files, viewing messages, and more.
To help you get comfortable with the Eclipse user interface, I’ll show you how to create a DumpArgs
project containing a single DumpArgs.java source file with Listing 1-2’s source code. You’ll also learn
how to compile and run this application.
Complete the following steps to create the DumpArgs project.
1. Select New from the File menu and Java Project from the resulting pop-up
menu.
2. In the resulting New Java Project dialog box, enter DumpArgs into the Project
name text field. Keep all of the other defaults, and click the Finish button.
After the second step (and after closing the Welcome tab), you’ll see a workbench similar to the one
shown in Figure 1-6.

Figure 1-6.  A DumpArgs entry appears in the workbench’s Package Explorer

www.it-ebooks.info


Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay

×