No more programming in Notepad. Although it's technically possible to write Java code in anything that can save a text file, it's also technically possible to recreate the Internet using carrier pigeons (See http://www.ietf.org/rfc/rfc1149). But we don't do that either.
The first step to writing in a new language is to make sure you have the language's compilers and/or interpreters installed. If you do not have the Java Development Kit (JDK) installed already, you can download it from Oracle's website from the following link:
http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html
Writing Java can often become tedious when dealing with Java's often finicky demands. Because Java is a strongly typed language, it is necessary to remember which datatype a particular variable is, in a particular section of code, and which operations can be done on it. A class that has been defined to accept a float
value in one file cannot be called with an argument of a double
type in another file, and a static class cannot be called in a non-static context later on. When navigating Java, it helps to have a tour guide on your team—that's where the IDE comes in.
A good Integrated Development Environment (IDE) is nearly indispensable to work with Java, especially with complex projects. An IDE allows you to catch compile time bugs and syntax errors on the fly, use advanced debugging features (no more print
statements in every line), inspect objects, and even suggest functions and auto-complete code for you.
There are many excellent IDEs on the market. The most popular among them are Eclipse, NetBeans, and JCreator. However, you should use the IDE you feel most comfortable with if you have a preference. We will be working with Eclipse in this book, and any reference to tasks done in the IDE will assume that you are using Eclipse.
Eclipse Classic can be found for Windows, Mac OS X, and Linux at the following link:
http://www.eclipse.org/downloads/packages/eclipse-classic-422/junosr2
The download will likely take a few minutes to finish, so read through the rest of the recipe while it's working.
C:javajre7
).Programs
or Applications
folder, or on your desktop.Users/rmitchell/workspace
is usually convenient.The JDK is a collection of useful tools for developing Java software. The most important of these is the javac (Java compiler, pronounced as "java-see"), which translates your written Java files into Java bytecode. You may be familiar with Java bytecode as the .class
files that are created after your .java
files are compiled.
The mantra of Sun Microsystems, while developing Java, was:
"Write once, run anywhere."
This was accomplished through the creation of JVMs, custom-written to precise specifications, in order to add a buffer between inconsistent operating systems and processors, and the Java bytecode that was being run on them. Rather than writing software for each environment it would be running in, Java programmers could write it once, compile, and let the JVM take care of the rest.
Several other useful applications come bundled with the JDK that we will use. Among these are the Java Archive (JAR), which is based on the .zip
format and used to bundle collections of executable Java files and any associated resources needed. Another application of this type is Javadoc
, a documentation generator that aggregates source code comments and other information to create HTML-based documents for your code.
We've only mentioned a few of the dozens of technologies that combine to form the Java platform. However, you don't really need to know about much other than a handful of interface-level tools and APIs in order to write some serious Java code. Having a better understanding of how everything fits together can be useful for improving your control over the language and understanding the documentation when the going gets tough. An overview of the Java platform and complete documentation for the language can be found on Oracle's website, at http://docs.oracle.com/javase/7/docs/.