Index

Next

Chapter 1 What is Java used for?

1.1) The early days

In the early days of Java it was assumed that the main use of Java would be as applets, small Java programs embedded in web pages. Applets offered the benefit of a fully fledged programming language allowing developers to embed just about any functionality. Because they are accessed from a browser and loaded over the web they should simplify or remove many installation issues. A combination of performance issues and lack of standardisation has hampered the widespread adoption of Applets in web pages.

It was hoped that people would access fully featured applications like word processors and spreadsheets in the form of applets. This idea was hampered by a combination of bandwidth issues, and Java performance issues. It is not practical to access a multi-megabyte applet with a 64Kbps dial up connection and the widespread adoption of multi megabit broadband was slower than some optimists expected. When faced with the choice of spending tens of minutes to access a Java applet that then ran relatively slowly and using a locally installed application there was no competition. And yet Java has had enormous take up as a programming languages in other areas.

A Brief History of Java

It was the wide take up of the Internet that drove the development and acception of Java. At the time it was invented other programming languages made it hard to write Internet enabled programs.

Java was designed “from the ground up” with the Internet in mind. From the very start Java understood about the underlying protocols or rules of the internet. This is by contrast with other typical languages of the time such as C/C++ which generally required add-on libraries to talk to internet things such as web servers and . Java has increased enormously in size and performance since its invention. I used to have a copy of the first Java in a Nutshell by O'Reilly and it was a fairly slim book. The current versions require two quite fat books.

As more and more people have taken to using Java it has grown its own support system of add-on programs, web sites and books. Because of the early emphasis on Applets and their general lack of success, some people think that Java has not been a success. In fact it is usually the first or second most in demand technical skill if you search on Job sites. This is because it is widely used in situations that are not obvious to the general public. This can be in server side web applications (where the client just sees the HTML generated) and in internal developments, such as where Banks create systems to talk to their databases.

Applets, the desktop and server side

One of the most significant (and not widely predicted) uses of Java is for Server Side web programming, or the creation of Web applications.




One of the most significant uses of Java is for Server Side Web programming, with Servlets, JSP and EJB technologies

This is very different from the original focus on applets as the resultant applications generally run exclusively on the server and thus (within reason) will run on any browser. The web browser (Firefox, Internet Explorer, or Opera for example) sees a series of standard HTML forms and is unaware of what programming language is running on the server.

This is in significant contrast to Applets where the web browser needed to be configured up to run the right version of Java. Web applications are a huge area of growth with such Java technologies as Servlets, JSP and Enterprise Java Beans (EJB). Servlets and JSP are usually compared with Microsoft ASP and the Open Source PHP technology. It is worthwhile doing a search of your favourite jobsite on the key terms EJB and check out the salaries that are paid for this type of skill.

The database vendors have a huge need for portable software. Imagine you are running Oracle or Informix corporations and you need to develop for Windows XP,Vista, Mac OSX plus heaven knows how many flavours of Linux. Since the release of version 8.1 of Oracle in 1999 Oracle has had a version of Java integrated in the database. A Java programmer has suddenly come a long way to being an Oracle programmer.

Three applications that demonstrate Java as a general purpose language for desktop programming are Limewire, Azaurus and JdiskReport. Limewire http://www.limewire.org/ is a Peer to Peer client for the GnuTella network. This is similar to clients such as BearShare or eDonkey and allow people to search for and exchange files.

Azaurus is a bit torrent client that allows high performance peer to peer file sharing and can be found at

http://azureus.sourceforge.net/

The following screen shot of Azaurus shows how Java can be used to build a standard interface with common features such as pull down menus, icons and tabbed forms.



Why Choose Java?

Java is suited to creating general business programs rather than low level drivers, performance critical or operating system code. Typically it is used for accessing databases or writing networking systems. Some of the main alternative languages to Java are C++, Visual Basic and the new language from Microsoft called C#. You will sometimes see a description of a language as C/C++ because C and C++ are closely related. C++ compilers will generally compile any valid C language but most developers take advantage of the Object Oriented nature of C++.

C++ has a reputation for being able to create lower level programs than Java or Visual Basic and so is a natural choice if you are programming drivers or anything that involves getting "close to the metal" where performance is at a premium. Most programming is not of this type however and C++ does not come with a standard set of libraries for creating graphical programs.

