Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Overview of Java

INTRODUCTION

Java is a programming language and more. It originated from Sun Microsystem's Oak project and Sun still develop, maintain and supply it. Although it's not Open Source nor delivered under the GPL license, much is available as a no-charge download from Sun's various web sites.

At its first release, Java primarily used as a language for writing applications to be embedded in browsers (known as applets), but it has grown and grown into many other areas; these days, applets are very much a minority use of Java, although still an important one. Other uses include web server side programming (using "servlets" or "Java Server Pages") and large scale enterprise wide applications using resource servers, "Enterprise Beans" and more.

THE FUNDAMENTAL ELEMENTS OF JAVA

Java is an object oriented language, and all code you write is organised into classes. If you structure the way a class is defined and called according to certain rules, then that class may be usable as a program, or as an applet, or as a servlet. The code you actually write ("source code") is English-like text, and you save it into a regular text file, just as you do with other programming languages.

SOURCE CODE

Let's see an example of the source code of a Java program:

// Tiniest of programs to check compile / interpret tools

public class Hello {

public static void main(String[] args) {
        System.out.println("A program to exercise the Java tools");
        }
}

We've saved this example into a file called Hello.java. Note that the file name should be the same as the class name declared in the file followed by ".java". This rule can be broken with some environments / compilers, but it's a good rule to follow most of the time. Java is case sensitive; note that we have started our class name with a capital, followed by lower case letters - another suggested convention.

CLASS FILES

Some languages (Tcl, Shell, Jcl are examples) are interpreted and run directly from the source code, but Java isn't one of those. It's a language that's designed to run quickly, and interpreting the whole thing p-u-b-l-i-c etc. every time it's called is mighty inefficient, so we compile the Java into a binary format. Let's use the "javac" program, supplied by Sun as part of their free downloads, to do the conversion:

bash-2.04$ ls
Hello.java
bash-2.04$ javac Hello.java
bash-2.04$ ls
Hello.class Hello.java
bash-2.04$

If things are OK, javac doesn't produce any message but if your source code isn't in the correct syntax or refers to something that doesn't exist, you'll probably get an error message at compile time:

bash-2.04$ javac Oops.java
Oops.java:5: cannot resolve symbol
symbol : class string
location: class Oops
public static void main(string[] args) {
                        ^
1 error
bash-2.04$

Edit the source file, correct your error, save the file and compile again. You may have to go round this cycle a number of times until you get rid of all your errors. By the way, the mistake here was that we used a lower case s not a capital S for the words String.

The class file (once you compile successfully) is in a published binary format, but it's very rare for most programmers to have to get involved at that level. Suffice to say at this stage that class file is compact and is independent of host computer architecture - unlike most compilers, javac does NOT produce a snippet of executable code tuned for the particular processor architecture on which it is run. You might like to see what the format looks like - here's a binary dump:

0000000 ? ? ? \0 \0 \0 . \0 035 \n \0 006 \0 017 \t
0000020 \0 020 \0 021 \b \0 022 \n \0 023 \0 024 \a \0 025 \a
0000040 \0 026 001 \0 006 < i n i t > 001 \0 003 ( )
0000060 V 001 \0 004 C o d e 001 \0 017 L i n e N
0000100 u m b e r T a b l e 001 \0 004 m a i
0000120 n 001 \0 026 ( [ L j a v a / l a n g
0000140 / S t r i n g ; ) V 001 \0 \n S o u
0000160 r c e F i l e 001 \0 \n H e l l o .
0000200 j a v a \f \0 \a \0 \b \a \0 027 \f \0 030 \0
0000220 031 001 \0 $ A p r o g r a m t o
0000240 e x e r c i s e t h e J a
0000260 v a t o o l s \a \0 032 \f \0 033 \0 034
0000300 001 \0 005 H e l l o 001 \0 020 j a v a /
0000320 l a n g / O b j e c t 001 \0 020 j a
0000340 v a / l a n g / S y s t e m 001 \0
0000360 003 o u t 001 \0 025 L j a v a / i o /
0000400 P r i n t S t r e a m ; 001 \0 023 j
0000420 a v a / i o / P r i n t S t r e
0000440 a m 001 \0 \a p r i n t l n 001 \0 025 (
0000460 L j a v a / l a n g / S t r i n
0000500 g ; ) V \0 ! \0 005 \0 006 \0 \0 \0 \0 \0 002
0000520 \0 001 \0 \a \0 \b \0 001 \0 \t \0 \0 \0 035 \0 001
0000540 \0 001 \0 \0 \0 005 * · \0 001 ± \0 \0 \0 001 \0
0000560 \n \0 \0 \0 006 \0 001 \0 \0 \0 003 \0 \t \0 \v \0
0000600 \f \0 001 \0 \t \0 \0 \0 % \0 002 \0 001 \0 \0 \0
0000620 \t ? \0 002 022 003 ¶ \0 004 ± \0 \0 \0 001 \0 \n
0000640 \0 \0 \0 \n \0 002 \0 \0 \0 006 \0 \b \0 \a \0 001
0000660 \0 \r \0 \0 \0 002 \0 016
0000670

THE JAVA RUNTIME ENVIRONMENT

How do we use a binary class that won't run on your computer? We run it though another program known as a Java Virtual Machine (JVM) which interprets each of the elements of the class file and performs the actions stipulated.

There are going to be many things that you want to do in your Java that others also want to do - things as simple as outputting text. So along with the JVM, Sun provide a large library of extra classes which give you the facilities you need without having to write them yourself. There are so many extra classes that they're arranged into "packages" to make them more manageable, and the combination of the Java Virtual Machine and these standard packages is know as the Java Runtime Environment or JRE.

Let's run the example program that we compiled earlier in the "java" JRE, which lets us run an appropriate class as a standalone program:

bash-2.04$ java Hello
A program to exercise the Java tools
bash-2.04$

EXERCISE

Log in to the system that you're using for the course, and use any editor of your choice (the tutor will be able to help you find an editor that suits you personally) to create a file containing a Java program similar to ours, but which displays the message "This is my first Java Program" rather than "A Program to exercise the Java tools". Compile it through javac, and run it through Java.

THE JAVA WORLD

Well House Consultants courses (of which this is a module) specialise in the teaching of programming languages and their application, so you'll be going on in subsequent sections to cover the details of what to put into the source files and (unless this is a compressed course) how to think through the design of your application and source code so that it's well structured, easy to use, easy to maintain, and easy to update in the future as your customer's requirement(s) develop. At first, a lot of this may seem very theoretic, so here we'll give you a brief glance further into the Java world that you're headed for.

JAVA DEVELOPMENT ENVIRONMENTS AND TOOLS.

As your Java source code grows in size, you'll find that it's quite a task to keep track of all the various classes and other components involved. Development environments such as JBuilder and Forte provide you with facilities to make this management much easier, and with shortcuts that let you enter and edit code far more efficiently than you could with a standard editor. We find that it's best to teach you the fundamentals of the Java language before we expose you to these tools - otherwise, you're likely to find yourself spending a great deal of time trying to understand some of the suggested code and options that the tool offers but which we haven't yet covered.

Sun's distribution includes a number of tools in addition to Java and javac, including: jar a tool for creating and manipulating Java archive files (jars) javadoc generates API documentation from source files javap generates a human-readable description of the API of a class file jdb a text-based debugger

Larger projects such as Java are ideally suited for involving considerable build management. Apache Ant is a Java-based build tool; in theory, it is kind of like Make, but without Make's wrinkles. It's open source, and becoming very popular. See http://ant.apache.org/ for further details.

JAVA RUNTIME ENVIRONMENTS

Stand alone programs probably aren't what you'll be writing in the longer term, even though they're excellent for learning the fundamentals of Java - later on you'll be slotting your classes into other Java runtime environments, some of which are open source and other are commercial products.

Server side, there are a number of environment interfaces you may make use of include: Servlets Executable programs with a web interface JSPs Embedding server executable content within a web page RMI and EJBs Providing object servers. JREs that support these interfaces include Apache Tomcat, BEA WebLogic JRockit and IBM's WebSphere application server.

Client Side, most users want to access information via their browser these days and plug ins that support Java are available for most common browsers. Java is built in to certain browsers too, but do beware that it's often an old (or even ancient) versions. Appletviewer, a part of Sun's distribution, is a JRE with the same interface that a browser provides but without the caching or overhead of a browser and is useful for development and testing.

And it turns out that the JRE that you use for stand alone programs can do much more; you can have what looks like a stand alone program at "the top" but have it act as a client or server (or both!), you can have it provide a graphic user interface (GUI) in much the same way that an applet would, and much more.

JAVA DISTRIBUTIONS

For one programmer, Java might be the tool he uses to program a toaster. Another might be using it to provide the core financial accounting services for a major bank. Is it possible for both of these requirements to be met by the same downloaded language? Yes and No.

Java is described as a "simple" language and indeed at it's centre it is. The syntax is not overcomplex, the facilities of the language itself are relatively few, which make it into something of a lean and mean core facility. The real power comes in the standard packages that are available, and that's where the requirements of our domestic appliance programmer will vary from the requirements of our banker.

The Java 2 Standard Edition (J2SE) provides provides the essential compiler, tools, runtimes, and APIs for writing, deploying, and running applets and applications in the Java programming language. All developers will need to download a copy of J2SE, or its equivalent (and you'll find that many of the development environments we mentioned earlier include it).

The Java 2 Enterprise Edition (J2EE) technology and its component based model simplifies enterprise development and deployment. The J2EE platform manages the infrastructure and supports the Web services to enable development of secure, robust and interoperable business applications. The download is essential extra classes and environments to use in addition to the J2SE, which you will also need.

The Java 2 Micro Edition (J2ME) specifically addresses the consumer space, which covers the range of extremely tiny commodities such as smart cards or a pager up to the set-top box. This download provides a highly optimised runtime environment; again, to develop code you'll also need J2SE.

All of the above can be downloaded from http://java.sun.com

Also available is a Java Runtime environment (J2RE) which will be required by users rather than developers - it includes the JVM and the classes that make up the JRE, but not tools such as the compiler. Different licensing rules apply.

JAVA STANDARD PACKAGES

Much of the power of a Java application is vested not in the language itself, but in all the standard classes provided and optional classes available. There are so many classes that they've been organised into bundles (called packages) for easier management. The first release of Java included 8 packages - these days it depends on just what edition you're talking about, but you'll probably find you have somewhere over 100 packages in your JRE.

Standard package names start "java." or "javax." - for example, there's java.lang to provide basic language facilities, java.net to provide network access, javax.swing to provide the Swing GUI and javax.servlet to provide Servlet support. You'll come across many more standard packages as you learn Java; as a rule of thumb, if the package name starts "java" or "javax", it's standard, but if it starts with something else like "com" it isn't.

JAVA VERSIONS

Java started off as Java release 1.0, and has progressed through to (currently) Java 1.4; Sun have been very conservative in moving the release numbers forward.

Java 1.2 was a major step forward, with a tripling of the number of packages provided, and at that point Sun rebranded the new version the "Java 2 Platform".

Although Java is designed to be processor and operating system independent, you need to be careful as you develop code that you do so for a runtime environment that will be available to your user. See how the number of standard packages / classes has increased:

     classes packages
   Java 1.0 212 8
   Java 1.1 504 23
   Java 2 1.2 1520 59
   Java 2 1.3 1842 76
   Java 2 1.4 2991 135

As well as the extra packages, there have also been some language changes such as an assert statement added to the language at release 1.4.

Note that there is a very large installed base of browsers running Java 1.1, so you may still want to use it for writing simple applets. If that's the case, you'll need to obtain a copy of the JDK (Java Development Kit) for that release. The JDK became the SDK (Software Development Kit), now Java 2 SDK Standard edition, Version 1.4.


See also Java training

Please note that articles in this section of our web site were current and correct to the best of our ability when published, but by the nature of our business may go out of date quite quickly. The quoting of a price, contract term or any other information in this area of our website is NOT an offer to supply now on those terms - please check back via our main web site

Related Material

Java Introduction
  [25] - ()
  [111] - ()
  [124] - ()
  [317] - ()
  [792] - ()
  [871] - ()
  [1049] - ()
  [1158] - ()
  [1418] - ()
  [1466] - ()
  [1497] - ()
  [1557] - ()
  [1766] - ()
  [1908] - ()
  [2115] - ()
  [2423] - ()
  [2536] - ()
  [4332] - ()

Hello Java World
  [2414] - ()
  [2859] - ()
  [4350] - ()

Review of Java Basics
  [1082] - ()
  [4394] - ()

Java - General
  [2087] - ()
  [2091] - ()
  [2114] - ()
  [2417] - ()
  [2420] - ()
  [2504] - ()
  [2861] - ()
  [3573] - ()
  [4305] - ()
  [4317] - ()
  [4412] - ()
  [4430] - ()

resource index - Java
Solutions centre home page

You'll find shorter technical items at The Horse's Mouth and delegate's questions answered at the Opentalk forum.

At Well House Consultants, we provide training courses on subjects such as Ruby, Lua, Perl, Python, Linux, C, C++, Tcl/Tk, Tomcat, PHP and MySQL. We're asked (and answer) many questions, and answers to those which are of general interest are published in this area of our site.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/solutions/java-ove ... -java.html • PAGE BUILT: Wed Mar 28 07:47:11 2012 • BUILD SYSTEM: wizard