Thinking in Java - 4th Edition
1079 pág.

Thinking in Java - 4th Edition


DisciplinaProgramação Orientada A Objetos4.277 materiais60.876 seguidores
Pré-visualização50 páginas
began using Enterprise Architect from Sparxsystems on this book, and it has rapidly 
become my UML tool of choice. Marco Hunsicker\u2019s Jalopy code formatter 
(www.triemax.com) came in handy on numerous occasions, and Marco was very helpful in 
Preface 5 
configuring it to my particular needs. I\u2019ve also found Slava Pestov\u2019s JEdit and plug-ins to be 
helpful at times (www.jedit.org) and it\u2019s quite a reasonable beginner\u2019s editor for seminars. 
And of course, if I don\u2019t say it enough everywhere else, I use Python (www.Python.org) 
constantly to solve problems, the brainchild of my buddy Guido Van Rossum and the gang of 
goofy geniuses with whom I spent a few great days sprinting (Tim Peters, I\u2019ve now framed 
that mouse you borrowed, officially named the \u201cTimBotMouse\u201d). You guys need to find 
healthier places to eat lunch. (Also, thanks to the entire Python community, an amazing 
bunch of people.) 
Lots of people sent in corrections and I am indebted to them all, but particular thanks go to 
(for the 1st edition): Kevin Raulerson (found tons of great bugs), Bob Resendes (simply 
incredible), John Pinto, Joe Dante, Joe Sharp (all three were fabulous), David Combs (many 
grammar and clarification corrections), Dr. Robert Stephenson, John Cook, Franklin Chen, 
Zev Griner, David Karr, Leander A. Stroschein, Steve Clark, Charles A. Lee, Austin Maher, 
Dennis P. Roth, Roque Oliveira, Douglas Dunn, Dejan Ristic, Neil Galarneau, David B. 
Malkovsky, Steve Wilkinson, and a host of others. Prof. Ir. Marc Meurrens put in a great deal 
of effort to publicize and make the electronic version of the 1st edition of the book available in 
Europe. 
Thanks to those who helped me rewrite the examples to use the Swing library (for the 2nd 
edition), and for other assistance: Jon Shvarts, Thomas Kirsch, Rahim Adatia, Rajesh Jain, 
Ravi Manthena, Banu Rajamani, Jens Brandt, Nitin Shivaram, Malcolm Davis, and everyone 
who expressed support. 
In the 4th edition, Chris Grindstaff was very helpful during the development of the SWT 
section, and Sean Neville wrote the first draft of the Flex section for me. 
Kraig Brockschmidt and Gen Kiyooka have been some of the smart technical people in my 
life who have become friends and have also been both influential and unusual in that they do 
yoga and practice other forms of spiritual enhancement, which I find quite inspirational and 
instructional. 
It\u2019s not that much of a surprise to me that understanding Delphi helped me understand Java, 
since there are many concepts and language design decisions in common. My Delphi friends 
provided assistance by helping me gain insight into that marvelous programming 
environment. They are Marco Cantu (another Italian\u2014perhaps being steeped in Latin gives 
one aptitude for programming languages?), Neil Rubenking (who used to do the 
yoga/vegetarian/Zen thing until he discovered computers), and of course Zack Urlocker (the 
original Delphi product manager), a long-time pal whom I\u2019ve traveled the world with. We\u2019re 
all indebted to the brilliance of Anders Hejlsberg, who continues to toil away at C# (which, as 
you\u2019ll learn in this book, was a major inspiration for Java SE5). 
My friend Richard Hale Shaw\u2019s insights and support have been very helpful (and Kim\u2019s, too). 
Richard and I spent many months giving seminars together and trying to work out the 
perfect learning experience for the attendees. 
The book design, cover design, and cover photo were created by my friend Daniel Will-
Harris, noted author and designer (www.Will-Harris.com), who used to play with rub-on 
letters in 
junior high school while he awaited the invention of computers and desktop publishing, and 
complained of me mumbling over my algebra problems. However, I produced the camera-
ready pages myself, so the typesetting errors are mine. Microsoft® Word XP for Windows 
was used to write the book and to create camera-ready pages in Adobe Acrobat; the book was 
created directly from the Acrobat PDF files. As a tribute to the electronic age, I happened to 
be overseas when I produced the final versions of the 1st and 2nd editions of the book\u2014the 1st 
edition was sent from Cape Town, South Africa, and the 2nd edition was posted from Prague. 
6 Thinking in Java Bruce Eckel 
Preface 7 
The 3rd and 4th came from Crested Butte, Colorado. The body typeface is Georgia and the 
headlines are in Verdana. The cover typeface is ITC Rennie Mackintosh. 
A special thanks to all my teachers and all my students (who are my teachers as well). 
Molly the cat often sat in my lap while I worked on this edition, and thus offered her own 
kind of warm, furry support. 
The supporting cast of friends includes, but is not limited to: Patty Gast (Masseuse 
extraordinaire), Andrew Binstock, Steve Sinofsky, JD Hildebrandt, Tom Keffer, Brian 
McElhinney, Brinkley Barr, Bill Gates at Midnight Engineering Magazine, Larry Constantine 
and Lucy Lockwood, Gene Wang, Dave Mayer, David Intersimone, Chris and Laura Strand, 
the Almquists, Brad Jerbic, Marilyn Cvitanic, Mark Mabry, the Robbins families, the Moelter 
families (and the McMillans), Michael Wilk, Dave Stoner, the Cranstons, Larry Fogg, Mike 
Sequeira, Gary Entsminger, Kevin and Sonda Donovan, Joe Lordi, Dave and Brenda Bartlett, 
Patti Gast, Blake, Annette & Jade, the Rentschlers, the Sudeks, Dick, Patty, and Lee Eckel, 
Lynn and Todd, and their families. And of course, Mom and Dad. 
 
