Introduction

As Java nears its 20th birthday, I think it’s time people stopped using the term cool to describe this technology and rock star to describe the people who have made shining contributions to it. In dog time, Java is 140 years old. In Internet time, that’s a millennium or so. Java today is more like the Roman Empire than a cultural phenomenon. It is pervasive and far-flung, a world power, a Goliath its neighbors fear and loathe. It supports a culture at least as worried about its decline as it is hopeful about its advance. And it is a well-guarded prize that those nerd barbarians on the steppes dream of sacking every day, as they should.

Java has a canon of literature devoted to it, books worth reading twice and keeping close at hand. There are websites loaded with friendly (or cagey) advice, open-air markets of tech articles, and Java experts. Java prophets hold forth at every forum; everyday users, mostly held together with caffeine and product logos, roam convention halls everywhere for popular talks and prime wireless signal. There is even a pantheon of major and minor Java deities, some of whom appear at so many conferences you might wonder when they write code or if they do anything else.

Java has been serious business for some time, and still is. Tens of thousands of people talk every day about its use, its applications, its rightful owners, and its future developments. It is all a conversation, as they like to say in the academic world. But what has not kept pace with this activity, in my view, is attention to how we should teach Java with this bustling culture in view.

The recent introductory books I have browsed take a pedagogical approach, as if you must recapitulate the learning process you got in middle school to learn anything new. These books introduce the elements of the language, a functional grammar, and a thin, neutral style for observing it. They skip lightly for hundreds of pages, emphasizing a necessary breadth of topics. The authors believe in developing your competence across this terrain: new element, brief example, relevant exercise, next topic.

This stuff is all about identifying the conversation I have just described. In this book, I want to help you join it. I believe you want that too. You want to learn to participate in it, contribute to and learn from the exchange, and get more from it over time. Even though spelling bees are fun for competition, and maybe course grades gave you a sense of accomplishment, that’s not the primary way you learn anymore.

Certification is, in my view, a low rung. If you want to find work in this field, it’s my failure to let you believe a high score on the exam will do more than bolster your confidence. The most common complaint I hear from colleagues is that the people they interview can’t say anything about Java programming. They can recite facts and answer stock questions if they aren’t too nervous. And certification may have been required to get the interview, but now what? If you cannot, in that moment, participate in a small conversation with a trained professional who engages in it every day, the interview is a waste of everyone’s time, including yours.

For that reason, I’ve included my opinions on Java usage and style in this book, as well as rules and rote facts. In the later chapters, I give you quite a bit more information on form and uses for Java than you need to get certified. I include some ideas that, if you learn to apply them, can raise your interviewer’s eyebrows: You know that already? In this book, you’ll learn more than someone who is trying only to meet expectations.

I have no idea what amount of time or knowledge is required for you to pass the exam. I don’t much care. This book will help you enter the conversation, using full sentences, discussing ideas on good practice and knowing there are multiple ways to write any program. If you’re new to this field, you can start by mimicking what I have to say (and hopefully not get spotted too soon). If you’re a skeptic who reads widely, you can balance this material against your other sources and start some conversations yourself. If you’re smarter or more informed than I am, you can prove me wrong and burn me in effigy on Amazon. Bring whatever you have, but above all show me what you have learned, not your exam score. Passing is passing.

In this book, you will learn the way adults learn: by relating what you know to what you don’t know, by interacting with people who know a bit more and a bit less than you do, by treating your mistakes as just another way to learn, and by doing something new with Java every day. I will talk to you about moving around this subject more like a planet and less like an electron. If you want to join this conversation—and pass the exam in due time—this book is for you.

Who Should Read This Book

This book is for people who want to learn Java programming well enough to recommend themselves for entry-level work in this field. Earning the Oracle Certified Associate, Java SE 7 Programmer certification is a constructive step in that direction. I’ve covered all the stated requirements for that exam in this book.

To profit fully from this book, in my view, you must invest time and effort in reading and writing Java code, not just reading about it. Before you can do that, you need to learn elements of the language, and before you can do that, you need a guide who can tell you something about the practice of programming. You should also ask whether you fancy sitting for hours at a time writing code, making it work, testing its reliability, and—here’s the killer—explaining how it works to someone else.

If you’re taking a college course, I believe this book is the best available guide to start learning and writing simple programs. If this book isn’t on your syllabus, propose it to your instructor. If it is, awesome! Yay for me! As for you, you’ll get two teachers for the price of one. I offer advice on good practice, provide a context for the skills you’re acquiring, and a few exercises to get the ball rolling. This is your chance to take on an occasional curveball, as you will at any job, but without people watching or expecting quick results.

If you’re the self-teaching type, this book will help you write better code along the way to preparing for the exam. This book is not just enough Java to get you over that hurdle. I aim to teach you Java itself. I want you to develop enough skill to help you get a job. Your ability to define keywords, identify faulty lines of code, and answer 68 questions correctly in 150 minutes or less—that’s all fine. It’s one bump in the road and, in my opinion, a minor one. You need to see Java in motion, set it in motion, and identify its motion in a few dozen lines of code. If that sounds like something you want to know how to do, this book is for you.

What You Will Learn

At a minimum, you’ll cover all the topics you need to prepare for the OCA, Java SE 7 Programmer exam. With proper study, you will apply the fundamentals of the Java programming language to increasingly complex Java program examples, learn how to write small tests and sample programs, and distinguish between code that merely works and code that is easier to read, maintain, and reuse. You’ll start by writing simple Java classes that contain only a main() method; go on to design methods and classes following some simple, easy-to-remember guidelines; and finally, create your first abstract types for adapting existing Java code to a wider range of use. You’ll wrap it all up by seeing how exception handling can help you protect and preserve the state of a running program as it encounters errors.

What You Need

A computer with Java Development Kit (JDK) SE version 7 fully and properly installed—Some examples in this book will work with JDK 6, but many of the examples apply features that are specific to JDK 7.
A text editor—I used vi to write every code example in this book. You might prefer a program that will highlight Java keywords and so on. Please do avoid an integrated development environment (IDE) like Eclipse or NetBeans for now, if you can. A beginner, in my opinion, should start with few tools and type things out. An IDE is more a crutch than a conveyance when you’re first learning to walk. Your legs can’t form right, in my opinion, if you don’t first train them to work on their own.
A user community—There are opinions in this book that other knowledgeable people may disagree with. That’s healthy and I encourage it. If you don’t know where to go, try the JavaRanch website. I may even see you there.
Time to write code—Don’t cheat yourself. People like me can separate, from yards away, those who have written a lot of code from those who cut, paste, compile, and call it learning. We’ll figure you out fast. Do the work.

What Is Covered in This Book

Java SE 7 Programming Essentials includes all the topics that make up the Oracle Certified Associate, Java SE 7 Programmer exam. It is organized to take the beginning student from language basics to understanding how Java programmers use inheritance, abstract types, exception handling, and fundamental design techniques in practice every day.

Chapter 1: Introducing the Basics of Java You’ll write your first Java program, learn how to import code into a program, and take a first look at using variables.
Chapter 2: Applying Data Types in Java Programming You’ll get your first look at working with primitive types, classes, and objects in Java. You’ll take a full look at two classes that manage text, the String and StringBuilder classes.
Chapter 3: Using Java Operators and Conditional Logic You’ll review Java’s built-in operators and use them to form expressions. You’ll learn how to test objects for equality and use tests in general to form decision statements.
Chapter 4: Using Java Arrays You’ll examine the anatomy of a Java array and test it in a sample program. You’ll also take a close look at the ArrayList class and learn its method interface.
Chapter 5: Using Loops in Java Code You’ll learn how to apply looping statements to call code repeatedly and how to break out of loops on specific conditions.
Chapter 6: Encapsulating Data and Exposing Methods in Java Starting here, you’ll address issues of design and style when writing Java code. You’ll learn how encapsulation works, how to manage class member visibility, and what is meant by abstracting data.
Chapter 7: Using Java Methods to Communicate You’ll design Java methods to clearly express their purpose and review the rules for passing primitive and object types from one method to another.
Chapter 8: Using Java Constructors You’ll write constructors to suit a variety of specials needs when making objects out of your classes.
Chapter 9: Inheriting Code and Data in Java You’ll learn how one class inherits from another and how it differs from encapsulation. You’ll also write subclasses that have to pass parameters to their parent classes before they’ll work.
Chapter 10: Understanding Java Interfaces and Abstract Classes You’ll learn how to form a type system in this chapter and review a complete program example—using an interface, an abstract class, and an enumeration type—to model a hypothetical business.
Chapter 11: Throwing and Catching Exceptions in Java You’ll throw and catch exception objects, learn the difference between checked and unchecked exceptions, and identify commonly seen exception classes.
Appendix A: Answers to Review Questions This appendix includes solutions and explanations for the questions at the end of each chapter.
Appendix B: OCA Certification Program This appendix outlines the requirements of the Oracle Certified Associate, Java SE 7 Programmer exam, including the format, minimum passing score, and exam duration. It also maps each exam topic to the chapter in which it is discussed.

You can also download Appendix C: Answers to Additional Exercises online at www.sybex.com/go/javase7essentials. This appendix includes solutions and explanations for the exercises at the end of each chapter.

The Essentials Series

The Essentials series from Sybex provides outstanding instruction for readers who are just beginning to develop their professional skills. Every Essentials book includes these features:

  • Skill-based instruction with chapters organized around projects rather than abstract concepts or subjects.
  • Suggestions for additional exercises at the end of each chapter, where you can practice and extend your skills.
  • Digital files (via download) so you can work through the project tutorials yourself. Please check the book’s web page at www.sybex.com/go/javase7essentials for these companion downloads.
certobjective.eps

The certification margin icon will alert you to passages that are especially relevant to Oracle Certified Associate, Java SE 7 Programmer certification. See Appendix B, “OCA Certification Program,” for more information.

How to Contact the Author

If you have feedback for this book, please write to me at [email protected]. My other email addresses aren’t big secrets, but I do filter my inboxes without let or hindrance. If you subscribe to JavaRanch, you can post longer comments and critiques in a forum there and let me know you did. Please don’t use their private message facility to contact me.

You have a short comment? I’m @mfernest on Twitter; you can use #Java7Essentials if you like. I have a profile on LinkedIn that you can consult if you’d like more information on my professional background. If you want information on the custom courses, seminars, and training I provide, and you have some dates in mind, please write to [email protected]. We are a small word-of-mouth business driven by human contact and long-term business relationships. There’s no website.

I maintain a tech-ish blog at http://radio.javaranch.com/michael. I don’t post there so often, but if I have something more to add about this book, and it’s not the kind of thing Sybex considers a useful supplement, I’ll post it there.

Sybex strives to keep you supplied with the latest tools and information you need for your work. Please check www.sybex.com/go/javase7essentials, where I’ll post additional content and updates that supplement this book if the need arises.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.255.250