How to Use This Book: Intro

image

Who is this book for?

If you can answer “yes” to all of these:

  • Images Have you done some programming?

  • Images Do you want to learn Java?

  • Images Do you prefer stimulating dinner party conversation to dry, dull, technical lectures?

this book is for you.

Note

This is NOT a reference book. Head First Java is a book designed for learning, not an encyclopedia of Java facts.

Who should probably back away from this book?

If you can answer “yes” to any one of these:

  • Images Is your programming background limited to HTML only, with no scripting language experience?

    (If you’ve done anything with looping or if/then logic, you’ll do fine with this book, but HTML tagging alone might not be enough.)

  • Images Are you a kick-butt C++ programmer looking for a reference book?

  • Images Are you afraid to try something different? Would you rather have a root canal than mix stripes with plaid? Do you believe that a technical book can’t be serious if there’s a picture of a duck in the memory management section?

this book is not for you.

image
Note

[note from marketing: who took out the part about how this book is for anyone with a valid credit card? And what about that “Give the Gift of Java” holiday promotion we discussed... -Fred]

We know what you’re thinking

“How can this be a serious Java programming book?”

“What’s with all the graphics?”

“Can I actually learn it this way?”

“Do I smell pizza?”

Images

And we know what your brain is thinking

Images

Your brain craves novelty. It’s always searching, scanning, waiting for something unusual. It was built that way, and it helps you stay alive.

Today, you’re less likely to be a tiger snack. But your brain’s still looking. You just never know.

So what does your brain do with all the routine, ordinary, normal things you encounter? Everything it can to stop them from interfering with the brain’s real job—recording things that matter. It doesn’t bother saving the boring things; they never make it past the “this is obviously not important” filter.

How does your brain know what’s important? Suppose you’re out for a day hike and a tiger jumps in front of you, what happens inside your head?

Neurons fire. Emotions crank up. Chemicals surge .

And that’s how your brain knows...

This must be important! Don’t forget it!

But imagine you’re at home, or in a library. It’s a safe, warm, tiger-free zone. You’re studying. Getting ready for an exam. Or trying to learn some tough technical topic your boss thinks will take a week, ten days at the most.

Just one problem. Your brain’s trying to do you a big favor. It’s trying to make sure that this obviously non-important content doesn’t clutter up scarce resources. Resources that are better spent storing the really big things. Like tigers. Like the danger of fire. Like how you should never again snowboard in shorts.

And there’s no simple way to tell your brain, “Hey, brain, thank you very much, but no matter how dull this book is, and how little I’m registering on the emotional richter scale right now, I really do want you to keep this stuff around.”

Images

Metacognition: thinking about thinking

Images

If you really want to learn, and you want to learn more quickly and more deeply, pay attention to how you pay attention. Think about how you think. Learn how you learn.

Most of us did not take courses on metacognition or learning theory when we were growing up. We were expected to learn, but rarely taught to learn.

But we assume that if you’re holding this book, you want to learn Java. And you probably don’t want to spend a lot of time.

To get the most from this book, or any book or learning experience, take responsibility for your brain. Your brain on that content.

The trick is to get your brain to see the new material you’re learning as Really Important. Crucial to your well-being. As important as a tiger. Otherwise, you’re in for a constant battle, with your brain doing its best to keep the new content from sticking.

So just how DO you get your brain to treat Java like it was a hungry tiger?

There’s the slow, tedious way, or the faster, more effective way. The slow way is about sheer repetition. You obviously know that you are able to learn and remember even the dullest of topics, if you keep pounding on the same thing. With enough repetition, your brain says, “This doesn’t feel important to him, but he keeps looking at the same thing over and over and over , so I suppose it must be.”

The faster way is to do anything that increases brain activity, especially different types of brain activity. The things on the previous page are a big part of the solution, and they’re all things that have been proven to help your brain work in your favor. For example, studies show that putting words within the pictures they describe (as opposed to somewhere else in the page, like a caption or in the body text) causes your brain to try to makes sense of how the words and picture relate, and this causes more neurons to fire. More neurons firing = more chances for your brain to get that this is something worth paying attention to, and possibly recording.

