[JAVA][Beginning Java 8 Language Features].pdf

(6658 KB) Pobierz
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 Reviewers �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
xxiii
Acknowledgments �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
xxv
Foreword �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
xxvii
Introduction �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
xxix
Chapter 1: Annotations �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½1
Chapter 2: Inner Classes �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½41
Chapter 3: Reflection �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½75
Chapter 4: Generics �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½103
Chapter 5: Lambda Expressions �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½123
Chapter 6: Threads �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½173
Chapter 7: Input/Output �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½281
Chapter 8: Working with Archive Files �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½359
Chapter 9: New Input/Output �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½389
Chapter 10: New Input/Output 2 �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½423
Chapter 11: Garbage Collection �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½485
Chapter 12: Collections �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½519
Chapter 13: Streams �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½597
Index �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½659
v
www.it-ebooks.info
Introduction
How This Book Came About
My first encounter with the Java programming language was during a one-week Java training session in 1997. I did
not get a chance to use Java in a project until 1999. I read two Java books and took a Java 2 Programmer certification
examination. I did very well on the test, scoring 95 percent. The three questions that I missed on the test made me
realize that the books I read did not adequately cover all of the details on all of the necessary Java topics. I made up
my mind to write a book on the Java programming language. So, I formulated a plan to cover most of the topics that
a Java developer needs understand to use the Java programming language effectively in a project, as well as to get a
certification. I initially planned to cover all essential topics in Java in 700 to 800 pages.
As I progressed, I realized that a book covering most of the Java topics in detail could not be written in 700 to 800
hundred pages. One chapter that covered data types, operators, and statements spanned 90 pages. I was then faced
with the question, “Should I shorten the content of the book or include all the details that I think a Java developer
needs?” I opted for including all the details in the book, rather than shortening the content to keep the number of
pages low. It has never been my intent to make lots of money from this book. I was never in a hurry to finish this book
because that rush could have compromised the quality and the coverage of its contents. In short, I wrote this book
to help the Java community understand and use the Java programming language effectively, without having to read
many books on the same subject. I wrote this book with the plan that it would be a comprehensive one-stop reference
for everyone who wants to learn and grasp the intricacies of the Java programming language.
One of my high school teachers used to tell us that if one wanted to understand a building, one must first
understand the bricks, steel, and mortar that make up the building. The same logic applies to most of the things that
we want to understand in our lives. It certainly applies to an understanding of the Java programming language. If you
want to master the Java programming language, you must start by understanding its basic building blocks. I have used
this approach throughout this book, endeavoring to build each topic by describing the basics first. In the book, you
will rarely find a topic described without first learning its background. Wherever possible, I have tried to correlate the
programming practices with activities in our daily life. Most books about the Java programming language either do not
include any pictures at all or have only a few. I believe in the adage, “A picture is worth a thousand words.” To a reader,
a picture makes a topic easier to understand and remember. I have included plenty of illustrations in this book to aid
readers in understanding and visualizing the contents. Developers who have little or no programming experience can
have difficulty putting things together to make a complete program. Keeping them in mind, the book contains over
290 complete Java programs that are ready to be compiled and run.
I spent countless hours doing research for writing this book. My main sources of research were the Java Language
Specification, white papers and articles on Java topics, and Java Specification Requests (JSRs). I also spent quite a bit
of time reading the Java source code to learn more about some of the Java topics. Sometimes it took a few months to
research a topic before I could write the first sentence on it. It was always fun to play with Java programs, sometimes
for hours, to add them to the book.
xxix
www.it-ebooks.info
IntroduCtIon
Structure of the Book
This is the second book in the three-book Beginning Java series. This book contains 13 chapters. The chapters contain
language-level topics of Java such as annotations, generics, lambda expressions, threads, I/O, collections, streams, etc.
Chapters introduce Java topics in an increasing order of complexity. The new features of Java 8 are included wherever
they fit in the chapter. The lambda expressions and Streams API, which were added in Java 8, are covered in depth.
After finishing this book, take your Java knowledge to the next level by learning the Java APIs, extensions, and
libraries; all of this is covered in the last book in this series,
Beginning Java 8 APIs, Extensions, and Libraries
(ISBN 978-1-4302-6661-7).
Audience
This book is designed to be useful for anyone who wants to learn the Java programming language. If you are a
beginner, with little or no programming background in Java, you are advised to read the companion book
Beginning
Java 8 Fundamentals
before reading this book. This book contains topics of various degrees of complexity. As a
beginner, if you find yourself overwhelmed while reading a section in a chapter, you can skip to the next section or the
next chapter, and revisit it later when you gain more experience.
If you are a Java developer with an intermediate or advanced level of experience, you can jump to a chapter or to
a section in a chapter directly. If a section uses an unfamiliar topic, you need to visit that topic before continuing the
current one.
If you are reading this book to get a certification in the Java programming language, you need to read almost all of
the chapters, paying attention to all of the detailed descriptions and rules. Most of the certification programs test your
fundamental knowledge of the language, not the advanced knowledge. You need to read only those topics that are
part of your certification test. Compiling and running over 290 complete Java programs will help you prepare for your
certification.
If you are a student who is attending a class in the Java programming language, you should read the chapters
of this book selectively. Some topics such as lambda expressions, collections, and streams are used extensively in
developing Java applications, whereas some topics such as threads and archive files are infrequently used. You need
to read only those chapters that are covered in your class syllabus. I am sure that you, as a Java student, do not need to
read the entire book page by page.
How to Use This Book
This book is the beginning, not the end, of gaining the knowledge of the Java programming language. If you are reading
this book, it means you are heading in the right direction to learn the Java programming language, which will enable
you to excel in your academic and professional career. However, there is always a higher goal for you to achieve and you
must constantly work hard to achieve it. The following quotations from some great thinkers may help you understand
the importance of working hard and constantly looking for knowledge with both your eyes and mind open.
The learning and knowledge that we have, is, at the most, but little compared with that of which
we are ignorant.
—Plato
True knowledge exists in knowing that you know nothing. And in knowing that you know nothing,
that makes you the smartest of all.
—Socrates
xxx
www.it-ebooks.info
Zgłoś jeśli naruszono regulamin