-->
Previous Table of Contents Next


The Java Language

From the programmer’s point of view, Java is a very stripped down object-oriented (OO) language. The principles of OO programming are suitable in most cases to client/server applications, so using OO for Java made sense. By staying with OO Java avoided the need to set up procedural language structures, all of which cause code bloat to the point that Java would not be nearly as fast. Also, by avoiding procedural language principles, Java designers can ignore many of the startup and initialization aspects of programming, again making the source code smaller and faster. Learning Java is not especially difficult. If you know an OO language such as C++, you will probably find Java easier to learn than if you’ve only worked in procedural languages such as C or Basic.

Java may be an OO language, but it’s not strict about it. SmallTalk, for example, is considered a pure OO language because every aspect of SmallTalk is dealt with as either objects or messages, and all data are object classes. Java doesn’t go quite as far, primarily to avoid an overly sizable language. Java implements all the simple C data types (integers, floating points, and characters) outside of the OO method, but everything else is object-based. This is both a benefit because it leads to smaller, faster code and a problem because it means you can’t subclass Java data objects.

As mentioned earlier, Java is interpreted. When you develop Java applications, you write the Java code as you would with any other language, then pass it through a Java processor to produce the binary object file. The binary object file, called a class file in Java, is not directly human readable, but can be transmitted faster than the source code from which it was derived. When a Java-equipped browser receives the class file, it runs it through the Java interpreter which decodes the class file instructions and executes them. In one sense, Java is both a compiler and an interpreter, in much the same way many early languages that produced pseudo-code requiring a runtime module (such as many Pascal compilers were). The Java client that decodes the class file performs a conversion from generic instructions contained in the class file to machine-specific instructions for the client’s hardware and operating system.

Programming in the Java language takes a little while to become comfortable with. The code often seems like a hybrid of C and C++, and experience with both is handy. However, because Java is a fairly simple language, even nonprogrammers can pick it up with a little practice. The most difficult aspects of learning Java for most people are the need to understand object-oriented design and programming, and the somewhat awkward syntax requirements (also these are familiar to C and C++ programmers). For example, here’s a simple Java program:


    // HelloWorldApp.java

    class HelloWorldApp {

      public static void main (String args[]){

        System.out.println(“Hello World!”);

      }

    }

This program (called HelloWorldApp) defines a single method called main, which returns nothing (void) and consists of a single Java instruction to print out the message “Hello World!”. The first line in the code is a comment. To compile this source code into a class file, you would invoke the Java compiler with the command line:


javac HelloWorldApp.java

and the compiler will grind away and produce a class file (HelloWorldApp.class) that can then be run in a Java-equipped browser, or from the command line client with the command:


java HelloWorldApp

which instructs Java to load the .class file and execute it. This applet must be run from the command line to see any results.

As you probably expect, Java is a lot more complicated than that in real life, but if you have programmed before you will see the similarities to other languages (especially C and C++). The Java language is quite rich and can take a couple of weeks to wade through, becoming familiar with its nuances as you go, but for most people Java is much more easily learned than other programming languages.

JavaScript and HTML

JavaScript commands are embedded inside HTML documents (see Chapter 53, “HTML Programming Basics,” for more detail on HTML) by enclosing them with the HTML tags <SCRIPT> and </SCRIPT>. The general syntax for a JavaScript program inside an HTML document looks like this:


<SCRIPT language=”JavaScript”>

  JavaScript statements

</SCRIPTt>


Previous Table of Contents Next