There are two very different versions of Visual Basic, VB classic, i.e. Any version before VB6 and VB.NET. VB.NET shares some of the object oriented concepts of Java, whereas VB classic is far more like “traditional” programming languages like Pascal and C. VB is probably slightly easier to learn than Java and although it is strictly a Windows platform it comes with tools for most of the popular business requirements. It can can connect to most can, and can create most types of graphical widget required for standard business applications. VB classic suffers from its heritage as a descendent of the original BASIC programming language in that the primary purpose of the original language was that it should be easy to use, rather than being optimised for software engineering.

The C# language is the joker in the pack when selecting a language bei.e.,e Microsoft recognised that Java included some very good ideas and "borrowed" many of the concepts to produce a new language that blended many of the virtues of C++ and Java. Although there are projects to make C# available (notable the Mono project http://www.mono-project.com/Main_Page ) on platforms other than Windows, the main platform at the moment is Windows and for commercial purposes it should probably be considered non portable. To write a C# program that is portable to Linux you would need to abandon some of the more attractive windows specific benefits of the environment. The good news is that C# is sufficiently like Java that if you learn the one it will not be hard to learn the other. By contrast Visual Basic has very little in common with C/C++ or C#.

The Java Development Kit

The standard tool for developing Java programs is Suns Java Development Kit or JDK. This is freely downloadable from the Sun website and is updated according to the current version. So for example at the time of writing the current version is JDK1.5 The current version number is often represented as a point release, i.e. JDK1.5.0.1. The JDK is under constant development and each new release brings new features, performance and bug fixes, so it is generally a good idea to download the latest version.

The JDK is a a very "bare bones" tool that expects you to work from the command prompt, i.e. That black box under Windows that runs cmd.exe or possibly command.com.

If you havaever used the command prompt you can access it by selecting the start menu, and then selecting run and enter cmd. If you are using the Linux oi.e.,ting system then the JDK is accessed via the shell (usually a program amusingly called bash). This is no place for a lesson on the details of using the shell in operating systems but you can read a little more about the Windows shell at

http://www.bleepingcomputer.com/tutorials/tutorial76.html

Graphical Development Tools

If the thought of using the command prompt really does sound too troubling for you, don't worry, you can download a free graphical tool with the kind of menus, help and prompts you would expect from a modern system. These are sometimes called Integrated Development Environments or IDE and they can include debuggers and visual design tools that allow programs to be created using “drag and drop” from a palette of controls. One of the problem with this type of tool when you are a beginner is that the interface can “get in the way” of understanding what is happening in the underlying language. In the examples I give I will assume you are using the “naked” JDK but you may find an IDE suits you better.

