This is not the document you are looking for? Use the search form below to find more!

Report home > Computer / Internet

Introduction to Java programming

3.33 (3 votes)
Document Description
Skill Level: Introductory The goal of this tutorial is to introduce you to the Java syntax you're most likely to encounter professionally, and to expose you to idioms that will help you avoid trouble. Follow along with Java professional Roy Miller as he guides you through the essentials of Java programming, including the OOP paradigm and how it applies to Java programming; Java language syntax and use; creating objects and adding behavior, working with collections, handling errors; and tips for writing better code.
File Details
Submitter
  • Username: feli
  • Name: feli
  • Documents: 15
Embed Code:

Add New Comment




Related Documents

An Introduction To Java Web Technology

by: igino, 40 pages

An Introduction to Java Web Technology (Java Servlet) Presented At: JECRC, Faculty of E&T, Jodhpur National University, Jodhpur, Rajasthan India -342001 HTTP HTTP ...

Introduction to Java Programming, Comprehensive, 8th Edition, Y. Daniel Liang, PRENTICE HALL, IM+EXAM and PROJECT+TB

by: mysmandtb, 9 pages

Solution Manuals and Test Banks I have huge collection of solution manuals and test banks. I strive to provide you unbeatable prices with excellent support. So, I assure you that you won’t be ...

Introduction to Econometrics Stock 2rd Edition Solutions Manual

by: gordonbarbier, 48 pages

Introduction to Econometrics Stock 2rd Edition Solutions Manual

Introduction to Financial Accounting Horngren 9th Edition Solutions Manual

by: gordonbarbier, 48 pages

Introduction to Financial Accounting Horngren 9th Edition Solutions Manual

Introduction to Management Accounting Horngren 14th Edition Solutions Manual

by: gordonbarbier, 48 pages

Introduction to Management Accounting Horngren 14th Edition Solutions Manual

Introduction to Managerial Accounting Brewer 5th Edition Solutions Manual

by: gordonbarbier, 48 pages

Introduction to Managerial Accounting Brewer 5th Edition Solutions Manual

Introduction to Management Science Taylor 10th Edition Solutions Manual

by: gordonbarbier, 48 pages

Introduction to Management Science Taylor 10th Edition Solutions Manual

Financial Accounting An Introduction to Concepts Methods and Uses Stickney 13th Edition Solutions Manual

by: gordonbarbier, 48 pages

Financial Accounting An Introduction to Concepts Methods and Uses Stickney 13th Edition Solutions Manual

Introduction to Management Accounting Horngren 14th Edition Solutions Manual

by: georgesheslers, 48 pages

Introduction to Management Accounting Horngren 14th Edition Solutions Manual

Introduction to Managerial Accounting Brewer 5th Edition Solutions Manual

by: georgesheslers, 48 pages

Introduction to Managerial Accounting Brewer 5th Edition Solutions Manual

Content Preview
Introduction to Java programming
Skill Level: Introductory
Roy W. Miller (roy@roywmiller.com)
IBM
19 Nov 2004
The Java language, and the ever-growing Java platform, have revolutionized
programming. The goal of this tutorial is to introduce you to the Java syntax you're
most likely to encounter professionally, and to expose you to idioms that will help you
avoid trouble. Follow along with Java professional Roy Miller as he guides you
through the essentials of Java programming, including the OOP paradigm and how it
applies to Java programming; Java language syntax and use; creating objects and
adding behavior, working with collections, handling errors; and tips for writing better
code.
Section 1. About this tutorial
What's this tutorial about?
This tutorial introduces you to object-oriented programming (OOP) with the Java
language. The Java platform is a vast topic, so we won't cover it all here, but we'll
cover enough to get you started. A follow-up tutorial will offer further information and
guidance in your Java programming endeavors.
The Java language certainly has its friends and foes, but its impact on the software
development industry is undeniable. On the plus side, the Java language gives
programmers less rope to hang themselves with than C++. It removes some of the
more onerous programming tasks, like explicit memory management, and allows
programmers to focus on business logic. On the negative side, according to OO
purists, the Java language has too many non-OO remnants to make it a good tool.
Regardless of your position, however, knowing how to use the language as a tool
when it's the right tool for the job is a wise career choice.
Should I take this tutorial?
Introduction to Java programming
© Copyright IBM Corporation 1994, 2005. All rights reserved.
Page 1 of 69

developerWorks®
ibm.com/developerWorks
The content of this tutorial is geared toward beginning Java programmers who might
not be familiar with OOP concepts, or with the Java platform specifically. It assumes
a general knowledge of downloading and installing software, and a general
knowledge of programming and data structures (like arrays), but doesn't assume
more than a cursory familiarity with OOP.
This tutorial will guide you through setting up the Java platform on your machine and
installing and working with Eclipse, a free integrated development environment
(IDE), to write Java code. From that point on, you'll learn the underpinings of Java
programming, including the OOP paradigm and how it applies to Java programming;
Java language syntax and use; creating objects and adding behavior, working with
collections, handling errors; and tips for writing better code. By the end of the
tutorial, you'll be a Java programmer -- a beginning Java programmer, but a Java
programmer nonetheless.
Software requirements
To run the examples or sample code in this tutorial, you'll need to have the Java 2
Platform, Standard Edition (J2SE) , version 1.4.2 or higher, and the Eclipse IDE
installed on your machine. Don't worry if you don't have these packages installed yet
-- we'll show you how to do that in Getting started. All code examples in this tutorial
have been tested with J2SE 1.4.2 running on Windows XP. One of the beautiful
things about the Eclipse platform, however, is that it runs on almost any OS you're
likely to use, including Windows 98/ME/2000/XP, Linux, Solaris, AIX, HP-UX, and
even Mac OS X.
Section 2. Getting started
Installation instructions
In this next few sections, I'll walk you through each of the steps for downloading and
installing the Java 2 Platform Standard Edition (J2SE), version 1.4.2, and the Eclipse
IDE. The former lets you compile and run Java programs. The latter gives you a
powerful and user-friendly way to write code in the Java language. If you already
have the Java SDK and Eclipse installed, feel free to skip to A brief Eclipse tour on
page or to the next section,
OOP concepts, if you're comfortable jumping right in.
Install the Java SDK
The original intent of the Java language was to let programmers write a single
program that would run on any platform, an idea encapsulated by the catch-phrase
"Write Once, Run Anywhere" (WORA). In reality, it's not quite that simple, but it's
Introduction to Java programming
Page 2 of 69
© Copyright IBM Corporation 1994, 2005. All rights reserved.

ibm.com/developerWorks
developerWorks®
becoming easier. The various components of Java technology support that effort.
The Java platform comes in three editions, Standard, Enterprise, and Mobile (the
latter two for enterprise mobile device development). We'll be working with J2SE,
which includes all of the core Java libraries. All you need to do is download it and
install it.
To download the J2SE software development kit (SDK), follow these steps:
1.
Open a browser and go to the Java Technology home page. In the top
middle of the page, you'll see links for various Java technology subject
areas. Select J2SE (Core/Desktop).
2.
In the list of current J2SE releases, click on J2SE 1.4.2.
3.
In the left navigation bar of the resulting page, click on Downloads.
4.
There are several downloads on this page. Find and click the Download
J2SE SDK
link.
5.
Accept the conditions of the license and click Continue.
6.
You'll see a list of downloads by platform. Chose the appropriate
download for whatever platform you're using.
7.
Save the file to your hard drive.
8.
When the download is complete, run the install program to install the SDK
on your hard drive, preferably in a well-named folder at the root of the
drive.
That's it! You now have a Java environment on your machine. The next step is to
install an integrated development environment (IDE).
Install Eclipse
An integrated development environment (IDE) hides lots of the mundane technical
details of working with the Java language, so you can focus on writing and running
code. The JDK you just installed includes several command-line tools that would let
you compile and run Java programs without an IDE, but using those tools quickly
becomes painful for all but the simplest programs. Using an IDE hides the details,
gives you powerful tools to help you program faster and better, and is simply a more
pleasant way to program.
It is no longer necessary to pay for an excellent IDE. The Eclipse IDE is an open
source project and is yours to download for free. Eclipse stores and tracks your Java
code in readable files stored on your file system. (You can use Eclipse to work with
code in a CVS repository as well.) The good news is that Eclipse lets you deal with
files if you want to, but hides the file details if you'd rather deal only with various Java
constructs like classes (which we'll discuss in detail later).
Introduction to Java programming
© Copyright IBM Corporation 1994, 2005. All rights reserved.
Page 3 of 69

developerWorks®
ibm.com/developerWorks
Downloading and installing Eclipse is simple. Follow these steps:
1.
Open a browser and go to the Eclipse Web site.
2.
Click the Downloads link on the left side of the page.
3.
Click the Main Eclipse Download Site link to go to the Eclipse project
downloads page.
4.
You'll see a list of build types and names. Click the 3.0 link.
5.
In the middle of the page, you'll see a list of Eclipse SDKs by platform;
choose the one appropriate for your system.
6.
Save the file to your hard drive.
7.
When the download is complete, run the install program and install
Eclipse on your hard drive, preferably in a well-named folder at the root of
the drive.
All that's left now is to set up the IDE.
Set up Eclipse
To use Eclipse to write Java code, you must tell Eclipse where Java is located on
your machine. Follow these steps:
1.
Launch Eclipse by double-clicking on eclipse.exe, or the equivalent
executable for your platform.
2.
When the Welcome screen appears, click the Go To The Workbench
link. This takes you to what's known as the Resource perspective (more
on this later).
3.
Click Window>Preferences>Installed JREs, which allows you to specify
where your Java environment is installed on your machine (see Figure 1).
Figure 1. Eclipse preferences
Introduction to Java programming
Page 4 of 69
© Copyright IBM Corporation 1994, 2005. All rights reserved.

ibm.com/developerWorks
developerWorks®
4.
Odds are good that Eclipse will find an installed Java Runtime
Environment (JRE), but you should explicitly point to the one you installed
in Install the Java SDK on page . You can do that in the Preferences
dialog. If Eclipse lists an existing JRE, click on it and press Edit;
otherwise click Add.
5.
Specify the path to the JRE folder of the JDK you installed in Install the
Java SDK on page .
6.
Click OK.
Eclipse is now set up to compile and run Java code. In the next section, we'll take a
brief tour of the Eclipse environment to familiarize you with the tool.
A brief Eclipse tour
Working with Eclipse is a large topic and mostly out of the scope of this tutorial. See
the Resources for links to more info on Eclipse. Here, we'll cover here just enough to
get you familiar with how Eclipse works, and how you can use it for Java
development.
Assuming that you still have Eclipse up and running, you left off looking at the
Resource perspective. Eclipse offers a set of perspectives on the code you write.
Introduction to Java programming
© Copyright IBM Corporation 1994, 2005. All rights reserved.
Page 5 of 69

developerWorks®
ibm.com/developerWorks
The Resource perspective shows a view of your file system within the Eclipse
workspace you're using. A workspace holds all the files related to Eclipse
development. Right now, there's nothing in your workspace that you really care
about.
In general, Eclipse has perspectives that contain views. In the Resource
perspective, you'll see a Navigator view, an Outline view, etc. You can drag and drop
all of these views to position them wherever you want. It's an almost infinitely
configurable environment. For now, though, the default arrangement is good
enough. But what we're looking at won't let us do what we want to do. The first step
to writing Java code in Eclipse is to create a Java project. This isn't a Java language
construct; it's simply an Eclipse construct that helps you organize your Java code.
Follow these steps to create a Java project:
1.
Click File>New>Project to display the New Project wizard (see Figure 2).
This is actually a "wizard wizard" -- in other words, it's a wizard that lets
you choose which wizard to use (the New Project wizard, the New File
wizard, and so on).
Figure 2. New project wizard
Introduction to Java programming
Page 6 of 69
© Copyright IBM Corporation 1994, 2005. All rights reserved.

ibm.com/developerWorks
developerWorks®
2.
Make sure the Java Project wizard is selected and click Next.
3.
Enter whatever project name you want ("Intro" might work well), leave all
the defaults selected, and click Finish.
4.
At this point, Eclipse should ask you if it should switch to the Java
perspective. Click No.
You just created a Java project called Intro, which you should see in the Navigator
view in the upper-left corner of the screen. We didn't switch to the Java perspective
after creating the project because there's a better perspective to use for our current
purposes. Click the Open Perspective button on the tab in the upper-right corner of
the window, then select the Java Browsing perspective. This perspective shows you
what you need to see to create Java programs easily. As we create Java code, we'll
walk through a few more Eclipse features so you can learn how to create, modify,
and manage your code. Before we do that, though, we have to cover some basic
object-oriented programming concepts, which we'll do in the next section. Right now,
we'll wrap up this section by taking a look at some online Java documentation.
The Java API online
The Java application programming interface (API) is vast, so learning how to find
things is important. The Java platform is big enough to give you almost any tool you
need as a programmer. Learning to exploit the possibilities can take just as much
effort as learning the mechanics of the language.
If you go to Sun's Java documentation page (see Resources for the link), you'll see a
link there for API documentation for each version of the SDK. Follow the one for
version 1.4.2 to see what the documentation is like.
You'll see three frames in your browser:
• A list of built-in packages at the upper left
• A list of all classes at the lower left
• Details of what you selected on the right
Every class in the SDK is there. Select the class HashMap. One the right you'll see a
description of the class. At the top you'll see the name and package it's in, its class
hierarchy, its implemented interfaces (which are outside the scope of this tutorial),
and any direct subclasses it may have. After that, you'll find a description of the
class. Sometimes this description includes example usage, related links, style
recommendations, etc. After the description, you'll see a list of constructors, then a
list of all of the methods on the class, then all inherited methods, then detailed
descriptions of all methods. It's very complete, and there's an exhaustive index at
the top and bottom of the right-hand frame.
Many of the terms in the previous paragraph (like package ) are new to you at this
Introduction to Java programming
© Copyright IBM Corporation 1994, 2005. All rights reserved.
Page 7 of 69

