Beginning Java 7 [Friesen 2011-11-22].pdf

(8581 KB) Pobierz
813477121.031.png 813477121.041.png 813477121.051.png 813477121.052.png 813477121.001.png
 
 
 
 
 
813477121.002.png 813477121.003.png 813477121.004.png
 
 
 
 
 
 
 
 
813477121.005.png 813477121.006.png 813477121.007.png 813477121.008.png 813477121.009.png 813477121.010.png 813477121.011.png 813477121.012.png 813477121.013.png 813477121.014.png 813477121.015.png 813477121.016.png 813477121.017.png 813477121.018.png 813477121.019.png 813477121.020.png 813477121.021.png 813477121.022.png 813477121.023.png 813477121.024.png 813477121.025.png 813477121.026.png 813477121.027.png 813477121.028.png 813477121.029.png 813477121.030.png
 
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.
813477121.032.png 813477121.033.png 813477121.034.png 813477121.035.png 813477121.036.png 813477121.037.png 813477121.038.png 813477121.039.png
Contents at a Glance
About the Author.................................................................................................. xiv
About the Technical Reviewer.............................................................................. xv
Acknowledgments............................................................................................... xvi
Introduction........................................................................................................ xvii
Chapter 1: Getting Started with Java...................................................................... 1
Chapter 2: Discovering Classes and Objects........................................................ 51
Chapter 3: Exploring Advanced Language Features........................................... 131
Chapter 4: Touring Language APIs..................................................................... 227
Chapter 5: Collecting Objects.............................................................................. 319
Chapter 6: Touring Additional Utility APIs.......................................................... 401
Chapter 7: Creating and Enriching Graphical User Interfaces............................ 435
Chapter 8: Interacting with Filesystems............................................................. 511
Chapter 9: Interacting with Networks and Databases........................................ 585
Chapter 10: Parsing, Creating, and Transforming XML Documents................... 663
Chapter 11: Working with Web Services............................................................ 751
Chapter 12: Java 7 Meets Android..................................................................... 831
Index................................................................................................................... 873
iii
813477121.040.png 813477121.042.png 813477121.043.png 813477121.044.png 813477121.045.png
C H A P T E R 1
Getting Started with Java
Welcome to Java. This chapter launches you on a tour of this technology by focusing on fundamentals.
First, you receive an answer to the “What is Java?” question. If you have not previously encountered Java,
the answer might surprise you. Next, you are introduced to some basic tools that will help you start
developing Java programs, and to the NetBeans integrated development environment, which simplifies
the development of these programs. Finally, you explore fundamental language features.
What Is Java?
Java is a language for describing programs, and Java is a platform on which to run programs written in
Java and other languages (e.g., Groovy, Jython, and JRuby). This section introduces you to Java the
language and Java the platform.
Note To discover Java’s history, check out Wikipedia’s “Java (programming language)”
( http://en.wikipedia.org/wiki/Java_(pro gramming_language)#History ) and “Java (software platform)”
( http://en.wikipedia.org/wiki/Java_(sof tware_platform)#History ) entries.
Java Is a Language
Java is a general-purpose, class-based, and object-oriented language patterned after C and C++ to make
it easier for existing C/C++ developers to migrate to this language. Not surprisingly, Java borrows
elements from these languages. The following list identifies some of these elements:
Java supports the same single-line and multiline comment styles as found in
C/C++ for documenting source code.
Java provides the if , switch , while , for , and other reserved words as found in the
C and C++ languages. Java also provides the try , catch , class , private , and other
reserved words that are found in C++ but not in C.
As with C and C++, Java supports character, integer, and other primitive types.
Furthermore, Java shares the same reserved words for naming these types; for
example, char (for character) and int (for integer).
1
813477121.046.png 813477121.047.png
 
CHAPTER 1 GETTING STARTED WITH JAVA
Java supports many of the same operators as C/C++: the arithmetic operators ( + , - ,
* , / , and % ) and conditional operator ( ?: ) are examples.
Java also supports the use of brace characters { and } to delimit blocks of
statements.
Although Java is similar to C and C++, it also differs in many respects. The following list itemizes
some of these differences:
Java supports an additional comment style known as Javadoc.
Java provides transient , synchronized , strictfp , and other reserved words not
found in C or C++.
Java’s character type has a larger size than the version of this type found in C and
C++, Java’s integer types do not include unsigned variants of these types (Java has
no equivalent of the C/C++ unsigned long integer type, for example), and Java’s
primitive types have guaranteed sizes, whereas no guarantees are made for the
equivalent C/C++ types.
Java doesn’t support all of the C/C++ operators. For example, there is no sizeof
operator. Also, Java provides some operators not found in C/C++. For example,
>>> (unsigned right shift) and instanceof are exclusive to Java.
Java provides labeled break and continue statements. These variants of the C/C++
break and continue statements provide a safer alternative to C/C++’s goto
statement, which Java doesn’t support.
Note Comments, reserved words, types, operators, and statements are examples of fundamental language
features, which are discussed later in this chapter.
A Java program starts out as source code that conforms to Java syntax , rules for combining symbols
into meaningful entities. The Java compiler translates the source code stored in files that have the
.java ” file extension into equivalent executable code, known as bytecode , which it stores in files that
have the “ .class ” file extension.
Note The files that store compiled Java code are known as classfiles because they often store the runtime
representation of Java classes, a language feature discussed in Chapter 2.
The Java language was designed with portability in mind. Ideally, Java developers write a Java
program’s source code once, compile this source code into bytecode once, and run the bytecode on any
platform (e.g., Windows, Linux, and Mac OS X) where Java is supported, without ever having to change
the source code and recompile. Portability is achieved in part by ensuring that primitive types have the
same sizes across platforms. For example, the size of Java’s integer type is always 32 bits.
2
813477121.048.png 813477121.049.png 813477121.050.png
 
Zgłoś jeśli naruszono regulamin