Chapter 2. Introducing Objects

  • Downloading and Compiling Java

  • What Is a Class?

  • What Is an Object?

  • Java Digital Clock Program

  • Summary

  • Exercises

  • Some Light Relief—Napster and LimeWire

Here's where we get to grips with a real Java program. Follow the steps in Appendix A to download a Java compiler. Then you can type in source code and test examples as we go along.

The chapter presents some basics of Object Oriented Programming (OOP). We'll explain exactly what language designers mean by a type, and relate that to the objects in OOP. We'll develop a type called Timestamp, then add a couple of other types to turn it into a complete program. Along the way, we'll clarify the super-important distinction between “an object” and “a reference to an object”. The chapter ends with the development of a small but complete running Java program that displays a desktop clock.

Downloading and Compiling Java

If you use Solaris or an Apple computer running MacOS X, a Java compiler is already installed on your system. So you folks can skip right ahead to “Running a Java Program”. Dell and H-P preload Java libraries on their Windows PCs, but you still need to download a compiler. If you are using Windows, Linux, or Solaris, Sun's website at java.sun.com is the easiest place to get a free Java compiler. There are other places too. IBM has some great Java compiler and tool downloads. If you are using something other than Linux, MacOS, Windows or Solaris, find the Java compiler by searching the manufacturer's website.

Downloading a Java compiler system

Follow the steps in Appendix A to download and install a Java Development Kit (JDK). The current version of the JDK is called "J2SE version 5", i.e. Java 2 Standard Edition version 5 of the Software Development Kit. During beta testing, this release was called version 1.5, and you may still hear old timers call it "JDK 1.5". It was promoted to version 5 in recognition of the large number of core language changes in the release.

Compiling a Java program

A lot of programmers use IDEs to develop their code, and there are many IDEs that support Java, including some free ones such as Sun's NetBeans, Eclipse, or the BlueJ environment. You can get a list by googling for “Java IDE”. We'll avoid IDEs here and do everything from the command line, to keep the learning experience focused on Java. Create a separate work directory just for your files, and don't put your directory anywhere under the JDK installation directory.

mkdir c:work
cd c:work

After installing, you can run the Java compiler. Type this at the command shell:

javac

Assuming you installed the JDK and set the path correctly, you will get back a dozen lines of messages, starting with this line:

Usage: javac <options> <source files>

Voila, your first Java program (no file at all, which is even easier than "hello world") compiles OK. If you saw something different, like "javac not recognized", troubleshoot your installation and/or the path variable as outlined in appendix A.

Running a Java program

The next step is to run a Java program. Java was originally implemented as an interpreted language. You ran the interpreter program, the interpreter read the bytecode, and interpreted it into the corresponding instructions for that architecture.

And that's still mostly true today for most implementations of Java. Sun refers to the interpreter program as the Java Virtual Machine (JVM). The JVM, the run-time library, and the extensive Java libraries together are known as the Java Runtime Environment (JRE). The JRE does for Java applications roughly what Microsoft's Common Language Runtime (CLR) does for .net applications. The CLR lets a program be written in any of several Windows languages and only run on Windows. The JRE lets a program (in any language that compiles to Java bytecode) run on every computer system.

That truly means Java runs on just about every computer system. Cell phones and digital cameras today contain general-purpose microprocessors, which is the main reason there's a few seconds ofdelay after turning them on—they take time to boot up. I haven't written a Java program for my digital camera yet, but there are plenty for my cell phone.

Continuing the discussion of setting up Java, here's the command that runs a Java program:

java classname

We are going to use a digital clock program as an example in the rest of the chapter. You can download the clock.java source code file from www.afu.com/jj6 or you can type in the code from the listings later in the chapter. Either way, make sure the files are in your work directory and “cd” to it, so it becomes your current working directory. After successfully compiling, you can run a program with the main code in a class called “clock” by typing:

java clock

When you compile and run the example program, a window containing a digital clock display appears, and the display keeps updated with the current time. The commands to compile and run are shown in Figure 2-1. The example uses a Mac.

Figure 2-1. Compiling and running clock.java

image

The javac command will create a file called “clock.class” containing the bytecode. If you create the same files on a windows system (or move the clock.class file from any system over to a windows system), you can execute it with the command “java clock”. You will see a window like Figure 2-2 appear, and keep time:

Figure 2-2. Running the same clock program on Windows

image

Download or type in, then compile and run clock.java now. Don't sweat the details about understanding individual lines of code; we will go over them in the following sections. It's time to say a few words about classes and objects.

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

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