There are several freely available IDE's available, but I am going to recommend NetBeans from http://www.netbeans.org . Netbeans itself is a Java program and so will run on many different types of computer from an Apple Macintosh, a Windows machine to a Linux or Unix machine. Two other freely available IDE's worth looking at are eclipse (http://www.eclipse.org) and and Dr Java (http://www.drjava.org).Eclipse is a huge project on a similar scale to NetBeans but Dr Java might be more appropriate to a beginner as it is a fairly small program that offers the key benefit of an interactive debugger without an excess of other features to distract the learner.

One of the very useful features of an is is is “code completion”. This is where the editor has a built in knowledge of the supporting classes and offers suggestions to help you complete each line of code. The following screen shot illustrates how NetBeans pops up a list of the possible methods that are available to the System.out. Class.



Quick Start Guide

Summary

andava.sun.com and download the latest version of the JDK for your operating system.

For the purpose of this book the version of Java you need is called Java2 Standard Edition (J2SE). You may come across the term J2EE which stands for Java2 Extended edition which includes support for more advanced features such as Enterprise Java Beans, which you can safely ignore until you are truly familiar with the basics of the java language.

The naming convention for Java is very confusing, because the names used seem to vary according to who is writing about it within Sun. Thus the version at the time of writing is called both Java 5 and Java2, but the development kit is called JDK1.5. Because the JDK naming convention seems to have been most consistent I'll refer to that.

The home page for J2SE is

http://java.sun.com/j2se/

Unfortunately Sun seems to change the pages quite a bit for downloading Java but the J2SE homepage has a popular downloads section on the right hand side that will usually indicate the latest greatest and most popular version. Once you select this option it can still be slightly confusing as the most prominent link tends to be the bundled combination of the NetBeans IDE and the JDK. I am recommending you go for the plain JDK, as working with NetBeans is a whole additional set of learning, even though it is a very powerful and impressive tool.

The JDK download is available in either an on-line or off-line version. I recommend the off-line version as this downloads all required files and can easily be transferred to another machine. The on-line version is simply a small “stub” program that performs the download. The actual JDK download for Windows is a standard executable (i.e. An .exe program). You will need either a good Internet connection or plenty of patience as the JDK is over 50 megabytes. Once installed the JDK expands significantly. The version I installed at the time of writing occupied around 180MB of disk space.

I also recommend you download the API documentation, which is a separate download to the JDK. The API documentation is in the form of HTML files and is an excellent technical guide to how to use the code and classes in the language, but again it does occupy a reasonably significant amount of disk space. You can also view the contents over the web at

http://java.sun.com/j2se/1.5.0/docs/api/

You can find some very useful commentry on the JDK in Roedy Greens Java Glossary at

http://mindprod.com/jgloss/jdk.html

I
llustration 1: API Documentation




What do you get with the JDK?

As you can tell from the amount of disk space it occupies the JDK contains a lot of files. From a user perspective the two most important programs in the JDK are

javac.exe (the java compiler)

and

Java.exe (starts the jre)

The Java compiler is the program that converts source code (the text you write when you create a program), into the bytecode that runs through the Java runtime environment. The output of the compilation process is a file with the extension *.class. Once you have a *.class file you no longer need the source code (*.java) file.

Before it generates the bytecode the compiler checks that the code is correct. For example it checks that every opening brace { is matched with a closing brace }. Once your operating system has associated files with the extension *.class it should be possible to click on them to start running.

The JDK also supplies a program called jar.exe which will wrap up multiple *.class files into a single file with the extension *.jar, which the java.exe program will run in the same way as a *.class file.

In addition to the compiler the JDK includes 35 other executables including a debugger and decompiler, but in over five years of intense use I have hardly ever used any of those programs. Debuggers are very useful tools to programmers as they allow you to track down errors in the logic of your program. Unfortunately the debugger that ships with the JDK has the interface from hell, and there are many alternative and usable alternative debuggers, notably the one that ships with the NetBeans IDE, and the Dr Java development system. It seems unfortunate that they did not make the debugger in the JDK a little easier to use, as installing NetBeans seems an excessive amount of time trouble and effort to get this ease and functionality.

JavaDoc and comments

Comments are directives to the programmer and have no meaning for the Java compiler. Comments to not add to the size of the output executable or make any difference in terms of performance. The only thing that the Java compiler cares about is that they are correct in terms of allowing it to ignore them. You should try to get into the habit of documenting all the code you write. Sooner or later you will come across a piece of code that makes you curse the idiot who wrote it, only after a short while you realise it was actually you that wrote it. It is sometimes surprising how quickly you can forget code that took a great deal of time to write only a short while ago.

Java has three types of comments which can broadly be described as old C style multi line comments, single line C++ style comments and JavaDoc comments. The oldest style of comments requires a /* to open and requires a */ to close. Thus the following will work

/* This code was written 
by Marcus Green 
And can cover many lines
*/

The C++ language lead to the introduction of a new single line style of comment thus

//This is a single line comment

This type of comment was so popular it was introduced to the C language. The final JavaDoc style of comments is really a slightly modified version of the multi line comment. JavaDoc is so incredibly useful I now look for similar facilities in other languages. I spent a great deal of time looking for the equivalent tool for Visual Basic 6 but with no luck.

The JavaDoc facility allows the generation of programmer documentation merely by extracting the contents of specially formatted comments. The jdk comes with a utility called JavaDoc which parses (walks through) code to generate documentation.

Thus if you see the following comment

/**
*@author Marcus Green
*
/**

The fact that the comment has the extra asterix (/**) indicates that this is a javadoc style comment and the contents should be extracted. The @author tag indicates that this should be included in the author field of the final output documentation. Note that all of the API documentation is the result of the source code of Java including JavaDoc style coding. The nice thing about JavaDoc is that it is possible to maintain the api documentation “as you go along” and there is no need to keep separate documentation that might get out of synchronization with the actual code.

What is a debugger?

A debugger doesn't magically remove the bugs from a program, but it is very useful in tracking them down. A debugger is a program that allows you to run a program “in slow motion” and to step through the source code line by line to see what effect each line has on the program. Debuggers are available for most programming languages. I have used Java debuggers in many different development environments, some free and some paid for. Out of these the one in NetBeans is easily my favourite, partly because of its easy to understand graphical interface.

The NetBeans debugger allows you to set a breakpoint, this is where the debugger stops the program when a certain condition is met. For example if you were looping through several thousand records in a database you might want the program to stop after one thousand records or where the first surname called “smith” was read from the database. Because a production program may have large numbers of variables the debugger allows you to specify what variables you want to watch.

Computers and Programming languages

Everything that runs on a computer starts out as code for in a programming language, a sequence of text and numbers that tell the computer what to do. The earliest computer programming were very complex and required an intimate understanding of the way the computer hardware works. As computers have become more powerful languages have hidden more of the details of the computer and allowed the user to concentrate on solving the problem rather than worrying about what the computer is doing.

Java is a programming language created by Sun Microsystems (http://www.java.sun.com) and available for free download over the Internet. It has been available since the beginning of 1996 and has aroused huge interest both as a general programming language and for its Internet capabilities. It is one of the most highly sought after skills, both in industry and universities. Programs developed with Java can be sold (or given away) without paying anything to Sun Microsystems.



Java is one of the most sought after skills, both in business and in universities.

One of its exciting features is that programs written in Java can run on many types of computer without modification. Thus a program written on a Windows computer can be run on a Apple Macintosh a Linux or a Unix computer. Sun created the slogan “Write once, run anywhere” to describe this ability. As well as general purpose programs Java is used extensively for "Server Side" web programming, where the programs run on the web server and the user just sees the pages using any web browser. Versions of Java are also available from other organisations, notably from IBM, and Java has become the de-facto standard for downloadable games for mobile phones.

Java: Compiler or Interpreter?

This is somewhat non essential background information, but it does put into perspective some of the design and performance considerations. An interpreted language is one where another program reads in the source code each time the program runs. That other program is the interpreter. The process of reading the source code and working out what it means (parsing) tends to mean that purely interpreted programs are slow. As a result very few commercial programming languages are interpreted.

A true compiler generates code that can be natively executed by the computer (or at least the operating system). In the windows environment the output of a compiler will have the extension *.exe (think executable). Natively executable programs have a reputation for speed, and for this reason the C and C++ languages which generate native executables are the languages of choice where the most important factor is execution speed.

Java is a byte code compiler, the output is an executable that can be understood by a “virtual machine” that runs as a separate process on the target computer. This approach gets rid of the overhead of reading and interpreting the source code each time the program runs, but it still has the overhead that it needs the runtime environment to allow the program to run. The biggest overhead is the start up of the runtime environment, which means a Java program will generally be slightly slower than the equivalent compiled program. The performance of Java versus other languages is a source of considerable controversy, but in my view, once a Java program have started the performance is indistinguishable from programs written in other languages.

Java Safety

One of the design goals when Java was created was safety. “Traditional” programming languages such as the C and C++ languages have been vulnerable to what is called the buffer overflow whereby a malicious user can inject rogue code into a program to gain additional privileges. By its design Java makes this type of attack somewhere close to impossible. Because of its built in safety Java does sacrifice some performance, but for many situations this is a price that is well worth paying.

In addition to runtime safety, when used to run Applets java provides what is known as the sandbox whereby code cannot access or modify resources on the client machine. For example it cannot read or update local or copy local files. Because of this there are virtually no known Java based viruses.

Where did Java come from?

The origins of Java can be traced back mainly through the C and C++ languages. The C programming language which dates back to the 1970's was one of the first "Portable" languages, meaning that it could run on many different types of computer. This was a huge advantage but the language was hard to learn. The C++ language was like C but was slightly easier to learn and included the ability to create classes, one of the central ideas behind Object Orientation. Around the time that Java was being developed The designers of Java decided to take advantage of the number of users who understood C++ and designed the language to be easy for C++ programmers to use.


Java was designed to be easy for C/C++ programmers to learn

Although C and C++ programs could be portable, they needed to be re-compiled for each system that they were to run on. Thus the executable program created for a Linux computer could not run directly on a MS Windows computer. Although it may sometimes seem that “everyone runs windows”, if you consider that a modern smart mobile phone is a sophisticated computer there are probably more mobile phones than desktop computers and the increase in the number of “embedded devices” such as Personal Digital Assistants is faster than that of desktop computers.

To be able to run on a different machine such programs needed to be re-compiled for the target machine. This could often be a complex and error prone process. By contrast, Java programs are what is called binary portable.


Java programs are binary portable, they run on different platforms without re-compilation.

Thus the output of the compilation process can be run directly on target machines with no further intervention.

Java is not JavaScript

Javascript was originally developed by Netscape under a different name (live script) as a lightweight scripting language embedded in the browser. Unfortunately it was decided to rename this technology as JavaScript which has caused huge amounts of confusion. JavaScript is embedded within web pages as plain text and is generally visible to anyone who knows how to look at the page source code. JavaScript is a relatively simple language that is commonly used to make web pages more interactive through features such as image rollovers where an image changes when your mouse hovers over it.

By contrast used with web pages Java is used in the form of applets, it can be used for almost anything a general programming language can do and the source code is not visible to people browsing a web page. Applets have generally fallen out of favour, partly because of industry politics, but they are still used for some specialist purposes such as online banking systems and scientific purposes.

Portability

There are two main ways of achieving programming language portability, either native executables or have a layer that executes an intermediate form of execuable. With the native executable approach the compilation system takes the original source code all the way through to the format of the required native executable. This requires an intimate knowledge of the operating system and underlying CPU, the brains chip of the computer, the head office and controller of the entire system.

An example of this is approach is the free GNU GCC compiler tool. This C/C++ compiler can output native executables for about 60 different CPU chip/Operating System platforms. You can download a free tool that allows you to use GCC under windows and it feels like just another compiler system.

By contrast the Microsoft C++ compiler supports the Windows Operating system and the Intel CPU chip (sometimes called the Wintel system). You might think that this is not such a problem as windows and that hardware/software platform seems to be in every desktop computer you have ever seen.

However desktop computers are the minority of computers in use in that there are many more “hidden computers”. According to a 2005 article in the Economist magazine

There is almost one mobile phone for every person in much of the developed world, according to new figures from the OECD. In Luxembourg, phones outnumber people, since many people who live in neighbouring countries have a second handset for use within its borders.“

http://www.economist.com/printedition/displayStory.cfm?Story_ID=4351974

More and more of these phones are smart phones with a fully operating system and the market for applications that will run on them is potentially vast.

The other way of achieving portability is to create a program that mimics a generalized version of a CPU, i.e. a virtual CPU chip. Then you create a “runtime environment” for the programming language so that it thinks it is always running on the same CPU/Operating system combination. Then when you need to port the whole language to another platform you only need to port the underlying virtual CPU rather than modifying the whole chain that generates a native executable. The description I have just given is very high level and an over generalisation, but in essence it makes creating a portable programming language easier. It tends to bring a drawback in that there is a performance overhead involved in the virtual CPU but with the enormous increases in CPU power this can be considered an acceptable price to pay.

The following diagram shows how portability works with C++, typically with something like the GNUCC compiler tool chain.

By contrast Java portability always outputs the same .class binary execuable no matter what the underlying platform. Thus the Java .class file that you run on a Linux machine is exactly the same as the one you run on a Windows machine. In fact I remember the thrill the first time I took a .class file I had compiled on my early Linux machine and executed it with no change whatsoever on my Windows 95 machine. Of course it simplifies matters that you only have a single executable to distribute rather than offering multiple executables according to platform.



Other sources

The history of Java according to Sun
http://www.java.com/en/javahistory/

Another history of Java
http://ei.cs.vt.edu/book/chap1/java_hist.html

Java according to Wikipedia http://en.wikipedia.org/wiki/Java_programming_language

The Java Language according to Sun
https://java.sun.com/docs/books/tutorial/getStarted/intro/definition.html

http://www.examulator.com/fjt



Previous
Index

Next