A conversational style helps because people tend to pay more attention when they perceive that they’re in a conversation, since they’re expected to follow along and hold up their end. The amazing thing is, your brain doesn’t necessarily care that the “conversation” is between you and a book! On the other hand, if the writing style is formal and dry, your brain perceives it the same way you experience being lectured to while sitting in a roomful of passive attendees. No need to stay awake.

But pictures and conversational style are just the beginning.

Here’s what WE did:

We used pictures, because your brain is tuned for visuals, not text. As far as your brain’s concerned, a picture really is worth 1024 words. And when text and pictures work together, we embedded the text in the pictures because your brain works more effectively when the text is within the thing the text refers to, as opposed to in a caption or buried in the text somewhere.

Images

We used repetition, saying the same thing in different ways and with different media types, and multiple senses , to increase the chance that the content gets coded into more than one area of your brain.

Images

We used concepts and pictures in unexpected ways because your brain is tuned for novelty, and we used pictures and ideas with at least some emotional content, because your brain is tuned to pay attention to the biochemistry of emotions. That which causes you to feel something is more likely to be remembered, even if that feeling is nothing more than a little humor, surprise, or interest.

Images

We used a personalized, conversational style, because your brain is tuned to pay more attention when it believes you’re in a conversation than if it thinks you’re passively listening to a presentation. Your brain does this even when you’re reading .

We included more than 50 exercises because your brain is tuned to learn and remember more when you do things than when you read about things. And we made the exercises challenging-yet-do-able, because that’s what most people prefer.

Images

We used multiple learning styles, because you might prefer step-by-step procedures, while someone else wants to understand the big picture first, while someone else just wants to see a code example. But regardless of your own learning preference, everyone benefits from seeing the same content represented in multiple ways.

Images
Images

We include content for both sides of your brain, because the more of your brain you engage, the more likely you are to learn and remember, and the longer you can stay focused. Since working one side of the brain often means giving the other side a chance to rest, you can be more productive at learning for a longer period of time.

And we included stories and exercises that present more than one point of view, because your brain is tuned to learn more deeply when it’s forced to make evaluations and judgments.

We included challenges, with exercises, and by asking questions that don’t always have a straight answer, because your brain is tuned to learn and remember when it has to work at something (just as you can’t get your body in shape by watching people at the gym). But we did our best to make sure that when you’re working hard, it’s on the right things. That you’re not spending one extra dendrite processing a hard-to-understand example, or parsing difficult, jargon-laden, or extremely terse text.

Images

We used an 80/20 approach. We assume that if you’re going for a PhD in Java, this won’t be your only book. So we don’t talk about everything . Just the stuff you’ll actually use .

Here’s what YOU can do to bend your brain into submission

image

So, we did our part. The rest is up to you. These tips are a starting point; Listen to your brain and figure out what works for you and what doesn’t. Try new things.

Note

Cut this out and stick it on your refrigerator.

  • Images Slow down. The more you understand, the less you have to memorize.

    Don’t just read. Stop and think. When the book asks you a question, don’t just skip to the answer. Imagine that someone really is asking the question. The more deeply you force your brain to think, the better chance you have of learning and remembering.

  • Images Do the exercises. Write your own notes.

    We put them in, but if we did them for you, that would be like having someone else do your workouts for you. And don’t just look at the exercises. Use a pencil. There’s plenty of evidence that physical activity while learning can increase the learning.

  • Images Read the “There are No Dumb Questions”

    That means all of them. They’re not optional sidebars—they’re part of the core content! Sometimes the questions are more useful than the answers.

  • Images Don’t do all your reading in one place.

    Stand up, stretch, move around, change chairs, change rooms. It’ll help your brain feel something, and it keeps your learning from being too connected to a particular place.

  • Images Make this the last thing you read before bed. Or at least the last challenging thing.

    Part of the learning (especially the transfer to long-term memory) happens after you put the book down. Your brain needs time on its own, to do more processing. If you put in something new during that processing time, some of what you just learned will be lost.

  • Images Drink water. Lots of it.

    Your brain works best in a nice bath of fluid. Dehydration (which can happen before you ever feel thirsty) decreases cognitive function.

  • Images Talk about it. Out loud.

    Speaking activates a different part of the brain. If you’re trying to understand something or increase your chance of remembering it later, say it out loud. Better still, try to explain it out loud to someone else. You’ll learn more quickly, and you might uncover ideas you hadn’t known were there when you were reading about it.

  • Images Listen to your brain.

    Pay attention to whether your brain is getting overloaded. If you find yourself starting to skim the surface or forget what you just read, it’s time for a break. Once you go past a certain point, you won’t learn faster by trying to shove more in, and you might even hurt the process.

  • Images Feel something!

    Your brain needs to know that this matters. Get involved with the stories. Make up your own captions for the photos. Groaning over a bad joke is still better than feeling nothing at all.

  • Images Type and run the code.

    Type and run the code examples. Then you can experiment with changing and improving the code (or breaking it, which is sometimes the best way to figure out what’s really happening). Most of the code, expecially long examples and Ready-Bake Code, are at https://oreil.ly/hfJava_3e_examples.

