Search Ebook here:


Head First Java: A Brain-Friendly Guide 3rd Edition



Head First Java: A Brain-Friendly Guide 3rd Edition PDF

Author: Kathy Sierra

Publisher: O'Reilly Media

Genres:

Publish Date: June 21, 2022

ISBN-10: 1491910771

Pages: 752

File Type: Epub

Language: English

read download

Book Preface

Table of Contents (the full version)

  • How to Use This Book: Intro

    Your brain on Java. Here you are trying to learn something, while here your brain is doing you a favor by making sure the learning doesn’t stick. Your brain’s thinking, “Better leave room for more important things, like which wild animals to avoid and whether naked snowboarding is a bad idea.” So how do you trick your brain into thinking that your life depends on knowing Java?

    • “Who is this book for?”
    • “We know what you’re thinking.”
    • “Metacognition: thinking about thinking.”
    • “Here’s what WE did:”
    • “Here’s what YOU can do to bend your brain into submission.”
    • “What you need for this book:”
    • “Last-minute things you need to know:”
  • Chapter 1

    Java takes you to new places. From its humble release to the public as the (wimpy) version 1.02, Java seduced programmers with its friendly syntax, object-oriented features, memory management, and best of all—the promise of portability. We’ll take a quick dip and write some code, compile it, and run it. We’re talking syntax, loops, branching, and what makes Java so cool. Dive in.

    • “The Way Java Works”
    • “What you’ll do in Java”
    • “A Very Brief History of Java”
    • “Code structure in Java”
    • “Writing a class with a main()”
    • “Simple boolean tests”
    • “Conditional branching”
    • “Coding a Serious Business Application”
    • “Phrase-O-Matic”
    • “Exercise”
    • “Exercise Solutions”
  • Chapter 2

    I was told there would be objects. In Chapter 1, we put all of our code in the main() method. That’s not exactly object-oriented. So now we’ve got to leave that procedural world behind and start making some objects of our own. We’ll look at what makes object-oriented (OO) development in Java so much fun. We’ll look at the difference between a class and an object. We’ll look at how objects can improve your life.

    • “Chair Wars”
    • “Making your first object”
    • “Making and testing Movie objects”
    • “Quick! Get out of main!”
    • “Running the Guessing Game”
    • “Exercise”
    • “Puzzle Solutions”
  • Chapter 3

    Variables come in two flavors: primitive and reference. There’s gotta be more to life than integers, Strings, and arrays. What if you have a PetOwner object with a Dog instance variable? Or a Car with an Engine? In this chapter we’ll unwrap the mysteries of Java types and look at what you can declare as a variable, what you can put in a variable, and what you can do with a variable. And we’ll finally see what life is truly like on the garbage-collectible heap.

    • “Declaring a variable”
    • ““I’d like a double mocha, no, make it an int.””
    • “Back away from that keyword!”
    • “Controlling your Dog object”
    • “An object reference is just another variable value.”
    • “Life on the garbage-collectible heap”
    • “An array is like a tray of cups”
    • “A Dog example”
    • “Exercise”
    • “Exercise Solutions”
  • Chapter 4

    State affects behavior, behavior affects state. We know that objects have state and behavior, represented by instance variables and methods. Now we’ll look at how state and behavior are related. An object’s behavior uses an object’s unique state. In other words, methods use instance variable values. Like, “if dog weight is less than 14 pounds, make yippy sound, else…” Let’s go change some state!

    • “Remember: a class describes what an object knows and what an object does
    • “The size affects the bark”
    • “You can send things to a method”
    • “You can get things back from a method.”
    • “You can send more than one thing to a method”
    • “Cool things you can do with parameters and return types”
    • “Encapsulation”
    • “How do objects in an array behave?”
    • “Declaring and initializing instance variables”
    • “Comparing variables (primitives or references)”
    • “Exercise”
    • “Exercise Solutions”
  • Chapter 5

    Let’s put some muscle in our methods. You dabbled with variables, played with a few objects, and wrote a little code. But you need more tools. Like operators. And loops. Might be useful to generate random numbers. And turn a String into an int, yeah, that would be cool. And why don’t we learn it all by building something real, to see what it’s like to write (and test) a program from scratch. Maybe a game, like Sink a Dot Com (similar to Battleship).

    • “Let’s build a Battleship-style game: “Sink a Startup””
    • “Developing a Class”
    • “Writing the method implementations”
    • “Writing test code for the SimpleStartup class”
    • “The checkYourself() method”
    • “Prep code for the SimpleStartupGame class”
    • “The game’s main() method”
    • “Let’s play”
    • “More about for loops”
    • “The enhanced for loop”
    • “Casting primitives”
    • “Exercise”
    • “Exercise Solutions”
  • Chapter 6

    Java ships with hundreds of pre-built classes. You don’t have to reinvent the wheel if you know how to find what you need from the Java library, commonly known as the Java API. You’ve got better things to do. If you’re going to write code, you might as well write only the parts that are custom for your application. The core Java library is a giant pile of classes just waiting for you to use like building blocks.

    • “In our last chapter, we left you with the cliff-hanger. A bug.”
    • “Wake up and smell the library”
    • “Some things you can do with ArrayList
    • “Comparing ArrayList to a regular array”
    • “Let’s build the REAL game: “Sink a Startup””
    • “Prep code for the real StartupBust class”
    • “The final version of the Startup class”
    • “Super Powerful Boolean Expressions”
    • “Using the Library (the Java API)”
    • “Exercise”
    • “Exercise Solutions”
  • Chapter 7

    Plan your programs with the future in mind. What if you could write code that someone else could extend, easily? What if you could write code that was flexible, for those pesky last-minute spec changes? When you get on the Polymorphism Plan, you’ll learn the 5 steps to better class design, the 3 tricks to polymorphism, the ways to make flexible code, and if you act now—a bonus lesson on the 4 tips for exploiting inheritance.

    • “Chair Wars Revisited…”
    • “Understanding Inheritance”
    • “Let’s design the inheritance tree for an Animal simulation program”
    • “Looking for more inheritance opportunities”
    • “Using IS-A and HAS-A”
    • “How do you know if you’ve got your inheritance right?”
    • “When designing with inheritance, are you using or abusing?”
    • “Keeping the contract: rules for overriding”
    • “Overloading a method”
    • “Exercise”
    • “Exercise Solutions”
  • Chapter 8

    Inheritance is just the beginning. To exploit polymorphism, we need interfaces. We need to go beyond simple inheritance to flexibility you can get only by designing and coding to interfaces. What’s an interface? A 100% abstract class. What’s an abstract class? A class that can’t be instantiated. What’s that good for? Read the chapter…

    • “Did we forget about something when we designed this?”
    • “The compiler won’t let you instantiate an abstract class”
    • “Abstract vs. Concrete”
    • “You MUST implement all abstract methods”
    • “Polymorphism in action”
    • “What about non-Animals? Why not make a class generic enough to take anything?”
    • “When a Dog won’t act like a Dog”
    • “Let’s explore some design options for reusing some of our existing classes in a PetShop program.”
    • “Making and Implementing the Pet interface”
    • “Invoking the superclass version of a method”
    • “Exercise”
    • “Exercise Solutions”
  • Chapter 9

    Objects are born and objects die. You’re in charge. You decide when and how to construct them. You decide when to abandon them. The Garbage Collector (gc) reclaims the memory. We’ll look at how objects are created, where they live, and how to keep or abandon them efficiently. That means we’ll talk about the heap, the stack, scope, constructors, super constructors, null references, and gc eligibility.

    • “The Stack and the Heap: where things live”
    • “Methods are stacked”
    • “What about local variables that are objects?
    • “The miracle of object creation”
    • “Construct a Duck”
    • “Doesn’t the compiler always make a no-arg constructor for you? No!”
    • “Nanoreview: four things to remember about constructors”
    • “The role of superclass constructors in an object’s life.”
    • “Can the child exist before the parents?”
    • “What about reference variables?”
    • Figure
    • “Exercise”
    • “Exercise Solutions”
  • Chapter 10

    Do the Math. The Java API has methods for absolute value, rounding, min/max, etc. But what about formatting? You might want numbers to print exactly two decimal points, or with commas in all the right places. And you might want to print and manipulate dates, too. And what about parsing a String into a number? Or turning a number into a String? We’ll start by learning what it means for a variable or method to be static.

    • “MATH methods: as close as you’ll ever get to a global method”
    • “The difference between regular (non-static) and static methods”
    • “Initializing a static variable”
    • “Math methods”
    • “Wrapping a primitive”
    • “Autoboxing works almost everywhere”
    • “And now in reverse… turning a primitive number into a String”
    • “Number formatting”
    • “The format specifier”
    • “Exercise”
    • “Exercise Solutions”
  • Chapter 11

    Sorting is a snap in Java. You have all the tools for collecting and manipulating your data without having to write your own sort algorithms The Java Collections Framework has a data structure that should work for virtually anything you’ll ever need to do. Want to keep a list that you can easily keep adding to? Want to find something by name? Want to create a list that automatically takes out all the duplicates? Sort your coworkers by the number of times they’ve stabbed you in the back?

    • “Exploring the java.util API, List and Collections”
    • “Generics means more type-safety”
    • “Revisiting the sort( ) method”
    • “The new, improved, comparable Song class”
    • “Sorting using only Comparators”
    • “Updating the Jukebox code with Lambdas”
    • “Using a HashSet instead of ArrayList”
    • “What you MUST know about TreeSet…”
    • “We’ve seen Lists and Sets, now we’ll use a Map”
    • “Finally, back to generics”
    • “Exercise Solution”
  • Chapter 12

    What if… you didn’t need to tell the computer HOW to do something? In this chapter we’ll look at the Streams API. You’ll see how helpful lambda expressions can be when you’re using streams, and you’ll learn how to use the Streams API to query and transform the data in a collection.

    • “Tell the computer WHAT you want”
    • “When for loops go wrong”
    • “Introducing the Streams API”
    • “Getting a result from a Stream”
    • “Guidelines for working with streams”
    • “Hello Lambda, my (not so) old friend”
    • “Spotting Functional Interfaces”
    • “Lou’s Challenge #1: Find all the “rock” songs”
    • “Lou’s Challenge #2: List all the genres”
  • Chapter 13

    Stuff happens. The file isn’t there. The server is down. No matter how good a programmer you are, you can’t control everything. When you write a risky method, you need code to handle the bad things that might happen. But how do you know when a method is risky? Where do you put the code to handle the exceptional situation? In this chapter, we’re going to build a MIDI Music Player, that uses the risky JavaSound API, so we better find out.

    • “Let’s make a Music Machine”
    • “First we need a Sequencer”
    • “An exception is an object… of type Exception.”
    • “Flow control in try/catch blocks”
    • “Did we mention that a method can throw more than one exception?”
    • “Multiple catch blocks must be ordered from smallest to biggest”
    • “Ducking (by declaring) only delays the inevitable”
    • “Code Kitchen”
    • “Version 1: Your very first sound player app”
    • “Version 2: Using command-line args to experiment with sounds”
    • “Exercise”
    • “Exercise Solution”
  • Chapter 14

    Face it, you need to make GUIs. Even if you believe that for the rest of your life you’ll write only server-side code, sooner or later you’ll need to write tools, and you’ll want a graphical interface. We’ll spend two chapters on GUIs, and learn more language features including Event Handling and Inner Classes. We’ll put a button on the screen, we’ll paint on the screen, we’ll display a jpeg image, and we’ll even do some animation.

    • “It all starts with a window”
    • “Getting a user event”
    • “Listeners, Sources, and Events”
    • “Make your own drawing widget”
    • “Fun things to do in paintComponent()”
    • “GUI layouts: putting more than one widget on a frame”
    • “Inner class to the rescue!”
    • “lambdas to the rescue! (again)”
    • “Using an inner class for animation”
    • “An easier way to make messages / events”
    • “Exercise”
    • “Exercise Solutions”
  • Chapter 15

    Swing is easy. Unless you actually care where everything goes. Swing code looks easy, but then compile it, run it, look at it and think, “hey, that’s not supposed to go there.” The thing that makes it easy to code is the thing that makes it hard to control—the Layout Manager. But with a little work, you can get layout managers to submit to your will. In this chapter, we’ll work on our Swing and learn more about widgets.

    • “Swing components”
    • “Layout Managers”
    • “The Big Three layout managers: border, flow, and box.”
    • “Playing with Swing components”
    • “Code Kitchen”
    • “Making the BeatBox”
    • “Exercise”
    • “Exercise Solutions”
  • Chapter 16

    Objects can be flattened and inflated. Objects have state and behavior. Behavior lives in the class, but state lives within each individual object. If your program needs to save state, you can do it the hard way, interrogating each object, painstakingly writing the value of each instance variable. Or, you can do it the easy OO way—you simply freeze-dry the object (serialize it) and reconstitute (deserialize) it to get it back.

    • “Writing a serialized object to a file”
    • “If you want your class to be serializable, implement Serializable”
    • “Deserialization: restoring an object”
    • “Version ID: A Big Serialization Gotcha”
    • “Writing a String to a Text File”
    • “Reading from a Text File”
    • “Quiz Card Player (code outline)”
    • “Path, Paths, and Files (messing with directories)”
    • “Finally, a closer look at finally
    • “Saving a BeatBox pattern”
    • “Exercise”
    • “Exercise Solutions”
  • Chapter 17

    Connect with the outside world. It’s easy. All the low-level networking details are taken care of by classes in the java.net library. One of Java’s best features is that sending and receiving data over a network is really just I/O with a slightly different connection stream at the end of the chain. In this chapter we’ll make client sockets. We’ll make server sockets. We’ll make clients and servers. Before the chapter’s done, you’ll have a fully-functional, multithreaded chat client. Did we just say multithreaded?

    • “Connecting, Sending, and Receiving”
    • “The DailyAdviceClient”
    • “Writing a simple server application”
    • “Java has multiple threads but only one Thread class”
    • “The three states of a new thread”
    • “Putting a thread to sleep”
    • “Making and starting two threads (or more!)”
    • “Closing time at the thread pool”
    • “New and improved SimpleChatClient”
    • “Exercise”
    • “Exercise Solutions”
  • Chapter 18

    Doing two or more things at once is hard. Writing multithreaded code is easy. Writing multithreaded code that works the way you expect, can be much harder. In this final chapter, we’re going to show you some of the things that can go wrong when two or more threads are working at the same time. You’ll learn about some of the tools in java.util.concurrent that can help you to write multithreaded code that works correctly. You’ll learn how to create immutable objects (objects that don’t change) that are safe for multiple threads to use. By the end of the chapter, you’ll have a lot of different tools in your toolkit for working with concurrency.

    • “The Ryan and Monica problem, in code”
    • “Using an object’s lock”
    • “The dreaded “Lost Update” problem”
    • “Make the increment() method atomic. Synchronize it!”
    • “Deadlock, a deadly side of synchronization”
    • “Compare-and-swap with atomic variables”
    • “Using immutable objects”
    • “More problems with shared data”
    • “Use a thread-safe data structure”
    • “Exercise”
    • “Exercise Solutions”
  • Appendix A

    The final Code Kitchen project. All the code for the full client-server chat beat box. Your chance to be a rock star.

    • “Final BeatBox client program”
    • “Final BeatBox server program”
  • Appendix B

    The Top Ten-ish Things that didn’t make it into the book. We can’t send you out into the world just yet. We have a few more things for you, but this is the end of the book. And this time we really mean it.

    • “#11 JShell (Java REPL)”
    • “#10 Packages”
    • “#9 Immutability in Strings and Wrappers”
    • “#8 Access Levels and Access Modifiers (Who Sees What)”
    • “#7 Varargs”
    • “#6 Annotations”
    • “#5 Lambdas and Maps”
    • “#4 Parallel Streams”
    • “#3 Enumerations (also called Enumerated Types or Enums)”
    • “#2 Local Variable Type Inference (var)”
    • “#1 Records”

Download Ebook Read Now File Type Upload Date
Download here Read Now Epub July 7, 2022

How to Read and Open File Type for PC ?