Setting up your Java Environment (Simple)

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.

Getting ready

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.

How to do it...

  1. Assuming you don't have the JDK installed (if you have it, congratulations! Feel free to skip the steps involving downloading and installing the JDK), make sure that you have the files for both Eclipse and the JDK downloaded.
  2. Install the JDK first (unless you are an expert and have a strong reason not to, opt to install all packages that it suggests).
  3. Use the default folder for installation (for Windows the default folder is C:javajre7).

    Note

    For more information about system requirements, installation, and troubleshooting installation, Oracle's JDK 7 and JRE 7 Installation Guide is an excellent resource. You can find the guide from the following link:

    http://docs.oracle.com/javase/7/docs/webnotes/install

  4. After successfully installing the JDK, extract the Eclipse downloaded file to any folder. You will find the Eclipse application inside the extracted folder. I suggest creating a shortcut to Eclipse somewhere convenient, such as in your Programs or Applications folder, or on your desktop.
  5. The first time you run Eclipse, it will prompt you to create a workspace. This is a location where all of your Java files will be stored. The location doesn't matter, but keep in mind that you'll be typing the location into the command line quite a bit—something short like /Users/rmitchell/workspace is usually convenient.

How it works...

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.

Note

This Java bytecode is read by the Java Virtual Machine (JVM) and is translated into lower-level assembly language specific to each operating system. JVMs can also be written to run at a hardware level, resulting in faster-running code, although this is less common.

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.

There's more...

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/.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset