J Introtojava2 PDF
J Introtojava2 PDF
19 Aug 2010
In Part 1 of this tutorial, professional Java programmer J. Steven Perry introduced
the Java language syntax and libraries you need to write simple Java applications.
Part 2, still geared toward developers new to Java application development,
introduces the more-sophisticated programming constructs required for building
complex, real-world Java applications. Topics covered include exception handling,
inheritance and abstraction, regular expressions, generics, Java I/O, and Java
serialization.
Trademarks
Page 1 of 59
developerWorks
ibm.com/developerWorks
Objectives
The Java language is mature and sophisticated enough to help you accomplish
nearly any programming task. In this tutorial, you'll be introduced to features of the
Java language that you will need to handle complex programming scenarios,
including:
Exception handling
Inheritance and abstraction
Interfaces
Nested classes
Regular expressions
Generics
enum types
I/O
Serialization
Prerequisites
The content of this tutorial is geared toward programmers new to the Java language
who are unfamiliar with its more-sophisticated features. The tutorial assumes that
you have worked through "Introduction to Java programming, Part 1: Java language
basics" in order to:
Gain an understanding of the basics of OOP on the Java platform.
Set up the development environment for the tutorial examples.
Begin the programming project that you will continue developing in Part 2.
System requirements
Constructs for real-world applications
Copyright IBM Corporation 2010. All rights reserved.
Trademarks
Page 2 of 59
ibm.com/developerWorks
developerWorks
Overloading methods
When you create two methods with the same name but with different argument lists
(that is, different numbers or types of parameters), you have an overloaded method.
Overloaded methods are always in the same class. At run time, the Java Runtime
Environment (JRE; also known as the Java runtime) decides which variation of your
overloaded method to call based on the arguments that have been passed to it.
Suppose that Person needs a couple of methods to print an audit of its current
state. I'll call those methods printAudit(). Paste the overloaded method in
Listing 1 into the Eclipse editor view:
Trademarks
Page 3 of 59
developerWorks
ibm.com/developerWorks
In this case, you have two overloaded versions of printAudit(), and one actually
uses the other. By providing two versions, you give the caller a choice of how to print
an audit of the class. Depending on the parameters that are passed, the Java
runtime will call the correct method.
Two rules of method overloading
Remember these two important rules when using overloaded methods:
You can't overload a method just by changing its return type.
You can't have two methods with the same parameter list.
If you violate these rules, the compiler will give you an error.
Overriding methods
When a subclass of another class provides its own implementation of a method
defined on a parent class, that's called method overriding. In order to see how
method overriding is useful, you need to do some work on your Employee class.
Once you have it set up, I'll be able to show you where method overriding comes in
handy.
Employee: A subclass of Person
Recall from Part 1 of this tutorial that Employee might be a subclass (or child) of
Person that has some additional attributes:
Taxpayer identification number
Employee number
Trademarks
Page 4 of 59
ibm.com/developerWorks
developerWorks
Hire date
Salary
To declare such a class in a file called Employee.java, right-click the
com.makotogroup.intro package in Eclipse. Choose New > Class..., and the
New Java Class dialog box will open, as shown in Figure 1:
Figure 1. New Java Class dialog
Type Employee as the name of the class and Person as its superclass, then click
Finish. You will see the Employee class in an edit window. You don't explicitly need
to declare a constructor, but go ahead and implement both constructors anyway.
Trademarks
Page 5 of 59
developerWorks
ibm.com/developerWorks
First, make sure the Employee class edit window has the focus, then go to Source
> Generate Constructors from Superclass..., and you'll see a dialog that looks like
Figure 2:
Figure 2. Generate Constructors from Superclass dialog
Check both constructors (as shown in Figure 2), and click OK. Eclipse will generate
the constructors for you. You should now have an Employee class like the one in
Listing 2:
Listing 2. The new, improved Employee class
package com.makotogroup.intro;
public class Employee extends Person {
Trademarks
Page 6 of 59
ibm.com/developerWorks
developerWorks
public Employee() {
super();
// TODO Auto-generated constructor stub
}
public Employee(String name, int age, int height, int weight,
String eyeColor, String gender) {
super(name, age, height, weight, eyeColor, gender);
// TODO Auto-generated constructor stub
}
}
Trademarks
Page 7 of 59
developerWorks
ibm.com/developerWorks
Trademarks
Page 8 of 59
ibm.com/developerWorks
developerWorks
Notice the call to super.printAudit(). What you're doing here is asking the
(Person) superclass to exhibit its behavior for printAudit(), and then you
augment it with Employee-type printAudit() behavior.
The call to super.printAudit() doesn't need to be first; it just seemed like a
good idea to print those attributes first. In fact, you don't need to call
super.printAudit() at all. If you don't call it, you must either format the
attributes from Person yourself (in the Employee.printAudit() method), or
exclude them altogether. Making the call to super.printAudit(), in this case, is
easier.
Class members
The variables and methods you have on Person and Employee are instance
variables and methods. To use them you either must instantiate the class you need
or have a reference to the instance. Every object instance has variables and
methods, and for each one the exact behavior (for example, what's generated by
calling printAudit()) will be different, because it is based on the state of the
object instance.
Classes themselves can also have variables and methods, which are called class
members. You declare class members with the static keyword introduced in Part
1 of this tutorial. The differences between class members and instance members
are:
Every instance of a class shares a single copy of a class variable.
You can call class methods on the class itself, without having an instance.
Instance methods can access class variables, but class methods cannot
access instance variables.
Class methods can access only class variables.
Adding class variables and methods
When does it make sense to add class variables and methods? The best rule of
thumb is to do so rarely, so that you don't overuse them. That said, it's a good idea
to use class variables and methods:
To declare constants that any instance of the class can use (and whose
value is fixed at development time).
To track "counters" of instances of the class.
On a class with utility methods that don't ever need an instance of the
Trademarks
Page 9 of 59
developerWorks
ibm.com/developerWorks
Class variables
To create a class variable, use the static keyword when you declare it:
accessSpecifier static variableName [= initialValue];
Note: The square brackets here indicate that their contents are optional. They are
not part of the declaration syntax.
The JRE creates space in memory to store each of a class's instance variables for
every instance of that class. In contrast, the JRE creates only a single copy of each
class variable, regardless of the number of instances. It does so the first time the
class is loaded (that is, the first time it encounters the class in a program). All
instances of the class will share that single copy of the variable. That makes class
variables a good choice for constants that all instances should be able to use.
For example, you declared the Gender attribute of Person to be a String, but you
didn't put any constraints around it. Listing 4 shows a common use of class
variables:
Listing 4. Using class variables
public class Person {
//. . .
public static final String GENDER_MALE = "MALE";
public static final String GENDER_FEMALE = "FEMALE";
// . . .
public static void main(String[] args) {
Person p = new Person("Joe Q Author", 42, 173, 82, "Brown", GENDER_MALE);
// . . .
}
//. . .
}
Declaring constants
Typically, constants are:
Named in all uppercase.
Named as multiple words, separated by underscores.
Declared final (so that their values cannot be modified).
Declared with a public access specifier (so that they can be accessed
by other classes that need to reference their values by name).
Trademarks
Page 10 of 59
ibm.com/developerWorks
developerWorks
In Listing 4, to use the constant for MALE in the Person constructor call, you would
simply reference its name. To use a constant outside of the class, you'd preface it
with the name of the class where it was declared, like this:
String genderValue = Person.GENDER_MALE;
Class methods
If you've been following along since Part 1, you've already called the static method
Logger.getLogger() several times whenever you've retrieved a Logger
instance to write some output to the console. Notice, though, that you didn't need an
instance of Logger to do this; instead, you referenced the Logger class itself. This
is the syntax for making a class method call. As with class variables, the static
keyword identifies Logger (in this example) as a class method. Class methods are
also sometimes called static methods for this reason.
Using class methods
Now you'll combine what you've learned about static variables and methods to
create a static method on Employee.You'll declare a private static final
variable to hold a Logger, which all instances will share, and which will be
accessible by calling getLogger() on the Employee class. Listing 5 shows how:
Listing 5. Creating a class (or static) method
public class Employee extends Person {
private static final Logger logger = Logger.getLogger(Employee.class.getName());
//. . .
public static Logger getLogger() {
return logger;
}
}
Trademarks
Page 11 of 59
developerWorks
ibm.com/developerWorks
Comparing objects
The Java language provides two ways to compare objects:
The == operator
The equals() method
Comparing objects with ==
The == syntax compares objects for equality such that a == b returns true only if
a and b have the same value. For objects, this means that the two refer to the same
object instance. For primitives, it means that the values are identical. Consider the
example in Listing 6:
Listing 6. Comparing objects with ==
int int1 = 1;
int int2 = 1;
l.info("Q: int1 == int2?
If you run the Listing 6 code inside Eclipse, the output should be:
Apr 19, 2010 5:30:10 AM com.makotogroup.intro.Employee
INFO: Q: int1 == int2?
A: true
Apr 19, 2010 5:30:10 AM com.makotogroup.intro.Employee
INFO: Q: Integer1 == Integer2?
A: true
Apr 19, 2010 5:30:10 AM com.makotogroup.intro.Employee
INFO: Q: Integer1 == Integer2?
A: false
Apr 19, 2010 5:30:10 AM com.makotogroup.intro.Employee
INFO: Q: Employee1 == Employee2? A: false
main
main
main
main
In the first case in Listing 6, the values of the primitives are the same, so the ==
operator returns true. In the second case, the Integer objects refer to the same
instance, so again == returns true. In the third case, even though the Integer
objects wrap the same value, == returns false because integer1 and integer2
refer to different objects. Based on this, it should be clear why employee1 ==
Trademarks
Page 12 of 59
ibm.com/developerWorks
developerWorks
integer1 =
integer2 =
l.info("Q:
l.info("Q:
new Integer(integer1);
new Integer(integer2);
integer1 == integer2?
integer1.equals(integer2)?
main
main
main
main
main
main
Trademarks
Page 13 of 59
developerWorks
ibm.com/developerWorks
true if == returns true; but notice what happens in the second case, where you
create separate objects that both wrap the value 1: == returns false because
integer1 and integer2 refer to different objects; but equals() returns true.
The writers of the JDK decided that for Integer, the meaning of equals() would
be different from the default (which is to compare the object references to see if they
refer to the same object), and would instead return true in cases in which the
underlying int value is the same.
For Employee, you did not override equals(), so the default behavior (of using
==) returns what you would expect, given that employee1 and employee2 do in
fact refer to different objects.
Basically, this means that for any object you write, you can define what equals()
means as appropriate for the application you are writing.
Overriding equals()
You can define what equals() means to your application's objects by overriding
the default behavior of Object.equals(). Again, you can use Eclipse to do this.
Make sure Employee has the focus in your Eclipse IDE's Source window, then go to
Source > Override/Implement Methods. The dialog box in Figure 4 will appear:
Figure 4. Override/Implement Methods dialog
Trademarks
Page 14 of 59
ibm.com/developerWorks
developerWorks
You've used this dialog before, but in this case you want to implement the
Object.equals() superclass method. So, find Object in the list, check the
equals(Object) method, and click OK. Eclipse will generate the correct code and
place it in your source file.
It makes sense that the two Employee objects are equal if the states of those
objects are equal. That is, they're equal if their values last name, first name, age
are the same.
Autogenerating equals()
Eclipse can generate an equals() method for you based on the instance variables
(attributes) you've defined for a class. Because Employee is a subclass of Person,
you'll first generate equals() for Person. In Eclipse's Project Explorer view,
right-click Person and choose Generate hashCode() and equals() to bring up the
dialog box shown in Figure 5:
Figure 5. Generate hashCode() and equals() dialog
Trademarks
Page 15 of 59
developerWorks
ibm.com/developerWorks
Select all attributes (as shown in Figure 5) and click OK. Eclipse will generate an
equals() method that looks like the one in Listing 8:
Listing 8. An equals() method generated by Eclipse
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (age != other.age)
return false;
if (eyeColor == null) {
if (other.eyeColor != null)
return false;
} else if (!eyeColor.equals(other.eyeColor))
return false;
if (gender == null) {
Trademarks
Page 16 of 59
ibm.com/developerWorks
developerWorks
if (other.gender != null)
return false;
} else if (!gender.equals(other.gender))
return false;
if (height != other.height)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (weight != other.weight)
return false;
return true;
}
Don't worry about hashCode() for now you can keep it or delete it. The
equals() method generated by Eclipse looks complicated, but what it does is
pretty simple: if the object passed in is the same object as the one in Listing 8, then
equals() will return true. If the object passed in is null, it will return false.
After that, the method checks to see if the Class objects are the same (meaning the
passed-in object must be a Person object). If that's true, then each attribute value of
the object passed in is checked to see if it matches value-for-value with the state of
the given Person instance. If the attribute values are null (meaning missing) then
the equals() will check as many as it can, and if those match, the objects will be
considered equal. You may not want this behavior for every program, but it works for
most purposes.
Exercise: Generate an equals() for Employee
Try following the steps in Autogenerating equals() to generate an equals() for
Employee. Once you have your generated equals(), add the following code
above it:
public static void main(String[] args) {
Logger l = Logger.getLogger(Employee.class.getName());
Employee employee1 = new Employee();
employee1.setName("J Smith");
Employee employee2 = new Employee();
employee2.setName("J Smith");
l.info("Q: employee1 == employee2?
A: " + (employee1 == employee2));
l.info("Q: employee1.equals(employee2)? A: " + employee1.equals(employee2));
}
If you run the code, you should see the following output:
Apr 19, 2010 5:26:50 PM com.makotogroup.intro.Employee main
INFO: Q: employee1 == employee2?
A: false
Apr 19, 2010 5:26:50 PM com.makotogroup.intro.Employee main
INFO: Q: employee1.equals(employee2)? A: true
Trademarks
Page 17 of 59
developerWorks
ibm.com/developerWorks
In this case, a match on Name alone was enough to convince equals() that the two
objects were equal. Feel free to add more attributes to this example and see what
you get.
Exercise: Override toString()
Remember the printAudit() method from the beginning of this section? If you
thought it was working a little too hard, you were right! Formatting the state of an
object into a String is such a common pattern that the designers of the Java
language built it right into Object itself, in a method called (no surprise)
toString(). The default implementation of toString() is not very useful, but
every object has one. In this exercise, you'll make the default toString() a little
more useful.
If you suspect that Eclipse can generate a toString() method for you, you are
correct. Go back into your Project Explorer and right-click the Employee class, then
choose Source > Generate toString().... You'll see a dialog box similar to the one in
Figure 5. Choose all attributes and click OK. The code generated by Eclipse for
Employee is shown in Listing 9:
Listing 9. A toString() method generated by Eclipse
@Override
public String toString() {
return "Employee [employeeNumber=" + employeeNumber + ", salary=" + salary
+ ", taxpayerIdentificationNumber=" + taxpayerIdentificationNumber
+ "]";
}
The code Eclipse generates for toString doesn't include the superclass's
toString() (Employee's superclass being Person). You can fix that in a flash,
using Eclipse, with this override:
@Override
public String toString() {
return super.toString() +
"Employee [employeeNumber=" + employeeNumber + ", salary=" + salary
+ ", taxpayerIdentificationNumber=" + taxpayerIdentificationNumber
+ "]";
}
Trademarks
Page 18 of 59
ibm.com/developerWorks
developerWorks
toString() now does the heavy lifting of formatting the object's current state, and
you simply stuff what it returns into the StringBuilder and return.
I recommend always implementing toString() in your classes, if only for support
purposes. It's virtually inevitable that at some point you'll want to see what an
object's state is while your application is running, and toString() is a great hook
for doing that.
Section 3. Exceptions
No program ever works 100 percent of the time, and the designers of the Java
language knew this. In this section, learn about the Java platform's built-in
mechanisms for handling situations where your code doesn't work exactly as
planned.
Exception-handling basics
An exception is an event that occurs during program execution that disrupts the
normal flow of the program's instructions. Exception handling is an essential
technique of Java programming. In essence, you wrap your code in a try block
(which means "try this and let me know if it causes an exception"), and use it to
catch various types of exceptions.
To get started with exception handling, take a look at the code in Listing 10:
Listing 10. Do you see the error?
// . . .
public class Employee extends Person {
// . . .
private static Logger logger;// = Logger.getLogger(Employee.class.getName());
public static void main(String[] args) {
Employee employee1 = new Employee();
employee1.setName("J Smith");
Employee employee2 = new Employee();
employee2.setName("J Smith");
logger.info("Q: employee1 == employee2?
A: " + (employee1 == employee2));
logger.info("Q: employee1.equals(employee2)? A: " + employee1.equals(employee2));
}
Notice that the initializer for the static variable holding the Logger reference has
been commented out. Run this code and you'll get the following output:
Trademarks
Page 19 of 59
developerWorks
ibm.com/developerWorks
This output is telling you that you are trying to reference an object that isn't there,
which is a pretty serious development error. Fortunately, you can use try and
catch blocks to catch it (along with a little help from finally).
try, catch, and finally
Listing 11 shows the buggy code from Listing 10 cleaned up with the standard code
blocks for exception handling try, catch, and finally:
Listing 11. Catching an exception
// . . .
public class Employee extends Person {
// . . .
private static Logger logger;// = Logger.getLogger(Employee.class.getName());
public static void main(String[] args) {
try {
Employee employee1 = new Employee();
employee1.setName("J Smith");
Employee employee2 = new Employee();
employee2.setName("J Smith");
logger.info("Q: employee1 == employee2?
A: " + (employee1 == employee2));
logger.info("Q: employee1.equals(employee2)? A: " + employee1.equals(employee2));
} catch (NullPointerException npe) {
// Handle...
System.out.println("Yuck! Outputting a message with System.out.println() " +
"because the developer did something dumb!");
} finally {
// Always executes
}
}
Together, the try, catch, and finally blocks form a net for catching exceptions.
First, the try statement wraps code that might throw an exception. If it does,
execution drops immediately to the catch block, or exception handler. When all the
trying and catching is done, execution continues to the finally block, whether or
not an exception has been thrown. When you catch an exception, you can try to
recover gracefully from it, or you can exit the program (or method).
In Listing 11, the program recovers from the error, then prints out a message to
report what happened.
Trademarks
Page 20 of 59
ibm.com/developerWorks
developerWorks
Trademarks
Page 21 of 59
developerWorks
ibm.com/developerWorks
In this section, you will continue building up Person as a Java application. Along the
way, you'll get a better idea of how an object, or collection of objects, evolves into an
application.
Create a driver class in Eclipse using the same procedure you used to create
Person and Employee. Name the class HumanResourcesApplication, being
sure to select the option to add a main() method to the class. Eclipse will generate
the class for you.
Add some code to your new main() so that it looks like this:
public class Person {
. . .
private final Logger log = Logger.getLogger(Person.class);
. . .
public static void main(String[] args) {
Employee e = new Employee();
e.setName("J Smith");
e.setEmployeeNumber("0001");
e.setTaxpayerIdentificationNumber("123-45-6789");
e.printAudit(log);
}
. . .
}
Trademarks
Page 22 of 59
ibm.com/developerWorks
developerWorks
should see this output (with the backslashes here indicating a line continuation):
Apr 29, 2010 6:45:17 AM com.makotogroup.intro.Person printAudit
INFO: Person [age=0, eyeColor=null, gender=null, height=0, name=J Smith,\
weight=0]Employee [employeeNumber=0001, salary=null,\
taxpayerIdentificationNumber=123-45-6789]
That's really all there is to creating a simple Java application. In the next section,
you'll begin looking at some of the syntax and libraries that will help you develop
more-complex applications.
Section 5. Inheritance
You've encountered examples of inheritance a few times already in this tutorial. This
section reviews some of Part 1's material on inheritance and explains in more detail
how inheritance works including the inheritance hierarchy, constructors and
inheritance, and inheritance abstraction.
Trademarks
Page 23 of 59
developerWorks
ibm.com/developerWorks
The Person class in Listing 12 implicitly inherits from Object. Because that's
assumed for every class, you don't need to type extends Object for every class
you define. But what does it mean to say that a class inherits from its superclass? It
simply means that Person has access to the exposed variables and methods in its
superclasses. In this case, Person can see and use Object's public methods and
variables and Object's protected methods and variables.
The Employee inheritance graph implies that Employee has access to all public
and protected variables and methods in Person (because it directly extends it), as
well as Object (because it actually extends that class, too, though indirectly).
However, because Employee and Person are in the same package, Employee
also has access to the package-private (sometimes called friendly) variables and
methods in Person.
To go one step deeper into the class hierarchy, you could create a third class that
extends Employee:
Trademarks
Page 24 of 59
ibm.com/developerWorks
developerWorks
In the Java language, any class can have at most one superclass, but a class can
have any number of subclasses. That is the most important thing to remember about
inheritance hierarchy in the Java language.
Every class has at least one constructor, and if you don't explicitly define a
constructor for your class, the compiler will generate one for you, called the default
constructor. The preceding class definition and this one are identical in how they
function:
public class Person {
}
Trademarks
Page 25 of 59
developerWorks
ibm.com/developerWorks
You would probably never want to initialize a new Employee object this way,
however. Until you get more comfortable with object-oriented concepts, and Java
syntax in general, it's a good idea to implement superclass constructors in
subclasses if you think you will need them, and invoke them homogeneously. Listing
14 defines a constructor in Employee that looks like the one in Person so that they
match up. It's much less confusing from a maintenance standpoint.
Listing 14. Invoking a superclass homogeneously
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
}
// Meanwhile, in Employee.java
public class Employee extends Person {
public Employee(String name) {
super(name);
}
}
Declaring a constructor
The first thing a constructor does is invoke the default constructor of its immediate
superclass, unless you on the first line of code in the constructor invoke a
different constructor. For example, these two declarations are functionally identical,
so pick one:
public class Person {
public Person() {
}
}
// Meanwhile, in Employee.java
public class Employee extends Person {
public Employee() {
}
}
Or:
Trademarks
Page 26 of 59
ibm.com/developerWorks
developerWorks
No-arg constructors
If you provide an alternate constructor, you must explicitly provide the default
constructor, or it is not available. For example, the following code would give you a
compile error:
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
}
// Meanwhile, in Employee.java
public class Employee extends Person {
public Employee() {
}
}
You will see this idiom frequently, where one constructor delegates to another,
passing in some default value if that constructor is invoked. It's also a great way to
Trademarks
Page 27 of 59
developerWorks
ibm.com/developerWorks
add a new constructor to a class while minimizing impact on code that already uses
an older constructor.
Constructor access levels
Constructors can have any access level you want, and certain rules of visibility
apply. Table 1 summarizes the rules of constructor access:
Table 1. Constructor access rules
Constructor access modifier
Description
public
protected
No modifier (package-private)
private
You may be able to think of use cases where constructors would be declared
protected or even package-private, but how is a private constructor useful? I've
used private constructors when I didn't want to allow direct creation of an object
through the new keyword when implementing, say, the Factory pattern (see
Resources). In that case, a static method would be used to create instances of the
class, and that method, being included in the class itself, would be allowed to invoke
the private constructor:
Trademarks
Page 28 of 59
ibm.com/developerWorks
developerWorks
higher up the inheritance hierarchy than the current class. When you move variables
or methods from a subclass to a superclass, you say you are abstracting those
members. The main reason for doing this is to reuse common code by pushing it as
far up the hierarchy as possible. Having common code in one place makes it easier
to maintain.
Abstract classes and methods
There are times when you will want to create classes that only serve as abstractions
and do not necessarily ever need to be instantiated. Such classes are called
abstract classes. By the same token, you will find that there are times when certain
methods need to be implemented differently for each subclass that implements the
superclass. Such methods are abstract methods. Here are some basic rules for
abstract classes and methods:
Any class can be declared abstract.
Abstract classes cannot be instantiated.
An abstract method cannot contain a method body.
Any class with an abstract method must be declared abstract.
Using abstraction
Suppose you don't want to allow the Employee class to be instantiated directly. You
simply declare it using the abstract keyword, and you're done:
public abstract class Employee extends Person {
// etc.
}
Trademarks
Page 29 of 59
developerWorks
ibm.com/developerWorks
Assignments: Classes
When assigning a reference from one class to a variable of a type belonging to
another class, you can do so, but there are rules. Let's look at this example:
Manager m = new Manager();
Employee e = new Employee();
Person p = m; // okay
p = e; // still okay
Employee e2 = e; // yep, okay
e = m; // still okay
e2 = p; // wrong!
The destination variable must be of a supertype of the class belonging to the source
reference, or the compiler will give you an error. Basically, whatever is on the right
Trademarks
Page 30 of 59
ibm.com/developerWorks
developerWorks
side of the assignment must be a subclass or the same class as the thing on the left.
If not, it's possible for assignments of objects with different inheritance graphs (such
as Manager and Employee) to be assigned to a variable of the wrong type.
Consider this example:
Manager m = new Manager();
Person p = m; // so far so good
Employee e = m; // okay
Employee e = p; // wrong!
Section 6. Interfaces
In this section, begin learning about interfaces and start using them in your Java
code.
Defining an interface
An interface is a named set of behaviors (and/or constant data elements) for which
an implementer must provide code. An interface specifies what behavior the
implementation provides, but not how it is accomplished.
Defining an interface is straightforward:
public interface interfaceName {
returnType methodName( argumentList );
}
An interface declaration looks like a class declaration, except that you use the
interface keyword. You can name the interface anything you want to (subject to
language rules), but by convention interface names look like class names.
Methods defined in an interface have no method body. The implementer of the
interface is responsible for providing the method body (just as with abstract
methods).
You define hierarchies of interfaces, just as you do for classes, except that a single
class can implement as many interfaces as it wants to. (Remember, a class can
extend only one class.) If one class extends another and implements interface(s),
Constructs for real-world applications
Copyright IBM Corporation 2010. All rights reserved.
Trademarks
Page 31 of 59
developerWorks
ibm.com/developerWorks
then the interfaces are listed after the extended class, like this:
public class Manager extends Employee implements BonusEligible, StockOptionRecipient {
// Etc...
}
Marker interfaces
An interface does not need to have any body at all. In fact, the following definition is
perfectly acceptable:
public interface BonusEligible {
}
Generally speaking, such interfaces are called marker interfaces, because they mark
a class as implementing that interface but offer no special explicit behavior.
Once you know all that, actually defining an interface is easy:
public interface StockOptionRecipient {
void processStockOptions(int numberOfOptions, BigDecimal price);
}
Implementing interfaces
To use an interface, you implement it, which simply means providing a method body,
which in turn provides the behavior to fulfill the interface's contract. You do that with
the implements keyword:
public class className extends superclassName implements interfaceName {
// Class Body
}
Trademarks
Page 32 of 59
ibm.com/developerWorks
developerWorks
When you implement the interface, you provide behavior for the method(s) on the
interface. You must implement the methods with signatures that match the ones on
the interface, with the addition of the public access modifier.
Generating interfaces in Eclipse
Eclipse can easily generate the correct method signature for you if you decide one of
your classes should implement an interface. Just change the class signature to
implement the interface. Eclipse puts a red squiggly line under the class, flagging it
to be in error because the class doesn't provide the method(s) on the interface. Click
the class name with your mouse, press Ctrl + 1, and Eclipse will suggest "quick
fixes" for you. Of these, choose Add Unimplemented Methods, and Eclipse will
generate the methods for you, placing them at the bottom of the source file.
An abstract class can declare that it implements a particular interface, but it isn't
required to implement all of the methods on that interface. This is because abstract
classes aren't required to provide implementations for all of the methods they claim
to implement. However, the first concrete class (that is, the first one that can be
instantiated) must implement all methods the hierarchy does not.
Using interfaces
An interface defines a new reference data type, which means you can refer to an
interface anywhere you would refer to a class. This includes when you declare a
reference variable, or cast from one type to another, as shown in Listing 16.
Listing 16. Assigning a new Manager instance to a StockOptionEligible
reference
public static void main(String[] args) {
StockOptionEligible soe = new Manager();// perfectly valid
calculateAndAwardStockOptions(soe);
calculateAndAwardStockOptions(new Manager());// works too
}
. . .
public static void calculateAndAwardStockOptions(StockOptionEligible soe) {
BigDecimal reallyCheapPrice = BigDecimal.valueOf(0.01);
int numberOfOptions = 10000;
soe.processStockOptions(numberOfOptions, reallyCheapPrice);
}
Assignments: Classes
Trademarks
Page 33 of 59
developerWorks
ibm.com/developerWorks
Because Employee is supertype of Manager, this might at first seem okay, but it is
not. Because Manager is a specialization of Employee, it is *different* and in this
particular case implements an interface that Employee does not.
Assignments such as these follow the rules of assignment we saw in Inheritance.
And just like with classes, you may only assign an interface reference to a variable
of the same type or a superinterface type.
Just like member variables and methods, Java classes can also be defined at any
scope including public, private, or protected. Nested classes can be useful
when you want to handle internal processing within your class in an object-oriented
fashion, but this functionality is limited to the class where you need it.
Typically, you'll use a nested class for cases where you need a class that is tightly
Trademarks
Page 34 of 59
ibm.com/developerWorks
developerWorks
coupled with the class in which it is defined. A nested class has access to the private
data within its enclosing class, but this carries with it some side-effects that are not
obvious when you start working with nested (or inner) classes.
Trademarks
Page 35 of 59
developerWorks
ibm.com/developerWorks
}
//
public static void main(String[] args) {
Manager.DirectReports dr = new Manager.DirectReports();// This won't work!
}
The code in Listing 17 doesn't work, and you're probably wondering why. The
problem (and also its solution) lies with the way DirectReports is defined within
Manager, and with the rules of scope.
The rules of scope, revisited
If you had a member variable of Manager, you would expect the compiler to require
you to have a reference to a Manager object before you could reference it, right?
Well, the same applies to DirectReports, at least as you defined it in Listing 17.
To create an instance of a public nested class, you use a special version of the new
operator. Combined with a reference to some enclosing instance of an outer class,
new allows you to create an instance of the nested class:
public class Manager extends Employee {
public Manager() {
}
. . .
private class DirectReports {
. . .
}
}
// Meanwhile, in another method somewhere...
public static void main(String[] args) {
Manager manager = new Manager();
Manager.DirectReports dr = manager.new DirectReports();
}
Note that the syntax calls for a reference to the enclosing instance, plus a dot and
the new keyword, followed by the class you want to create.
Trademarks
Page 36 of 59
ibm.com/developerWorks
developerWorks
}
}
// Meanwhile, in another method somewhere...
public static void main(String[] args) {
Manager.ManagerComparator mc = new Manager.ManagerComparator();
. . .
}
In this case, you don't need an enclosing instance. Static inner classes act like their
regular Java class counterparts, and they should really only be used when you need
to couple a class tightly with its definition. Clearly, in the case of a utility class like
ManagerComparator, creating an external class is unnecessary and potentially
clutters up your code base. Defining such classes as static inner classes is the way
to go.
Trademarks
Page 37 of 59
developerWorks
ibm.com/developerWorks
Trademarks
Page 38 of 59
ibm.com/developerWorks
developerWorks
A regex pattern describes the structure of the string that the expression will try to
find in an input string. This is where regular expressions can look a bit strange. Once
you understand the syntax, though, it becomes easier to decipher. Table 2 lists
some of the most common regex constructs that you will use in pattern strings:
Table 2. Common regex constructs
Regex construct
Any character
[]
\d
\D
\s
\S
\w
\W
The first few constructs are called quantifiers, because they quantify what comes
before them. Constructs like \d are predefined character classes. Any character that
doesn't have special meaning in a pattern is a literal and matches itself.
Pattern matching
Armed with the pattern syntax in Table 2, you can work through the simple example
in Listing 19, using the classes in the Java Regular Expressions API:
Listing 19. Pattern matching with regex
Pattern pattern = Pattern.compile("a.*string");
Matcher matcher = pattern.matcher("a string");
boolean didMatch = matcher.matches();
Logger.getAnonymousLogger().info (didMatch);
int patternStartIndex = matcher.start();
Logger.getAnonymousLogger().info (patternStartIndex);
int patternEndIndex = matcher.end();
Logger.getAnonymousLogger().info (patternEndIndex);
Trademarks
Page 39 of 59
developerWorks
ibm.com/developerWorks
You could search it for a.*string and get a match if you use lookingAt(). But if
you use matches(), it would return false, because there's more to the string than
just what's in the pattern.
Trademarks
Page 40 of 59
ibm.com/developerWorks
developerWorks
You could search for wiki words in this string with a regex pattern like this:
[A-Z][a-z]*([A-Z][a-z]*)+
Run this code, and you should see the three wiki words in your console.
Replacing strings
Searching for matches is useful, but you also can manipulate strings once you find a
match for them. You can do that by replacing matched strings with something else,
just as you might search for some text in a word-processing program and replace it
with other text. Matcher has a couple of methods for replacing string elements:
replaceAll() replaces all matches with a specified string.
replaceFirst() replaces only the first match with a specified string.
Trademarks
Page 41 of 59
developerWorks
ibm.com/developerWorks
This code finds wiki words, as before. When the Matcher finds a match, it replaces
the wiki word text with its replacement. When you run this code, you should see the
following on your console:
Before: Here is WikiWord followed by AnotherWikiWord, then SomeWikiWord.
After: Here is replacement followed by replacement, then replacement.
Run this code and you should get the following console output:
Before: Here is a WikiWord followed by AnotherWikiWord, then SomeWikiWord.
Trademarks
Page 42 of 59
ibm.com/developerWorks
developerWorks
Section 9. Generics
The introduction of generics in JDK 5 marked a huge leap forward for the Java
language. If you've used C++ templates, you'll find that generics in the Java
language are similar, but not exactly the same. If you haven't used C++ templates,
then don't worry: This section offers a high-level introduction to generics in the Java
language.
Trademarks
Page 43 of 59
developerWorks
ibm.com/developerWorks
As you can see, the ArrayList is heterogeneous: it contains two String types
and one Integer type. Before JDK 5 there was nothing in the Java language to
constrain this behavior, which caused many coding mistakes. In Listing 21, for
example, everything is looking good so far. But what about accessing the elements
of the ArrayList, which Listing 22 tries to do?
Listing 22. An attempt to access elements in ArrayList
ArrayList arrayList = new ArrayList();
arrayList.add("A String");
arrayList.add(new Integer(10));
arrayList.add("Another String");
// So far, so good.
*processArrayList(arrayList);
*// In some later part of the code...
private void processArrayList(ArrayList theList) {
for (int aa = 0; aa < theList.size(); aa++) {
// At some point, this will fail...
String s = (String)theList.get(aa);
}
}
Without prior knowledge of what's in the ArrayList, you either must check the
element you want to access to see if you can handle its type, or face a possible
ClassCastException.
With generics, you can specify the type of item that went in the ArrayList. Listing
23 shows how:
Listing 23. A second attempt, using generics
Trademarks
Page 44 of 59
ibm.com/developerWorks
developerWorks
This syntax works for any type of object that is Iterable (that is, implements the
Iterable interface).
Parameterized classes
Parameterized classes really shine when it comes to collections, so that's how you'll
look at them. Consider the (real) List interface. It represents an ordered collection
of objects. In the most common use case, you add items to the List and then
access those items either by index or by iterating over the List.
If you're thinking about parameterizing a class, consider whether the following
criteria apply:
A core class is at the center of some kind of wrapper: that is, the "thing" at
the center of the class might apply widely, and the features (attributes, for
example) surrounding it are identical.
Common behavior: you do pretty much the same operations regardless of
the "thing" at the center of the class.
Applying these two criteria, it's pretty obvious that a collection fits the bill:
Trademarks
Page 45 of 59
developerWorks
ibm.com/developerWorks
The E, which stands for Element, is the "thing" I mentioned earlier. The
concreteListClass is the class from the JDK you are instantiating. The JDK
includes several List<E> implementations, but you'll use ArrayList<E>. Another
way you might see a generic class discussed is Class<T>, where T stands for
Type. When you see E in Java code, it's usually referring to a collection of some
kind. And when you see T, it's denoting a parameterized class.
So, to create an ArrayList of, say, java.lang.Integer, you'd do this:
List<Integer> listOfIntegers = new ArrayList<Integer>();
Trademarks
Page 46 of 59
ibm.com/developerWorks
developerWorks
return e;
else
return null;
}
public int size() {
return backingStore.size();
}
public void clear() {
backingStore.clear();
}
}
SimpleList can be parameterized with any Object subclass. To create and use a
SimpleList of, say, java.math.BigDecimal objects, you'd do this:
public static void main(String[] args) {
SimpleList<BigDecimal> sl = new SimpleList<BigDecimal>();
sl.add(BigDecimal.ONE);
log.info("SimpleList size is : " + sl.size());
sl.add(BigDecimal.ZERO);
log.info("SimpleList size is : " + sl.size());
sl.clear();
log.info("SimpleList size is : " + sl.size());
}
Enum types
In JDK 5, a new data type was added to the Java language, called enum. Not to be
confused with java.util.Enumeration, enum represents a set of constant
objects that are all related to a particular concept, each of which represents a
different constant value in that set. Before enum was introduced to the Java
language, you would have defined a set of constant values for a concept (say,
gender) like so:
public class Person {
public static final String MALE = "male";
public static final String FEMALE = "female";
}
Whatever code needed to reference that constant value would have been written
something like this:
Trademarks
Page 47 of 59
developerWorks
ibm.com/developerWorks
That just scratches the surface of what you can do with enums. In fact, enums are
much like classes, so they can have constructors, attributes, and methods:
package com.makotogroup.intro;
public enum Gender {
MALE("male"),
FEMALE("female");
private String displayName;
private Gender(String displayName) {
this.displayName = displayName;
}
public String getDisplayName() {
return this.displayName;
}
}
One difference between a class and an enum is that an enum's constructor must be
declared private, and it cannot extend (or inherit from) other enums. However, an
enum can implement an interface.
An enum implements an interface
Suppose you define an interface, Displayable:
package com.makotogroup.intro;
public interface Displayable {
public String getDisplayName();
}
Your Gender enum could implement this interface (as well as any other enum that
needed to produce a friendly display name), like so:
Trademarks
Page 48 of 59
ibm.com/developerWorks
developerWorks
package com.makotogroup.intro;
public enum Gender implements Displayable {
MALE("male"),
FEMALE("female");
private String displayName;
private Gender(String displayName) {
this.displayName = displayName;
}
@Override
public String getDisplayName() {
return this.displayName;
}
}
Files
Of all the data sources available to your Java applications, files are the most
common and often the most convenient. If you want to read a file in your Java
application, you must use streams that parse its incoming bytes into Java language
types.
java.io.File is a class that defines a resource on your file system and
represents that resource in an abstract way. Creating a File object is easy:
File f = new File("temp.txt");
File f2 = new File("/home/steve/testFile.txt");
Trademarks
Page 49 of 59
developerWorks
ibm.com/developerWorks
The File constructor takes the name of the file it will create. The first call creates a
file called temp.txt in the given directory. The second call creates a file in a
specific location on my Linux system. You can pass any String to the constructor
of File, so long as it is a valid file name for your OS, whether or not the file that it
references even exists.
This code asks the newly created File object if the file exists:
File f2 = new File("/home/steve/testFile.txt");
if (f2.exists()) {
// File exists. Process it...
} else {
// File doesn't exist. Create it...
f2.createNewFile();
}
java.io.File has some other handy methods that you can use to delete files,
create directories (by passing a directory name as the argument to File's
constructor), determine whether a resource is a file, directory, or symbolic link, and
more.
The real action of Java I/O is in writing to and reading from data sources, which is
where streams come in.
Trademarks
Page 50 of 59
ibm.com/developerWorks
developerWorks
2.
Call read() to read one character at a time until you reach the end of the
file.
Writing to a File
As with reading from a File, there are several ways to write to a File. Once again,
Constructs for real-world applications
Copyright IBM Corporation 2010. All rights reserved.
Trademarks
Page 51 of 59
developerWorks
ibm.com/developerWorks
2.
Buffering streams
Reading and writing character streams one character at a time is not exactly
efficient, so in most cases, you'll probably want to use buffered I/O instead. To read
from a file using buffered I/O, the code looks just like Listing 25, except that you
wrap the InputStreamReader in a BufferedReader, as shown in Listing 27:
Listing 27. Reading from a File with buffered I/O
Logger log = Logger.getAnonymousLogger();
StringBuilder sb = new StringBuilder();
try {
InputStream inputStream = new FileInputStream(new File("input.txt"));
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
try {
String line = reader.readLine();
while (line != null) {
sb.append(line);
line = reader.readLine();
}
} finally {
reader.close();
}
} catch (IOException e) {
log.info("Caught exception while processing file: " + e.getMessage());
}
Writing to a file using buffered I/O is the same: you just wrap the
OutputStreamWriter in a BufferedWriter, as shown in Listing 28
Trademarks
Page 52 of 59
ibm.com/developerWorks
developerWorks
I've merely scratched the surface of what's possible with this essential Java library.
On your own, try applying what you've learned about files to other data sources.
Trademarks
Page 53 of 59
developerWorks
ibm.com/developerWorks
java.io.Serializable
The first step to making serialization work is to enable your objects to use the
mechanism. Every object you want to be serializable must implement an interface
called java.io.Serializable:
import java.io.Serializable;
public class Person implements Serializable {
// etc...
}
The Serializable interface marks the objects of the Person class to the runtime
as serializable. Every subclass of Person will also be marked as serializable.
Any attributes of an object that are not serializable will cause the Java runtime to
throw a NotSerializableException if it tries to serialize your object. You can
manage this by using the transient keyword to tell the runtime not to try to
serialize certain attributes. In that case, you are responsible for making sure the
attributes are restored so that your object will function properly.
Serializing an object
Now you'll try an example that combines what you've just learned about Java I/O
with what you're learning now about serialization.
Suppose you create and populate a Manager object (recall that Manager is in the
inheritance graph of Person, which is serializable) and then want to serialize that
object to an OutputStream, in this case to a file. That process is shown in Listing
29:
Listing 29. Serializing an object
Manager m = new Manager();
m.setEmployeeNumber("0001");
m.setGender(Gender.FEMALE);
m.setAge(29);
m.setHeight(170);
m.setName("Mary D. Boss");
m.setTaxpayerIdentificationNumber("123-45-6789");
log.info("About to write object using serialization... object looks like:");
m.printAudit(log);
try {
String filename = "Manager-" + m.hashCode() + ".ser";
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename));
oos.writeObject(m);
log.info("Wrote object...");
} catch (Exception e) {
log.log(Level.SEVERE, "Caught Exception processing object", e);
}
Trademarks
Page 54 of 59
ibm.com/developerWorks
developerWorks
The first step is to create the object and set some attribute values. Next, you create
an OutputStream, in this case a FileOutputStream, and then call
writeObject() on that stream. writeObject() is a method that uses Java
serialization to serialize an object to the stream.
In this example, you are storing the object in a file, but this same technique is used
for any type of serialization.
Deserializing an object
The whole point of serializing an object is to be able to reconstitute, or deserialize, it.
Listing 30 reads the file you've just serialized and deserializes its contents, thus
restoring the state of the Manager object:
Listing 30. Deserializing an object
Manager m = new Manager();
m.setEmployeeNumber("0001");
m.setGender(Gender.FEMALE);
m.setAge(29);
m.setHeight(170);
m.setName("Mary D. Boss");
m.setTaxpayerIdentificationNumber("123-45-6789");
log.info("About to write object using serialization... object looks like:");
m.printAudit(log);
try {
String filename = "Manager-" + m.hashCode() + ".ser";
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename));
oos.writeObject(m);
log.info("Wrote object...");
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename));
m = (Manager)ois.readObject();
log.info("Read object using serialization... object looks like:");
m.printAudit(log);
} catch (Exception e) {
log.log(Level.SEVERE, "Caught Exception processing object", e);
}
For most application purposes, marking your objects as serializable is all you'll
ever need to worry about when it comes to serialization. In cases where you do need
to serialize and deserialize your objects explicitly, you can use the technique shown
in Listings 29 and 30. But as your application objects evolve, and you add and
remove attributes to and from them, serialization takes on a new layer of complexity.
serialVersionUID
Back in the early days of middleware and remote object communication, developers
were largely responsible for controlling the "wire format" of their objects, which
caused no end of headaches as technology began to evolve.
Suppose you added an attribute to an object, recompiled it, and redistributed the
Constructs for real-world applications
Copyright IBM Corporation 2010. All rights reserved.
Trademarks
Page 55 of 59
developerWorks
ibm.com/developerWorks
Trademarks
Page 56 of 59
ibm.com/developerWorks
developerWorks
Trademarks
Page 57 of 59
developerWorks
ibm.com/developerWorks
Resources
Learn
Java technology homepage: The official Java site has links to all things related
to the Java platform, including the Java language specification and Java API
documentation.
Java 6: Learn more about JDK 6 and the tools that come with it.
Javadoc homepage: Learn the ins and outs of using the Javadoc, including how
to use the command-line tool and how to write your own Doclets that let you
create custom formats for your documentation.
New to Java technology: Check out this compendium of developerWorks
resources for beginning Java developers.
5 things you didn't know about ...: This developerWorks series provides short
introductions to lesser-known (but often introductory) Java programming tips
and lore.
"Try to catch me: Does exception handling impair performance?" (Tony Sintes,
JavaWorld, July 2001): This article provides introductory information about
exception handling in Java programs.
"Exception: Don't get thrown for a loss" (Tony Sintes, JavaWorld, February
2002): Understand the difference between checked and runtime exceptions.
"Regular expressions simplify pattern-matching code" (Jeff Friesen,
JavaWorld.com, February 2003): Explore this extended introduction to regex.
"Diagnosing Java code: Java generics without the pain, Part 1" (Eric Allen,
developerWorks, February 2003): This is the first part of an article series
introducing generics in Java syntax.
"Java technology, IBM style: A new era in Java technology" (Chris Bailey
developerWorks, April 2010): Read about upcoming changes in the official Java
SE 7 release, as well as IBM value-adds focused on improving Java platform
performance, reliability, and serviceability.
Refactoring: Improving the Design of Existing Code (Martin Fowler et al.,
Addison-Wesley, 1999): This book is an excellent resource for learning how to
write cleaner, more maintainable code.
Design patterns: Elements of reusable object-oriented software (Erich
Gammaet al., Addison-Wesley, 1994): Learn more about the Factory pattern,
one of 23 design patterns that continue to define contemporary software
development.
Eclipse IDE project resources from IBM: Learn what Eclipse is good for, why it
Trademarks
Page 58 of 59
ibm.com/developerWorks
developerWorks
is important, how you can get started with it, and where to learn more about it.
The Java Tutorials: Get a comprehensive introduction to the Java language.
The developerWorks Java technology zone: Hundreds of articles about every
aspect of Java programming.
Get products and technologies
JDK 6: Download JDK 6 from Sun (Oracle).
Eclipse: Download the Eclipse IDE for Java Developers.
IBM developer kits: IBM provides a number of Java developer kits for use on
popular platforms.
Discuss
Get involved in the My developerWorks community. Connect with other
developerWorks users while exploring the developer-driven blogs, forums,
groups, and wikis.
Trademarks
Java and all Java-based trademarks are trademarks of Sun Microsystems, Inc. in the
United States, other countries, or both.
Trademarks
Page 59 of 59