Cover | Table of Contents | Colophon
import statements, forgotten
variable declarations, omitted semicolons, garbled syntax,
typos—all these problems will cause the Java command-line
compiler, javac, to cough in your face and display
pages of annoying error messages. The error messages tell you that
javac knows what the error is, so why
doesn't it just fix the problem and let you get on
developing?javac can't fix the problem; it
isn't an editor. That makes long streams of errors
scrolling off the page an all-too-common experience for Java
developers, and leaves them with the feeling that Java is too prickly
about what can go wrong. To change all that, you can use an
integrated development environment (IDE), which will not only catch
errors before you try to compile, but also suggest solutions. Java is
badly in need of a good IDE, and a number of candidates are
available, but the premiere Java IDE these days is the one this book
is all about: Eclipse. You can see Eclipse in action in Figure 1-1.
javac to stumble are
usually handled before you even try to compile, and if there is an
issue, Eclipse will suggest solutions. All you have to do is point
and click—no need for serious head-scratching. If
you're like most Java developers,
you're going to find yourself thinking,
javac to stumble are
usually handled before you even try to compile, and if there is an
issue, Eclipse will suggest solutions. All you have to do is point
and click—no need for serious head-scratching. If
you're like most Java developers,
you're going to find yourself thinking,
This is great!
http://www.eclipse.org were so
busy that it was almost impossible to download a copy for the first
few days.http://www.eclipse.org, in
Figure 1-2.
http://www.eclipse.org/downloads and select
one of the download mirrors available on that page. When you do,
you'll be presented with a list of the available
downloads, which are of these types:public class Ch01_01
{
public static void main(String[] args)
{
System.out.println("No worries.");
}
}
Ch01_01 code to display the
"No worries." message along with
today's date:public class Ch01_01 {
public static void main(String[] args) {
outString = "No Worries on ";
Calendar rightNow = Calendar.getInstance( );
System.out.println(outString + rightNow.getTime( ));
}
}
outString is not declared, which makes the first
and last lines of code in main invalid, and the
Calendar class has not been imported, making the
middle line of code invalid. If you were using
javac, you'd have to quit editing
and run javac to catch those errors. But the
second you enter these lines into Eclipse, they'll
be flagged as errors with wavy red underlines, as you see in Figure 1-16.
Ch01_01, you'll find Eclipse
getting more and more crowded, since all your projects are displayed
in the Java perspective's Package Explorer view, as
well as the Navigator view (recall that the Navigator view is there
to let you navigate between projects). If you have 30 projects, there
will be 30 entries there. There are various ways to deal with this
clutter (such as creating working sets, as we'll see
in Chapter 2), but we'll take
a look at the simplest one here.
Ch01_01
project, just right-click its icon and select the
Delete item. Eclipse will display the Confirm
Project Delete dialog box, as you see in Figure 1-20.
printer, which displays
the message "No worries.", as you
can see in Example 2-1.public class Ch02_01
{
public static void main(String[] args)
{
printer( );
}
private static void printer( )
{
System.out.println("No worries.");
}
}
Ch02_01. Then create a new Java class named
Ch02_01, making it part of the
org.eclipsebook.ch02 package. Leave the checkbox
for the creation of a stub for the main method
checked when you create this new class. This gives you the code:public class Ch02_01 {
public static void main(String[] args) {
}
}
printer method, of course, but
Eclipse can also be of assistance here. Move the cursor below the
body of the main method and type
private to make this new method a private method,
and then type Ctrl+Space to open code assist, as you see in Figure 2-1.printer, which displays
the message "No worries.", as you
can see in Example 2-1.public class Ch02_01
{
public static void main(String[] args)
{
printer( );
}
private static void printer( )
{
System.out.println("No worries.");
}
}
Ch02_01. Then create a new Java class named
Ch02_01, making it part of the
org.eclipsebook.ch02 package. Leave the checkbox
for the creation of a stub for the main method
checked when you create this new class. This gives you the code:public class Ch02_01 {
public static void main(String[] args) {
}
}
printer method, of course, but
Eclipse can also be of assistance here. Move the cursor below the
body of the main method and type
private to make this new method a private method,
and then type Ctrl+Space to open code assist, as you see in Figure 2-1.
Ch02_03 project and select Project→
Build Project, the .class files for this project
will appear in the directory
workspace/Ch02_03/org/eclipsebook/Ch02 (recall
that the classes in this project are in the
org.eclipsebook.Ch02 package, which the directory
structure reflects). Once created, these .class
files are ready for use and distribution.
package org.eclipsebook.ch02;
/**
* @author Steven Holzner
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
.
.
.
@param and invoke code
assist with Ctrl+Space, code assist will list the parameters a method
takes. Typing @exception and using code assist
will list the exceptions a method throws, and so on. Typing
@ in a comment and pausing will make code assist
display the Javadoc possibilities, like @author,
@deprecated, and so on.
Ch02_05
project in Figure 2-19.
package org.eclipse.ch02;
/**
* @author Steven Holzner
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class Ch0206 {
public static void main(String[] args) {
name( );
}
public static void name( ) {
System.out.println("No worries.");
}
}
name, is called in the main
method, and it could be called from other locations in your code as
well. How can you change the name of this method and automatically
update all calls to it? Select name in the editor
and then select the Refactor→ Rename menu item, opening the
Rename Method dialog you see in Figure 2-20.
printer in this
case, and click OK. When you do, the name of this method and all
references to it will be updated throughout your code, including all
code in the project, as you see here:package org.eclipse.ch02;
/**
* @author Steven Holzner
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class Ch0206 {
public static void main(String[] args) {
System.out class—just highlight
System.out in your code and open its hierarchy.
You can also open this view by selecting an item in the editor and
selecting the Navigate→ Open Type Hierarchy item.
public void printer( ) {
System.out.println("No worries.");
}
public void printer( )
{
System.out.println("No worries.");
}
/* * Created on Oct 17, 2003 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */
assertEquals(a, b)
a is equal to b
(a and b are either primitive
values or must have an equals method for
comparison purposes)assertFalse(a)
a is false, where a is
a Boolean valueassertNotNull(a)
a is not null, where
a is either an object or null
assertNotSame(a, b)
a and b both do not
refer to the identical objectassertNull(a)
a is null, where
a is either an object or assertEquals(a, b)
a is equal to b
(a and b are either primitive
values or must have an equals method for
comparison purposes)assertFalse(a)
a is false, where a is
a Boolean valueassertNotNull(a)
a is not null, where
a is either an object or null
assertNotSame(a, b)
a and b both do not
refer to the identical objectassertNull(a)
a is null, where
a is either an object or null
assertSame(a, b)
a and b both refer to
the identical objectpackage org.eclipsebook.ch03;
public class Ch03_02 {
public static void main(String[] args) {
System.out.println(factorial(6));
}
public static int factorial(int value) {
if(value == 0){
return value;
}
else {
return value * factorial(value - 1);
}
}
}
n! =
n
* (n
- 1) * (n
- 2) ... * 1. In our
example, to calculate factorial(n), the
factorial method multiplies n by
factorial(n
-
1), calling itself to determine the factorial of
(n-1). To determine http://www.cvshome.org.http://www.cvshome.org.cvs
--help at the prompt; you should see a list of help items.
If you can't find a CVS server, you can download
what you need from http://www.cvshome.org.http://www.cvsnt.org. To install
CVSNT, just download the executable file and run it.cvs
-d
path
init,
where path gives the location of the
directory you want to use as the repository (the permissions and
ownership for path should be set so all
members of your development team can access it).Ch04_01, to the CVS repository. After this project
is in the CVS repository, anyone with access to the repository can
check it out and work on it. You can see this sample project in Example 4-1; this sample code does nothing more than
display the word "Hello".package org.eclipsebook.ch04;
public class Ch04_01 {
public static void main(String[] args) {
System.out.println("Hello");
}
}
http://ant.apache.org
/).
Ant is a Java-based build tool that can perform all these tasks and
much more. You can download Ant and run it on the command line,
automating your build tasks to not only compile code, but to create
JAR files, move and create classes, delete and make directories, and
a great deal more.Ch05_01, and add a new class to it,
Ch05_01. In this class's
main method, we'll just display
the message "No worries.", as you
see in Example 5-1.package org.eclipsebook.ch05;
public class Ch05_01 {
public static void main(String[] args) {
System.out.println("No worries.");
}
}
Ch05_01, and add a new class to it,
Ch05_01. In this class's
main method, we'll just display
the message "No worries.", as you
see in Example 5-1.package org.eclipsebook.ch05;
public class Ch05_01 {
public static void main(String[] args) {
System.out.println("No worries.");
}
}
<?xml version = "1.0" encoding="UTF-8" ?>
<project name = "Ch05_01" default = "Main Build">
<target name = "Main Build">
<echo message = "Ant at work!" />
</target>
</project>
http://ant.apache.org/manual/index.html.Ch05_02. To emulate a
somewhat real-world project, we're going to store
the example's source code in a directory named
src and its output in a directory named
bin. You can set those directories up when you
create the project in the third pane of the New Java Project dialog
by clicking the Source tab, then clicking the Add Folder button, then
the Create New Folder button to open the New Folder dialog. Enter the
name src in the Folder name box and click OK
twice. Eclipse will ask if you want to remove the project as source
folder and update the build output folder to
Ch05_02/bin. Click Yes, then click Finish to
create the new project, which will be complete with src
and bin folders.Ch05_02, in a package named
org.eclipsebook.ch05, to the project. Ad