Java

Getting Started

The Oracle/Sun website has an abundance of material on Java (it is incredibly large though!) Try starting with The Java Tutorials, which has information about the Java language, including things like array syntax. The following sections of these tutorials will likely be useful throughout CMPUT 174/175 (and for other novice Java programmers):

Applets vs. Applications

What is the difference between an applet and an application?

Applets and applications are very similar in most respects; the theory that you have learned in your CMPUT 174 lectures is equally relevant to both applets and applications. The language, Java, is the same language when writing either kind of program. However, the big difference between the two lies in the way that they are actually launched.

An application is what we call a "stand alone" program - it doesn't rely on any other program to be able to execute. In this regard, a Java application is similar to your word processing program or database program, for example.

  • They don't require the assistance of other pieces of software to be able to execute.
  • You write the Java code, compile it into byte code, and then invoke the Java Virtual Machine (VM) which is responsible for interpreting the bytecode and executing it on your machine. There are various ways to invoke the Java VM - you might type a command at a prompt (if you're using a command-line interface, like the SDK), or you might click on a button in an IDE like Eclipse, which in turn executes the necessary command to invoke the VM.
  • Either way, the Java VM that you use to run your application is not part of some bigger program, or embedded into any other software.

An applet, on the other hand, gets executed by a VM that is part of some other program.

  • So we write the Java code for our applet, compile it into bytecode, and then use a VM that is embedded as part of some other piece of software to actually run the applet.
  • For example, modern web browsers have Java VMs that are built-in as part of the browser itself - when the browser is told to run an applet, the VM that is incorporated in the browser gets to work. Another way to run an applet is to use a program called an applet viewer - this also has a VM incorporated into it.
  • Applets do not contain a main() method, like applications do. Instead, there are other, special methods that you will use with your applets, which are discussed below.

The Life Cycle of An Applet

Students often feel uncomfortable with applets because they don't really understand the various methods that they need to include, why these are necessary, and what happens when the VM starts to run an applet. The following describes the events that take place when we launch an applet. If you have difficulty understanding any of these points, make sure that you talk to your TA or instructor.

  1. The web browser or applet viewer receives a command (from the user) to run a particular applet.
  2. The VM that is built-into the browser or viewer starts up, and the compiled version of the applet (the bytecode, which is stored in a .class file somewhere) gets located.
  3. The VM starts to interpret the bytecode into the machine language of the computer that we're trying to run the applet on.
  4. An Applet object is created. This is done automatically for us, behind the scenes. You don't need to invoke an Applet constructor in your code, the system takes care of creating the Applet object. This Applet object knows how to respond to all of the messages specified in the Applet class.
  5. A series of important Applet methods now gets executed, each method plays a specific role in the set-up and execution of the applet. If your applet code does not contain its own customized versions of these methods, the system will use its own "invisible", default versions instead. If you, the programmer, have supplied your own, more detailed versions of these methods, they will "override" the default versions. (You will understand this issue a lot more when we get to the "Inheritance" lecture later on in the course).

    These important methods include:

    1. init() method
      The init() method is executed only once, when the applet is first launched, just before it starts running. After creating the Applet object, the init message is sent to the Applet object to initialize the state of the Applet object. For example, suppose we want to have clickable buttons attached to our applet when it is first displayed - these buttons have to be created and then attached to the Applet as part of its state.
    2. start() method
      This method performs the task of activating the method, making it actually run. Unlike the init() method, it can be invoked more than once. It is invoked after the init() method when the applet is first launched, and then re-invoked each time the applet needs to be started up again. (For example, the user paused the applet in order to do some other task, but when (s)he returns the applet needs to be started up again. To do this, the system invokes the start() method). In CMPUT 114, you won't usually have to worry about including a start() method of your own in your applet code, the default version is usually sufficient.
    3. paint() method is also called after the init() method.
      The paint() method is responsible for drawing (or "painting") the applet onto the screen for the user to see. The paint() method takes one argument of declared type Graphics:
      public void paint(Graphics g) {
      • When the applet is first launched a Graphics object is automatically created by the system, and a reference to this Graphics object is supplied as an argument for the invocation of the paint() method.
      • You, the programmer, do not have to use a constructor to create the Graphics object, or declare a Graphics instance variable or anything like that. The run-time system takes care of that automatically for us.
      • The Graphics object models the portion of the screen that your applet is going to be displayed on. So when the applet is launched and the init() method has finished executing, the paint() method is given a reference to this automatically-created Graphics object and uses it whenever the paint() method is invoked.
      • The paint method can be called many times - each time we need to re-draw the applet onto the screen, the paint() method gets invoked.
      • Note: in CMPUT 174 we have a rule which says that students must not use single character identifiers for variable names. Most Java programmers write their paint method signature as given above with the name "g", but to be consistent with 114 style rules we will use the following signature in our example code:
        public void paint(Graphics g) {
    4. There are other applet methods that may be invoked during the lifecycle of an applet, but as we do not need to use them in this course, we won't discuss them in this document.

For further information about applets and applet methods, take a look at the Applet Tutorial at the Sun website.

Sample Code

All files below are .JAVA files of 5kb or less unless otherwise noted

Intro

Loops, sorting & recursion

Vectors, objects & subclasses

Small programs

Adventure program