Book Review - Murach's Java Programming, 5th Edition
Book Review - Murach's Java Programming, 5th Edition
murach’s
Java
programming
5th Edition
(Chapter 1)
Thanks for downloading this chapter from Murach’s Java Programming (5th Edition).
We hope it will show you how easy it is to learn from any Murach book, with its paired-
pages presentation, its “how-to” headings, its practical coding examples, and its clear,
concise style.
To view the full table of contents for this book, you can go to our website. From there,
you can read more about this book, you can find out about any additional downloads that
are available, and you can review our other books on related topics.
Thanks for your interest in our books!
“I bought your Java book a week ago and I am already writing useful programs,
not ‘toys’!”
Richard Cooper, Programmer
“I love your Java book. It cuts right to the essential information, providing the
perfect balance between too many details and too little information. Example
apps are incredible. Keep up the good work.”
Steve, Programmer, Denver, Colorado
“Murach’s Java is now my go-to source for reference and learning and
brushing up, well above and beyond the other books in my collection.”
Jeff Salter, Sacramento Java Users Group (SacJUG)
“I love the 2-page style that you use in your Java book. It is such a great way to
organize the lessons.”
Dave Merton, Director of Development, Connecticut
“If I’d seen this book first, I would not have wasted money (and time) on
6 other books! This one is highly organized, clear, and very effective as a
learning tool.”
Posted at Amazon.com
“The style is clean, very user friendly, and simple. The narrative is totally
accurate and focuses on important issues; it is not dumbed down. This book is a
winner!”
Dr. Richard Wiener, Journal of Object Technology
Contents
Introduction xvii
Reference Aids
Appendix A How to set up Windows for this book 743
Appendix B How to set up Mac OS X for this book 753
Index 763
Section 1
Essential skills
This section gets you started quickly with Java programming. First, chapter
1 introduces you to Java applications and shows you how to use an IDE
to work with Java projects. Then, chapter 2 shows you how to write your
first Java applications. When you complete these chapters, you’ll be able to
write, test, and debug simple applications of your own.
After that, chapter 3 presents the details for working with the eight
primitive data types. Chapter 4 presents the details for coding control
statements. Chapter 5 shows how to code methods, handle exceptions, and
validate data. And chapter 6 shows how to thoroughly test and debug an
application. In addition, it shows how to deploy an application.
These are the essential skills that you’ll use in almost every Java
application that you develop. When you finish these chapters, you’ll be able
to write solid programs of your own. And you’ll have the background that
you need for learning how to develop object-oriented programs.
1
An introduction to Java
This chapter starts by presenting some background information about Java.
This information isn’t essential to developing Java applications, so you
can skim it if you want. However, it does show how Java works and how it
compares to other languages.
After the background information, this chapter shows how to use the
NetBeans IDE (Integrated Development Environment) to work with a Java
application. For this book, we recommend using NetBeans because we think
it’s the best and most intuitive IDE for getting started with Java.
However, Eclipse is another great IDE for working with Java
applications, and many programmers prefer it. As a result, the download for
this book includes a PDF file that shows how to use Eclipse with this book
instead of NetBeans. So, if you want to use Eclipse, you can use this PDF file
whenever you need to learn how to perform a task with Eclipse.
An overview of Java................................................................4
Java timeline.....................................................................................................4
Java editions......................................................................................................4
How Java compares to C++ and C#.................................................................6
Types of Java applications.....................................................8
Two types of desktop applications....................................................................8
Web applications and mobile apps.................................................................10
An introduction to Java development.................................12
The code for a console application.................................................................12
How Java compiles and interprets code.........................................................14
An introduction to Java IDEs.........................................................................16
How to use NetBeans to work with existing projects........18
An introduction to NetBeans..........................................................................18
How to open, close, and delete a project........................................................20
How to compile and run a project..................................................................20
How to use the Output window with a console application...........................22
How to work with two or more projects........................................................ 24
How to use NetBeans to develop new projects.................26
How to create a new project...........................................................................26
How to work with Java source code and files.................................................28
How to use the code completion feature........................................................30
How to detect and correct syntax errors.........................................................32
Perspective............................................................................34
4 Section 1 Essential Java skills
An overview of Java
In 1996, Sun Microsystems released a new programming language called
Java. Today, Java is owned by Oracle and is one of the most widely used
programming languages in the world.
Java timeline
Figure 1-1 starts by describing all major releases of Java starting with
version 1.0 and ending with version 1.9. Throughout Java’s history, the terms
Java Development Kit (JDK) and Software Development Kit (SDK) have been
used to describe the Java toolkit. In this book, we’ll use the term JDK since it’s
the most current and commonly used term.
In addition, different numbering schemes have been used to indicate the
version of Java. For example, Java SE 8 or Java 1.8 both refer to the eighth
major version of Java. Similarly, Java SE 9 and Java 1.9 both refer to the ninth
major version of Java. The documentation for the Java API uses the 1.x style
of numbering. As a result, you should be familiar with it. However, it’s also
common to only use a single number such as Java 6.
This book shows how to use Java 9. However, Java is backwards compatible,
so future versions of Java should work with this book too. In addition, most of
the skills described in this book have been a part of Java since its earliest
versions. As a result, earlier versions of Java work with most of the skills
described in this book.
Java editions
This figure also describes the three most common editions of Java. To start,
the Standard Edition is known as Java SE. It’s designed for general purpose use
on desktop computers and servers, and it’s the edition that you’ll learn how to
work with in this book. For example, you can use Java SE to create a desktop
application like the ones presented in section 4.
The Enterprise Edition is known as Java EE. It’s designed to develop
distributed applications that run on an intranet or the Internet. You can use Java
EE to create web applications.
The Micro Edition is known as Java ME. It’s designed to run on devices that
have limited resources, such as mobile devices, TV set-top boxes, printers, smart
cards, hotel room key cards, and so on.
With some older versions of Java, Java SE was known as J2SE (Java 2
Platform, Standard Edition). Similarly, Java EE was known as J2EE (Java 2
Platform, Enterprise Edition). If you are searching for information about Java on
the Internet, you may come across these terms. However, they aren’t commonly
used anymore.
Chapter 1 An introduction to Java 5
Java timeline
Year Month Release
1996 January JDK 1.0
1997 February JDK 1.1
1998 December SDK 1.2
1999 August Java 2 Platform, Standard Edition (J2SE)
December Java 2 Platform, Enterprise Edition (J2EE)
2000 May J2SE with SDK 1.3
2002 February J2SE with SDK 1.4
2004 September J2SE 5.0 with JDK 1.5
2006 December Java SE 6 with JDK 1.6
2011 July Java SE 7 with JDK 1.7
2014 March Java SE 8 with JDK 1.8
2017 July Java SE 9 with JDK 1.9
Java editions
Platform Description
Java SE (Standard Edition) For general purpose use on desktop computers and servers. Some
early versions were called J2SE (Java 2 Platform, Standard Edition).
Java EE (Enterprise Edition) For developing distributed applications that run on an intranet or the
Internet. Some early versions were called J2EE (Java 2 Platform,
Enterprise Edition).
Java ME (Micro Edition) For devices with limited resources such as mobile devices, TV set-top
boxes, printers, and smart cards.
Description
•• The Java Development Kit (JDK) includes a compiler, a runtime environment, and
other tools that you can use to develop Java applications. Some early versions were
called the Software Development Kit (SDK).
•• Java was originally developed and released by Sun Microsystems. However, Oracle
bought Sun Microsystems in April 2010.
Java compared to C#
Feature Description
Syntax Java syntax is similar to C# syntax.
Platforms Like Java, compiled C# code can run on any platform that
has a runtime environment for it.
Speed Java runs faster than C# in most contexts.
Memory Like Java, C# handles most memory operations automatically.
A console application
A GUI application
Description
•• A console application uses the console to interact with the user.
•• A GUI application uses a graphical user interface to interact with the user.
A web application
A mobile app
Description
•• An applet is a type of Java application that runs within a web browser. In the past,
it was possible to run applets in most web browsers. Today, fewer and fewer web
browsers support applets, so they are effectively obsolete.
•• A servlet is a type of Java application that runs on a web server. A servlet accepts
requests from clients and returns responses to them. Typically, the clients are web
browsers.
•• A mobile app uses a mobile device such as a smartphone or tablet to interface with
the user.
•• The Android operating system supports a subset of Java, including all features of
Java 7 and some features of Java 8.
Description
•• When you develop a Java application, you typically use a code editor to work with
the source code for the application. Files that contain source code have the .java
extension.
•• The Java compiler translates Java source code into a platform-independent format
known as Java bytecode. Files that contain Java bytecode have the .class extension.
•• A Java virtual machine (JVM) includes a Java interpreter that executes Java
bytecode. Most modern implementations of the JVM have replaced the Java
interpreter with a just-in-time compiler (JIT compiler). A JIT compiler is similar to
an interpreter in some ways, but it improves performance significantly.
•• A Java runtime environment (JRE) has all of the components necessary to run
bytecode including a JVM. Since JREs are available for most operating systems,
Java bytecode can be run on most operating systems.
Description
•• To develop Java applications, you typically use an Integrated Development
Environment (IDE) like those listed above. All of these IDEs are either free or have
free editions.
An introduction to NetBeans
Figure 1-8 shows the NetBeans IDE with an open Java project. In NetBeans,
a project is a folder that contains all the files for an application. In this example,
the project is named ch01_FutureValueConsole.
In the Projects window, the ch01_FutureValueConsole folder contains two
subfolders: (1) Source Packages and (2) Libraries. The Source Packages folder
contains the source files for the application, and the Libraries folder contains the
Java libraries that are used by the application. In this case, the application uses
just the JDK 1.8 libraries, but you can add others.
Within the Source Packages folder, the source files can be organized
into packages. In this case, there’s only one file, and it’s stored in the default
package. For simple applications like this, that’s acceptable. However, as you
develop more complex applications, it’s considered a best practice to store your
source files in packages as described in chapter 10.
In this figure, the application consists of a single source file named
FutureValueApp.java, and this file is open in the code editor. Because this class
contains the main() method for the application, it’s called the main class. When
you run an application, the main() method in the main class is executed by
default.
As of this writing, Java 9 has not yet been released. That’s why the JDK 1.8
library is used by this application, and that’s why NetBeans 8.1 is used to work
with this project. When Java 9 is released in July of 2017, though, you’ll be able
to use JDK 1.9 and NetBeans 9.0.
Chapter 1 An introduction to Java 19
Description
•• A NetBeans project consists of a top-level folder that contains the subfolders and
files for an application.
•• The Source Packages subfolder contains the .java files that make up the project.
These files define classes that are later compiled into .class files.
•• At a minimum, a project consists of a single class that contains the main() method.
The main() method is the starting point for the application. The class that contains
the main() method can be referred to as the main class.
•• The .java files that make up a project can be organized into one or more packages.
It’s generally considered a best practice to organize your classes by storing each
class within a package.
•• The Libraries subfolder contains the libraries that are available to your project.
These libraries contain the Java classes that you can use in your projects. By
default, you can use the classes in the JDK libraries.
•• The folders, files, and libraries that make up a Java project are listed in the
Projects window. If this window isn’t visible, you can display it by using the
WindowProjects command. Then, you can expand and collapse the nodes in this
window by clicking on the plus and minus signs.
•• When Java 9 is released in July of 2017, you’ll be able to use JDK 1.9 instead of
1.8 and NetBeans 9.0 instead of 8.1.
Mac OS X note
•• To enable right-clicking with Mac OS X, you can edit the system preferences for
the mouse.
An application that uses the Output window for input and output
Description
•• When you run an application that prints data to the console, that data is displayed in
the Output window.
•• When you run an application that requests input from the console, the Output
window pauses to accept the input. Then, you can click in the Output window, type
the input, and press the Enter key.
•• In addition to displaying output and accepting input, the Output window can display
other information. For example, it can display messages when the application is
compiled, and it can display errors that are encountered when an application is run.
Figure 1-10 How to use the Output window with a console application
24 Section 1 Essential Java skills
Description
•• NetBeans lets you open and work with two or more projects at the same time.
•• When you open multiple projects, they all appear in the Projects window.
•• When you open a file for a project, NetBeans opens the file in a tab in the main
window.
•• To run the currently selected project, press F6 or click the Run Project button in the
toolbar.
•• To change the currently selected project, click on the project in the Projects
window.
Description
•• To create a new project, use the FileNew Project command or click the New
Project button in the toolbar to display the New Project dialog box. Then, select a
project type, click the Next button, and complete the dialog box that’s displayed.
•• To create a Java Application project, enter the project name and location and the
name you want to use for the main class. It’s generally considered a best practice to
enter a package name for each class.
NetBean’s code editor with the starting source code for a project
Description
•• To open a .java file in the code editor, double-click on it in the Projects window.
Then, you can use normal editing techniques to work with the source code.
•• To collapse the code for a method or comment, click the minus sign (-) to its left.
Then, a plus sign (+) appears to the left of the method or comment, and you can
click the plus sign to display the code again.
•• To save the source code for a file, use the FileSave command (Ctrl+S) or click
the Save All Files button in the toolbar. This automatically compiles the file so it
doesn’t have to be compiled when the project is run.
•• To rename a file, right-click on it, select the RefactorRename command, and
enter the new name in the resulting dialog box.
•• To delete a file, you can right-click on it, select the Delete command, and confirm
the deletion in the resulting dialog box.
Figure 1-13 How to work with Java source code and files
30 Section 1 Essential Java skills
Description
•• You can use the code completion feature to help you enter the names of classes
and objects and select from the methods and fields that are available for a class or
object.
•• To activate the code completion feature for entering a class or object name, press
Ctrl+Spacebar after entering one or more letters. Then, a list of all the classes and
objects that start with those letters is displayed.
•• To insert an item from a code completion list, select the item and then press the
Enter key.
•• If you enter the opening quote for a string value, the code completion feature
automatically adds the closing quote and places the cursor between the two quotes.
Description
•• NetBeans often detects syntax errors as you enter code into the code editor.
•• When NetBeans detects a syntax error, it displays a red error icon to the left of the
statement in error, and it places a red wavy line under the statement.
•• To get more information about a syntax error, you can position the mouse pointer
over the error icon. Or, you can move the cursor to the line that contains the error
and press Alt+Enter.
Perspective
In this chapter, you were introduced to Java, and you learned how to use
an IDE such as NetBeans to create and run a Java application. With that as
background, you’re ready to learn how to write your own Java applications. But
first, I recommend that you familiarize yourself with an IDE such as NetBeans
by doing the exercises at the end of this chapter.
Summary
•• You can use the Java Development Kit (JDK) to develop Java applications.
•• The Standard Edition (SE) of Java is called Java SE.
•• You can use Java SE to create desktop applications that run on your
computer.
•• A desktop application can use a graphical user interface (GUI) or a console
to display output and get user input. Applications that use a console to
interact with the user are known as console applications.
•• You can use the Enterprise Edition of Java, which is known as Java EE, to
create web applications.
•• You can use Java to write the code for mobile apps such as Android apps.
•• The Java compiler translates source code into a platform-independent format
known as Java bytecode.
•• A Java runtime environment (JRE) includes all of the software needed to run
bytecode.
•• A JRE includes an implementation of a Java virtual machine (JVM).
•• A JVM includes a Java interpreter to translate the Java bytecode into native
code that can be understood by the underlying operating system.
•• An Integrated Development Environment (IDE) can make working with Java
easier by providing code completion, error detection, automatic compilation,
and a debugger.
•• NetBeans and Eclipse are two of the most commonly used IDEs for Java
development.
•• Java code is stored in classes. The main class of an application is the class
that contains the main method (named main()), which is the starting point of
the application.
Chapter 1 An introduction to Java 35
• Take advantage of all the time- and work-saving features offered by an IDE as you
develop your applications
• Pick up a new skill whenever you want or need to by focusing on material that’s
new to you
• Look up coding details or refresh your memory on forgotten details when you’re
in the middle of developing a Java application
• Loan to your colleagues who will be asking you more and more questions about
Java programming
To get your copies of these books, you can order online at www.murach.com or call
us at 1-800-221-5528 (toll-free in the U.S. and Canada). And remember, when you
order directly from us, this book comes with my personal guarantee:
100% Guarantee
You must be satisfied. Each book you buy directly
from us must outperform any competing book or
course you’ve ever tried, or send it back within 60
days for a full refund…no questions asked.