Java7
A Beginners Tutorial
Third Edition
Budi Kurniawan
JavaTM7 : A Beginner's Tutorial
Copyright 2011 by Budi Kurniawan
Third Edition: October 2011
All rights reserved. No part of this book may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage and retrieval system, without written permission from the publisher, except for the inclusion of brief quotations in a review.
ISBN: 978-0-9808396-1-6
Printed in the United States of America
Book and Cover Designer: Mona Setiadi
Technical Reviewer: Paul Deck
Indexer: Chris Mayle
Trademarks
Oracle and Java are registered trademarks of Oracle and/or its affiliates.
UNIX is a registered trademark of The Open Group.
Microsoft Internet Explorer is either a registered trademark or a trademark of Microsoft
Corporation in The United States and/or other countries.
Apache is a trademark of The Apache Software Foundation.
Firefox is a registered trademark of the Mozilla Foundation.
Google is a trademark of Google, Inc.
Throughout this book the printing of trademarked names without the trademark symbol is for editorial purpose only. We have no intention of infringement of the trademark.
Warning and Disclaimer
Every effort has been made to make this book as accurate as possible. The author and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information in this book.
Introduction
Welcome to Java 7: A Beginner's Tutorial (3rdEdition). Java is a mature programming language that is easy to learn. At the same time it is also a vast collection of technologies that are so diverse that beginners often don't know where to start. If you are one of them, then this book is for you because it has been designed as a tutorial for novices.
As a beginner's tutorial, this book does not teach you every Java technology there is. (It is impossible to cram everything into a single volume anyway, and that's why most Java titles are focused on one technology.) Rather, this book covers the most important Java programming topics that you need to master to be able to learn other technologies yourself. Nonetheless this book is comprehensive that by fully understanding all the chapters and doing the exercises you'll be able to perform an intermediate Java programmer's daily tasks quite well.
This book offers all the three subjects that a professional Java programmer must be proficient in:
Java as a programming language;
Object-oriented programming (OOP) with Java;
Java core libraries.
What makes structuring an effective Java course difficult is the fact that the three subjects are interdependent. On the one hand, Java is an OOP language, so its syntax is easier to learn if you already know about OOP. On the other hand, OOP features such as inheritance, polymorphism, and data encapsulation, are best taught if accompanied by real-world examples. Unfortunately, understanding real-world Java programs requires knowledge of the Java core libraries.
Because of such interdependence, the three main topics are not grouped into three isolated parts. Instead, chapters discussing a major topic and chapters teaching another are interwoven. For example, before explaining polymorphism, this book makes sure that you are familiar with certain Java classes so that real-world examples can be given. In addition, because a language feature such as generics cannot be explained effectively without the comprehension of a certain set of classes, it is covered after the discussion of the supporting classes.
There are also situations whereby a topic can be found in two or more places. For instance, the for statement is a basic language feature that should be discussed in an early chapter. However, for can also be used to iterate over a collection of objects, a feature that should only be given after the Collections Framework is taught. Therefore, for is first presented in , The Collections Framework.
The rest of this introduction presents a high-level overview of Java, an introduction to OOP, a brief description of each chapter, and instructions for installing the Java software.
Java, the Language and the Technology
Java is not only an object-oriented programming language, it is also a set of technologies that make software development more rapid and resulting applications more robust and secure. For years Java has been the technology of choice because of the benefits it offers:
platform independence
ease of use
complete libraries that speed up application development
security
scalability
extensive industry support
Sun Microsystems introduced Java in 1995 and Javaeven though it had been a general-purpose language right from the startwas soon well known as the language for writing applets, small programs that run inside web browsers and add interactivity to static websites. The growth of the Internet had much to contribute to the early success of Java.
Having said that, applets were not the only factor that made Java shine. The other most appealing feature of Java was its platform-independence promise, hence the slogan Write Once, Run Anywhere. What this means is the very same program you write will run on Windows, Unix, Mac, Linux, and other operating systems. This was something no other programming language could do. At that time, C and C++ were the two most commonly used languages for developing serious applications. Java seemed to have stolen their thunder since its first birthday.
That was Java version 1.0.
In 1997, Java 1.1 was released, adding significant features such as a better event model, Java Beans, and internationalization to the original.
Java 1.2 was launched in December 1998. Three days after it was released, the version number was changed to 2, marking the beginning of a huge marketing campaign that started in 1999 to sell Java as the next generation technology. Java 2 was sold in four flavors: the Standard Edition (J2SE), the Enterprise Edition (J2EE), the Micro Edition (J2ME), and Java Card (that never adopted 2 in its brand name).
The next version released in 2000 was 1.3, hence J2SE 1.3. 1.4 came two years later to make J2SE 1.4. J2SE version 1.5 was released in 2004. However, the name Java 2 version 1.5 was then changed to Java 5.
On November 13, 2006, a month before the official release date of Java 6, Sun Microsystems announced that it had open-sourced Java. Java SE 6 was the first Java release for which Sun Microsystems had invited outside developers to contribute code and help fix bugs. True that the company had in the past accepted contributions from non-employees, like the work of Doug Lea on multithreading, but this was the first time Sun had posted an open invitation. The company admitted that they had limited resources, and outside contributors would help them cross the finish line sooner.