Introduction 
\u201cHe gave man speech, and speech created thought, Which is the 
measure of the Universe\u201d\u2014Prometheus Unbound, Shelley 
Human beings ... are very much at the mercy of the particular language which has 
become the medium of expression for their society. It is quite an illusion to imagine 
that one adjusts to reality essentially without the use of language and that language 
is merely an incidental means of solving specific problems of communication and 
reflection. The fact of the matter is that the \u201creal world\u201d is to a large extent 
unconsciously built up on the language habits of the group. 
The Status of Linguistics as a Science, 1929, Edward Sapir 
Like any human language, Java provides a way to express concepts. If successful, this 
medium of expression will be significantly easier and more flexible than the alternatives as 
problems grow larger and more complex. 
You can\u2019t look at Java as just a collection of features\u2014some of the features make no sense in 
isolation. You can use the sum of the parts only if you are thinking about design, not simply 
coding. And to understand Java in this way, you must understand the problems with the 
language and with programming in general. This book discusses programming problems, 
why they are problems, and the approach Java has taken to solve them. Thus, the set of 
features that I explain in each chapter are based on the way I see a particular type of problem 
being solved with the language. In this way I hope to move you, a little at a time, to the point 
where the Java mindset becomes your native tongue. 
Throughout, I\u2019ll be taking the attitude that you want to build a model in your head that allows 
you to develop a deep understanding of the language; if you encounter a puzzle, you\u2019ll feed it 
to your model and deduce the answer. 
Prerequisites 
This book assumes that you have some programming familiarity: You understand that a 
program is a collection of statements, the idea of a subroutine/function/macro, control 
statements such as \u201cif\u201d and looping constructs such as \u201cwhile,\u201d etc. However, you might have 
learned this in many places, such as programming with a macro language or working with a 
tool like Perl. As long as you\u2019ve programmed to the point where you feel comfortable with the 
basic ideas of programming, you\u2019ll be able to work through this book. Of course, the book will 
be easier for C programmers and more so for C++ programmers, but don\u2019t count yourself out 
if you\u2019re not experienced with those languages\u2014however, come willing to work hard. Also, 
the