JCQ Java

Wednesday, September 06, 2006

Individual Objects

The one thing these Java objects have in common is this: their individuality.

In Java, the JVM (Java Virtual Machine) is responsible for creating objects -- and destroying them when they are no longer needed. Once an object is created, it behaves on its own -- like an individual.

To show you what this means, try:

java\demo\applets\MoleculeViewer\example3.html

There, you'll find more than one applet on a single webpage. Each applet runs the same Java program, but given different parameters. As a result, several objects (Java programs) are created, each displaying a different molecule. All these objects behave similarly (they come from the same program), yet their behaviour are individual (e.g. you can turn one molecule this way, the other molecule that way).

As you can see, that's are lot to learn about the simple "object". But it is fun, isn't it?
More examples of objects:

java\demo\applets\Clock\example1.html

* an object you can look but can't touch.


java\demo\applets\Animator\example3.html

* click to freeze, click again to unfreeze.
* an object you can only touch.


java\demo\applets\ArcTest\example1.html

* type the start angle (S) and end angle (E), then click "Draw", or "Fill" buttons.
* an object you can change by input.


java\demo\applets\SortDemo\example1.html

* click an applet to visually see how the sorting is done (the 2 color bars are swaps).
* start all the applets to see which sorting is fastest.
* similar objects (sort programs) but different behaviour (sorting methods).


The last one is an example of visualization of algorithm, using Java.

After all the data are sorted, you can click again to find out which algorithm is fastest on sorted data.

Java and OOP

Java is a programming language. Besides being platform-independent, Java supports Object-Oriented Programming (OOP). This brings up the following questions:

* What is Object?
* What is Object-Oriented?
* What is Object-Oriented Programming?


So there are 3 questions for just one concept: OOP!

I won't try to answer all these questions at once. In fact, without going into the details of Java programming, I cannot answer these questions fully. Just keep these questions in mind. In due course, you'll find the answers. And by that time, you'll know Java.

I just want to relate these questions to the applets you are playing.

Using the mouse, you can turn the 3-dimensional molecule around. It is fun, isn't it?

This is an example of an object -- something you can touch and manipulate.

However, this is a software object -- not a real one. Real molecules are too small to touch and turn. The Java program creates a software object that simulates the real one, and let you touch and turn it.

Software objects can simulate anything: real ones, imaginary one, even those you never think in terms of objects. For example, the Java program itself is an object -- that's why Java is OOP. However, you cannot touch or turn a Java program.

Since we shall be talking Java, "objects" always means "software objects" from now on.

Some objects (e.g. the simulated molecule) you can turn, but some objects (e.g. the Java program) you cannot turn. What do Java objects have in common?

Tuesday, September 05, 2006

Playing with Java

Java is a programming language. There is no point in learning another programming language if you are not interested in it. So I just want you to start playing with Java, to get an idea what Java is all about.

Java source code is compiled to class file(s). Class files are run by JVM - Java Virtual Machine, a piece of software in the JDK. This is quite technical. However, every modern browser has a JVM inside. This means that browsers can run little Java programs, or "applets" in Java-speak. Therefore, as long as you have a browser (e.g. IE or FF), you can run Java applets.

The JDK comes with a set of applet demos, located under [java]\demo (or somewhere similar). There you can find a lot of applets, under different folders. For example, TicTacToe is under the folder:

[java]\demo\applets\TicTacToe [java] is where you install JDK.

Once you are in a demo applet folder, you can double-click the webpage (.html) to put the page on the browser, and thereby loads and runs the applet.

Security is a built-in feature of Java. This means applets cannot do any harm to the computer. Therefore, choose from any applet folder, and run to see what it is -- this cannot do anything bad to the computer.

All applets are interactive, so use mouse clicks to see effects. For now, don't worry about how to write such Java applets. Just try out the applets and appreciate what Java can do.

Besides TicTacToe, my favourite demo applet is MoleculeViewer. There you can use the mouse to rotate a chemical molecule in 3-dimensions.

Discover your favourite demo applet, and see Java in action.

Sunday, September 03, 2006

Starting with Java

To start learning the programming language Java, the first steps are:

  1. Download the latest Java Development Kit (JDK)

  2. Get a good text editor (TextPad)

  3. Get a good browser (FireFox)

For step 1, go to java.sun.com. On the right is a list of popular downloads. Choose Java SE, SE = Standard Edition -- this is the first one. This leads to JDK 5.0. You only need to download JDK 5.0 update 7 (now the third one). The first one (which bundles with an IDE) is useful, and the source code may be handy (but needs registration).

Read the installation instructions. The default install location is: C:\Program Files\Java\jdk1.5.0_[version]\, but this pathname contains a space. Better install to c:\java\jdk. We shall denote this Java install location as [java], or JAVA_HOME. Remember to add %JAVA_HOME%\bin to the system PATH.

Rather than using an IDE (Integrated Development Environment), I prefer a simple text editor TextPad, from www.textpad.com. Get the latest version.

With a browser, you can run Java applets immediately. Although Windows XP comes with the browser Internet Explorer (IE), Microsoft is not giving full support of Java technology. A better one is FireFox (FF), from www.mozilla.com. Download the latest FF 1.5 -- there is a Chinese version.

Save all your download executables to a standard location. This is useful later.

It is recommended that you install JDK first, in a location [java] = c:\java\jdk = JAVA_HOME, update system PATH.
Both TextPad and FireFox will detect if Java is on the system PATH, and configure themselves accordingly.

For further info on setting system PATH for Java, have a read on:
Installing Java on your PC