developerWorks®
ibm.com/developerWorks
point. Don't worry. We'll cover all of them in detail. For now, the important thing to
know is that Java language documentation is available to you online.
Section 3. OOP concepts
What is an object?
Java is what's known as an object-oriented (OO) language, with which you can do
object-oriented programming (OOP). This is very different from procedural
programming, and can be a little strange for most non-OO programmers. The first
step is to understand what an object is, because that's what OOP is based on.
An object is a self-contained bunch of code that knows about itself and can tell other
objects about itself if they ask it questions that it understands. An object has data
members
(variables) and methods, which are the questions it knows how to answer
(even though they may not be worded as questions). The set of methods that an
object knows how to respond to is its interface. Some methods are open to the
public, meaning that another object can call (or invoke) them. That set of methods is
known as the object's public interface.
When one object invokes a method on an another object, that's known as sending a
message,
or a message send. That phrase is certainly OO terminology, but most
often in the Java world people tend to say, "Call this method" rather than, "Send this
message." In the next section, we'll look at a conceptual example that should make
this more clear.
Conceptual object example
Suppose we have a Person object. Each Person has a name, an age, a race, and a
gender. Each Person also knows how to speak and walk. One Person can ask
another Person how old it is, or could tell another Person to start (or stop) walking. In
programming terms, you would create a Person object and give it some variables
(like name and age). If you created a second Person object, it could ask the first how
old it is, or tell it to start walking. It would do those things by calling methods on the
first Person object. When we start writing code in the Java language, you'll see how
the language implements the concept of an object.
Generally, the concept of an object remains the same across the Java language and
other OO languages, though it gets implemented differently from language to
language. The concepts are universal. Because that's true, OO programmers,
regardless of the language they're programming in, tend to speak differently from
procedural programmers. Procedural programmers often talk about functions and
modules. OO programmers talk about objects, and they often talk about those
Introduction to Java programming
Page 8 of 69
© Copyright IBM Corporation 1994, 2005. All rights reserved.

ibm.com/developerWorks
developerWorks®
objects using personal pronouns. It's not uncommon to hear one OO programmer
say to another, "This Supervisor object here says to the Employee object, 'Give me
your ID,' because he needs it to assign tasks to the Employee."
Procedural programmers might think this way of talking is strange, but it's perfectly
natural for OO programmers. In their programming world, everything's an object
(with some notable exceptions in the Java language), and programs are objects
interacting (or "talking") with each other.
Fundamental OO principles
The concept of an object is critical for OOP, of course, as is the idea of objects
communicating with messages. But there are three other foundational principles you
need to understand.
You can remember the three fundamental OO principles with the acronym PIE:
P olymorphism
I nheritance
E ncapsulation
Those are fancy names, but the concepts aren't really all that difficult to understand.
In the next few sections, we'll talk about each in more detail, in reverse order.
Encapsulation
Remember, an object is a self-contained thing that contains data elements and
actions it can perform on those data elements. This is an implementation of a
principle known as information hiding. The idea is that an object knows about itself. If
another object wants facts about the first object, it has to ask. In OOP terms, it has
to send the object a message to ask for its age. In Java terms, it has to call a
method on the object that will return the age.
Encapsulation ensures that each object is distinct, and that programs are
conversations among objects. The Java language lets a programmer violate this
principle, but it's almost always a bad idea to do so.
Inheritance
When you were born, biologically speaking, you were a combination of the DNA of
your parents. You aren't exactly like either one of them, but you're similar to both.
OO has the same principle for objects. Think about a Person object again. Recall
that each person has a race. Not all People are the same race, but are they similar
to one another nonetheless? Sure! They aren't Horses, or Chimps, or Whales.
They're People. All People have certain things in common that make them distinct
Introduction to Java programming
© Copyright IBM Corporation 1994, 2005. All rights reserved.
Page 9 of 69

developerWorks®
ibm.com/developerWorks
from other kinds of animals. But they're also slightly different from each other. Is a
Baby the same as an Adult? Nope. They move and speak and differently. But a
Baby is certainly a Person.
In OO terms, Person and Baby are classes of things in the same hierarchy, and
(most likely) Baby inherits characteristics and behavior from its parent. We could say
that a particular Baby is a type of Person, or that Baby inherits from Person. It
doesn't work the other way around -- a Person isn't necessarily a Baby. Each Baby
object is an instance of the Baby class, and when we create a Baby object, we
instantiate it. Think of a class as the template for instances of that class. Generally,
what an object can do depends on what type of object it is -- or, in other words, what
class it's an instance of. Both a Baby and, say, an Adult are types of Person, but one
can have a job and one can't.
In Java terms, Person is a superclass of Baby and Adult, which are subclasses of
Person. Another related concept is the idea of abstraction. A Person is at a higher
level of abstraction than a Baby or an Adult. Both are types of Person, but they're
slightly different. Still, all Person objects have some things in common (like a name
and an age). Can you instantiate a Person? Not really. You either have a Baby or an
Adult. Person is what, in Java terms, you would call a abstract class. You can't have
an instance of Person directly. You'll have a Baby or an Adult, both of which are
types of Person, but realistic ones. Abstract classes are beyond the scope of this
tutorial, so this is the last we'll talk about them.
Now, think again about what it means for a Baby to "speak." We'll consider the
implications in the next panel.
Polymorphism
Does a Baby "speak" like an Adult? Of course not. A Baby makes noise, but it's not
necessarily recognizable words like an Adult uses. So, if I instantiate a Baby object
(saying "instantiate a Baby" means the same thing -- the word "object" is assumed)
and tell it to speak, it might coo or gurgle. One would hope that an Adult would be
coherent.
In the humanity hierarchy, we have Person at the top, with Baby and Adult beneath
it, as subclasses. All People can speak, so Baby and Adult can, too, but they do it
differently. A Baby gurgles and makes simple sounds. An Adult says words. That's
what polymorphism is: Objects doing things their own way.
How the Java language is (and is not) OO
As we'll see, the Java language lets you create first-class objects, but not everything
in the language is an object. That's quite different from some OO languages like
Smalltalk. Smalltalk is purely OO, meaning that everything in it is an object. The
Java language is a mixture of objects and non-objects. It also lets one object know
about the guts of another, if you as a programmer allow that to happen. That violates
the principle of encapsulation.
Introduction to Java programming
Page 10 of 69
© Copyright IBM Corporation 1994, 2005. All rights reserved.

Document Outline
  • ??
  • ??
    • ??
    • ??
    • ??
  • ??
    • ??
    • ??
    • ??
    • ??
    • ??
    • ??
  • ??
    • ??
    • ??
    • ??
    • ??
    • ??
    • ??
    • ??
  • ??
    • ??
    • ??
    • ??
  • ??
    • ??
    • ??
    • ??
    • ??
    • ??
    • ??
    • ??
    • ??
    • ??
  • ??
    • ??
    • ??
    • ??
    • ??
    • ??
    • ??
    • ??
    • ??
  • ??
    • ??
    • ??
    • ??
    • ??
    • ??
    • ??
    • ??
  • ??
    • ??
    • ??
    • ??
    • ??
    • ??
    • ??
    • ??
    • ??
    • ??
    • ??
    • ??
    • ??
  • ??
    • ??
    • ??
    • ??
    • ??
    • ??
    • ??
    • ??
  • ??
    • ??
    • ??
    • ??
    • ??
    • ??
    • ??
    • ??
    • ??
    • ??
    • ??
    • ??
    • ??
    • ??
    • ??
  • ??
    • ??
    • ??
    • ??
  • ??
    • ??
    • ??
    • ??
    • ??
    • ??
    • ??
    • ??
    • ??
    • ??
    • ??
  • ??
    • ??
  • ??
  • ??

Download
Introduction to Java programming

 

 

Your download will begin in a moment.
If it doesn't, click here to try again.

Share Introduction to Java programming to:

Insert your wordpress URL:

example:

http://myblog.wordpress.com/
or
http://myblog.com/

Share Introduction to Java programming as:

From:

To:

Share Introduction to Java programming.

Enter two words as shown below. If you cannot read the words, click the refresh icon.

loading

Share Introduction to Java programming as:

Copy html code above and paste to your web page.

loading