What you need for this book:

You do not need any other development tool, such as an Integrated Development Environment (IDE). We strongly recommend that you not use anything but a basic text editor until you complete this book. An IDE can protect you from some of the details that really matter, so you’re much better off learning from the command line and then, once you really understand what’s happening, move to a tool that automates some of the process.

Note

This book assumes you’re using Java 11 (with the exception of Appendix B). However, if you’re using Java 8, you will find most of the code still works.

If there’s discussion of a feature from a version of Java higher than Java 8, the required version will be mentioned.

The code from this book is available at https://oreil.ly/hfJava_3e_examples.

Last-minute things you need to know:

This is a learning experience, not a reference book. We deliberately stripped out everything that might get in the way of learning whatever it is we’re working on at that point in the book. And the first time through, you need to begin at the beginning, because the book makes assumptions about what you’ve already seen and learned.

Images

We use simple UML-like diagrams.

If we’d used pure UML, you’d be seeing something that looks like Java, but with syntax that’s just plain wrong. So we use a simplified version of UML that doesn’t conflict with Java syntax. If you don’t already know UML, you won’t have to worry about learning Java and UML at the same time.

We don’t worry about organizing and packaging your own code.

In this book, you can get on with the business of learning Java, without stressing over some of the organizational or administrative details of developing Java programs. You will , in the real world, need to know—and use—these details, but since building and deploying Java applications generally relies on third-party build tools like Maven and Gradle, we have assumed you’ll learn those tools separately.

Images

The end-of-chapter exercises are mandatory; puzzles are optional. Answers for both are at the end of each chapter.

One thing you need to know about the puzzles—they’re puzzles. As in logic puzzles, brain teasers, crossword puzzles, etc. The exercises are here to help you practice what you’ve learned, and you should do them all. The puzzles are a different story, and some of them are quite challenging in a puzzle way. These puzzles are meant for puzzlers , and you probably already know if you are one. If you’re not sure, we suggest you give some of them a try, but whatever happens, don’t be discouraged if you can’t solve a puzzle or if you simply can’t be bothered to take the time to work them out.

Images

The “Sharpen Your Pencil” exercises don’t all have answers.

Not printed in the book, anyway. For some of them, there is no right answer, and for the others, part of the learning experience for the Sharpen activities is for you to decide if and when your answers are right.

The code examples are as lean as possible.

It’s frustrating to wade through 200 lines of code looking for the two lines you need to understand. Most examples in this book are shown within the smallest possible context, so that the part you’re trying to learn is clear and simple. So don’t expect the code to be robust, or even complete. That’s your assignment for after you finish the book. The book examples are written specifically for learning and aren’t always fully functional.

Images

Technical Reviewers for the 3rd Edition

Marc Loy

Images

Marc started with Java training at Sun Microsystems in the early days (shout-out to HotJava!) and never looked back. He authored a number of early Java books and training courses, working with a wide variety of companies across the US, Europe, and Asia along the way. Most recently for O’Reilly, Marc authored Smaller C and co-authored the fifth edition of Learning Java. Currently in Ohio, Marc is a software developer and maker specializing in microcontrollers.

Abraham Marin-Perez

Images

Abraham is a Java programmer, consultant, author, and public speaker with more than ten years of experience in a variety of industries. Originally from Valencia, Spain, Abraham has built most of his career in London, UK, working with entities like JP Morgan or the United Kingdom’s Home Office, frequently in collaboration with Equal Experts. Thinking his experiences could be useful to others, Abraham became a Java news editor at InfoQ, authored Real-World Maintainable Software, and co-authored Continuous Delivery in Java. He also helps run the London Java Community. Always the learner, Abraham is pursuing a degree in physics.

