Thursday, 4 March 2010

Learning Java

-->

Learning Java

Java isn't difficult but the little details of compiling and packaging keep tripping me up so it's time to take a more rigorous (otherwise known as boring) approach.

Sun's Java tutorials are a good starting point. Religiously following the hello world examples takes only a few minutes and even if it doesn't teach you anything new it helps to crystallise the thoughts you have about the things that the tutorial hasn't told you so that you can pursue those things in a less scatterbrained manner (at least that's how it feels to me).

Lesson: Your First Cup of Java

  • Java programs are not executables in the traditional sense (I know that they can be but that is knowledge from elsewhere -> JavaJars). To execute them you provide the path to the class file to the java program.
  • The principle difference between a simple console application and an applet is that the applet must draw on a canvas when the paint method is called while the console application uses the System.out class to send characters to standard out.

Using Classes and Objects

This is a confusing section because it talks about class and instance methods and variables without explaining the differences in the declarations. That is, it explains how to use already defined class methods but doesn't tell you how to create your own even though the hello world application does exactly that.

The Anatomy of an Applet

Applets do not need to implement a main method. The lesson thereby implies that they could if they wanted to but doesn't expand on it.

Applets must extend Applet so they must import java.applet.Applet or the extends statement must fully qualify the Applet class.

public class HelloWorld extends java.applet.Applet {

If you want to use many classes from a package then you might want to just import the whole package; do this by using an asterisk instead of the class name:

import java.applet.*;

All the stuff in java.lang is imported by default so System and its friends do not need an import statement.

None of what we have learnt so far explains what a package is from our point of view as application programmers nor does it explain how or why we might create our own packages.

Solving Common Compiler and Interpreter Problems

At last, this is where I normally trip up and fail to get a runnable program.

Can't Locate the Compiler
make sure javac is in the path. Likewise appletviewer, javaw and so on.
Can't Find Class
the interpreter wants the name of the class not the name of the file containing it. I presume that this means that java searches the .class files as though they were a virtual file system. That is every .class file in the classpath counts as a little folder and the classes are the virtual files.
The main Method Is Not Defined
Once java has found the class it searches for the main method. If the clas doesn't have one the program can't run.

Exercises

Exercise 5 has two classes in two separate .java files. When compiled you get two separate .class files. If there had been more than one class in each .java file there would be one .class file for each class. To run the program you execute java with SecondClass as the argument:

SLOCOMBE# pwd
f:/Users/kjw/java/tutorials/cupojava
SLOCOMBE# java SecondClass
English (United Kingdom)

If you change to another directory and try what seems obvious to run the class again you will see that it fails:

SLOCOMBE# pwd
f:/Users/kjw/java/tutorials/cupojava
SLOCOMBE# cd ..
SLOCOMBE# java cupojava/SecondClass
java.lang.NoClassDefFoundError: cupojava/SecondClass (wrong name: SecondClass)
        at java.lang.ClassLoader.defineClass0(Native Method)
        ...

This further reinforces the lesson that the java compiler wants a class name not a filename.

No comments:

Post a Comment

Blog Archive

Followers