Week 6 - Introduction to Java

The docs we will be using for the second part of the semester is the Java Tutorial and the Java API Documentation. There is a page where you can download both of these for off-line viewing if you so desire.

Additional links and (hopefully) helpful info can be found on the resources page. As always, I welcome submissions if anybody's got a link to another site that they think would be useful.

Java Concepts

Two Components of Java Technology

When we speak of Java, there are basically two things that we're talking about:

Client-side

The HTML report you did is pretty much a server-side only technology. That is to say, you upload the document onto the server and then view it in a browser; no special client-side software is needed to view the document (other than a browser, of course).

Those of you who are familiar with the client-server "paradigm" know that you have a part that runs on the server, and a part that runs on the client. Java applets are an example of the client-side of the client-server paradigm.

One of the problems with the client-server paradigm is that it can become onerous to update all of the clients when a change is made to the client software. Java solves this problem by storing the software (.class files) on a server, and automatically downloading the applet through a browser interface when any changes are made. Pretty neat, I think.

Design Goals

Java was originally designed for embedded systems like smartcards, embedded systems, and consumer appliances. Later on, this thing called the Internet came along and if you wanted to deliver executable content to a user through a browser, the requirements for doing so were very similar to embedded systems. Java was a good fit for the Internet.

The following are the design goals of Java (Not coincidentally, these are also the most popular "buzzwords" surrounding Java):

Shortcomings of Java

Like all other technologies, Java has it's strong points and its weak points as well. It is worth noting that most of the weak points were/are political and not technical.

How Java Differs from C / C++

No Pointers - Java does not have pointers. This means it has no * (pointer) or & (address-of) operators. Here's why:

Garbage Collection - In C you need to make explicit calls to malloc() (and friends) to allocate memory. You likewise use free() to deallocate memory. In C++ you use the keywords new and delete. In Java, there is a new keyword, but no delete. Ojbects are implicitly deleted when they go out of scope or are no longer needed. This is known as garbage collection.

No Global Namespace - In C & C++ you can #define a value or declare a variable as global. You cannot do this in Java. Everything must belong to a class or package. It might hurt a little at first if you are used to declaring global variables, but it pays dividends in the long run.

No Multiple Inheritance - Java does not support multiple inheritance as C++ does, but most of the features of multiple inheritance are implemented in the form of interfaces.

Java Code

If you are new to Java (heck, even if you're not), I would suggest that you look at the Learning the Java Language article for a good overview of Java's features. Some of it reads a little bit like marketroid-speak, so take it accordingly.

The good news about learning Java is this: If you already know C and/or C++ then you know 80%-90% of Java already. We will go over some of the basic constructs/variables and some of the notable differences in class.

Hello World

You betcha. :-)

Console Version

Type the following into a file called "HelloWorld.java":


class HelloWorld
{
	public static void main(String[] args)
	{
		System.out.println("Hello World!");
	}
}

Compile the program like this: bash$ javac HelloWorld.java
(Note: "javac" stands for "Java Compiler. Tricky, eh?)
The compiler will create a byte-code class called HelloWorld.class.

Run it in the interpreter like this: bash$ java HelloWorld
It will output the text: Hello World!
(Note: you do not have to add the .class extension for the interpreter. In fact, it doesn't like it when you do.)

Applet Version

Look here to see how to make a hello-world applet in Java. This is a good example because it can serve as skeleton code for darn near any other applet you might want to make.

There is also an example of a stand-alone application which runs independant of a browser in a GUI environment.

Basics

Coding Conventions

This page contains a copius list of stylistic conventions used in Java programming. None of this stuff is mandatory, they're just common conventions which make code more readable.

Proposal For Assignment # 2

Check out the info for Assignment # 2. What I need from you all by next week (February 25th) is a proposal for what kind of Java program you want to do. Make a text file called "proposal2.txt" and put it in your home directory.

Changelog


2/17/99 - Initial revision
2/25/99 - Added section on how Java differs from C / C++