Other people to acknowledge for the 3rd Edition

At O’Reilly:

Huge thanks to Zan McQuade and Nicole Taché for enabling us to finally get this edition out! Zan, thanks for connecting Trisha back up to the Head First world, and Nicole, fantastic work driving us to get this done. Thanks to Meghan Blanchette, who left O’Reilly a hundred years ago, but it was she who introduced Bert and Trisha back in 2014.

Images

Trisha would like to thank:

Helen Scott, for providing frequent feedback on the new topics covered. She consistently stopped me from going too deep or assuming too much knowledge, and is a true champion of the learner. I can’t wait to start working even more closely with her on our next project.

My team at JetBrains for their patience and encouragement: Dalia Abo Sheasha, for test-driving the lambdas and streams chapter, and Mala Gupta, for giving me exactly the information I needed about modern Java certifications. Extra special thanks to Hadi Hariri for all his support, always.

The Friday Pub Lunch informaticos , for tolerating lunchtime conversations on whatever aspect of Java I was trying to explain that day or week, and Alys, Jen, and Clare for helping me to figure out when to prioritize this book over family. Thanks to Holly Cummins for finding a last minute bug.

Evie and Amy for the suggestions on how to improve the ice cream examples for Java’s Optional type. Thank you both for being genuinely interested in my progress, and for the spontaneous high-fives when you heard I’d finished.

None of this would have been possible without Israel Boza Rodriguez. You put up with me derailing important conversations like “what should we have for dinner?” with questions like “do you think CountDownLatch is too niche to teach beginner developers?” Crucially, you helped me to create space and time to work on the book, and regularly reminded me why I wanted to take on the project in the first place.

Thank you to Bert and Kathy for bringing me on this journey. It was an honor to learn how to be a Head First author from the horse’s mouth, so to speak.

Bert and Kathy would like to thank:

Beth Robson and Eric Freeman, for their overall, ongoing, badass support of the Head First series. A special thanks to Beth for the many conversations we had discussing what new Java topics to teach and how to teach them.

Paul Wheaton and the amazing moderators at CodeRanch.com (a.k.a. JavaRanch), for keeping CodeRanch a friendly place for Java beginners. A special thanks to Campbell Ritchie, Jeanne Boyarsky, Stephan van Hulst, Rob Spoor, Tim Cooke, Fred Rosenberger, and Frits Walraven for their invaluable input concerning what have been the truly important additions to Java since the 2nd edition.

Dave Gustafson, for teaching me so much about software development and rock climbing, AND for great discussions concerning the state of programming. Eric Normand, for teaching us a little FP, and helping us figure out how to slip a few of the best ideas from FP into an OO book. Simon Roberts, for his ongoing and passionate teaching of Java to students all over the world. Thanks to Heinz Kabutz and Venkat Subramaniam for helping us explore the nooks and crannies of Java Streams.

Laura Baldwin and Mike Loukides, for their tireless support of Head First for all these years. Ron Bilodeau and Kristen Brown, for their outstanding, always patient and friendly support.

Technical Editors for the 2nd Edition

Endless thanks to Jessica and Val for their hard work editing the 2nd edition.

Images

Jess works at Hewlett-Packard on the Self-Healing Services Team. She has a bachelor’s in computer engineering from Villanova University, has her SCJP 1.4 and SCWCD certifications, and is literally months away from receiving her master’s in software engineering at Drexel University (whew!).

When she’s not working, studying, or motoring in her MINI Cooper S, Jess can be found fighting her cat for yarn as she completes her latest knitting or crochet project (anybody want a hat?). She is originally from Salt Lake City, Utah (no, she’s not Mormon...yes, you were too going to ask) and is currently living near Philadelphia with her husband, Mendra, and two cats: Chai and Sake.

You can catch her moderating technical forums at javaranch.com .

Images

Valentin has a master’s degree in information and computer science from the Swiss Federal Institute of Technology in Lausanne (EPFL). He has worked as a software engineer with SRI International (Menlo Park, CA) and as a principal engineer in the Software Engineering Laboratory of EPFL.

