Table of Contents (the real thing)
-
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?
-
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.
-
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.
-
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.
-
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!
-
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 Startup (similar to Battleship).
-
Java ships with hundreds of prebuilt 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.
-
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 8 ways to make flexible code, and if you act nowâa bonus lesson on the 4 tips for exploiting inheritance.
-
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...
-
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.
-
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.
-
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?
-
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.
-
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.
-
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.
-
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.
-
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.
-
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?
-
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.
-
Final Code Kitchen. All the code for the full client-server chat beat box. Your chance to be a rock star.
-
The top ten-ish topics that didnât make it into the rest of 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.
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.