Valentin is the cofounder and CTO of Condris Technologies, a company specializing in the development of software architecture solutions.

His research and development interests include aspect-oriented technologies, design and architectural patterns, web services, and software architecture. Besides taking care of his wife, gardening, reading, and doing some sport, Valentin moderates the SCBCD and SCDJWS forums at Javaranch.com. He holds the SCJP, SCJD, SCBCD, SCWCD, and SCDJWS certifications. He has also had the opportunity to serve as a co-author for Whizlabs SCBCD Exam Simulator.

(We’re still in shock from seeing him in a tie .)

Other people to credit, for the 2nd Edition

At O’Reilly:

Our biggest thanks to Mike Loukides at O’Reilly, for taking a chance on this, and helping to shape the Head First concept into a book (and series). As this second edition goes to print there are now five Head First books, and he’s been with us all the way. To Tim O’Reilly, for his willingness to launch into something completely new and different. Thanks to the clever Kyle Hart for figuring out how Head First fits into the world and for launching the series. Finally, to Edie Freedman for designing the Head First “emphasize the head ” cover.

Images

Our intrepid beta testers and reviewer team:

Our top honors and thanks go to the director of our javaranch tech review team, Johannes de Jong. This is your fifth time around with us on a Head First book, and we’re thrilled you’re still speaking to us. Jeff Cumps is on his third book with us now and relentless about finding areas where we needed to be more clear or correct.

Corey McGlone, you rock. And we think you give the clearest explanations on JavaRanch. You’ll probably notice we stole one or two of them. Jason Menard saved our technical butts on more than a few details, and Thomas Paul, as always, gave us expert feedback and found the subtle Java issues the rest of us missed. Jane Griscti has her Java chops (and knows a thing or two about writing), and it was great to have her helping on the new edition along with long-time javarancher Barry Gaunt.

Marilyn de Queiroz gave us excellent help on both editions of the book. Chris Jones, John Nyquist, James Cubeta, Terri Cubeta, and Ira Becker gave us a ton of help on the first edition.

Special thanks to a few of the Head Firsters who’ve been helping us from the beginning: Angelo Celeste, Mikalai Zaikin, and Thomas Duff (twduff.com). And thanks to our terrific agent, David Rogelberg of StudioB (but seriously, what about the movie rights?)

Images

Just when you thought there wouldn’t be any more acknowledgments*

More Java technical experts who helped out on the first edition (in pseudo-random order):

Emiko Hori, Michael Taupitz, Mike Gallihugh, Manish Hatwalne, James Chegwidden, Shweta Mathur, Mohamed Mazahim, John Paverd, Joseph Bih, Skulrat Patanavanich, Sunil Palicha, Suddhasatwa Ghosh, Ramki Srinivasan, Alfred Raouf, Angelo Celeste, Mikalai Zaikin, John Zoetebier, Jim Pleger, Barry Gaunt, and Mark Dielen.

The first edition puzzle team:

Dirk Schreckmann, Mary “JavaCross Champion” Leners, Rodney J. Woodruff, Gavin Bong, and Jason Menard. Javaranch is lucky to have you all helping out.

Other co-conspirators to thank:

Paul Wheaton, the javaranch Trail Boss for supporting thousands of Java learners. Solveig Haugland, mistress of J2EE and author of Dating Design Patterns. Authors Dori Smith and Tom Negrino (backupbrain.com), for helping us navigate the tech book world.

Our Head First partners in crime, Eric Freeman and Beth Freeman (authors of Head First Design Patterns), for giving us the Bawls™ to finish this on time.

Sherry Dorris, for the things that really matter.

Brave early adopters of the Head First series:

Joe Litton, Ross P. Goldberg, Dominic Da Silva, honestpuck, Danny Bromberg, Stephen Lepp, Elton Hughes, Eric Christensen, Vulinh Nguyen, Mark Rau, Abdulhaf, Nathan Oliphant, Michael Bradly, Alex Darrow, Michael Fischer, Sarah Nottingham, Tim Allen, Bob Thomas, and Mike Bibby (the first).

*The large number of acknowledgments is because we’re testing the theory that everyone mentioned in a book acknowledgment will buy at least one copy, probably more, what with relatives and everything. If you’d like to be in the acknowledgment of our next book, and you have a large family, write to us.

Get Head First Java, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.