Introduction to Java           
P. Komisar 
©    Conestoga College       Latest revision  v.5.2   Jan /  2008

"Java is a general-purpose, concurrent, class-based, object-oriented
 language. It is designed to be simple enough that many programmers
 can achieve fluency in the language. Java is related to C and C++ but
 is organized rather differently, with a number of aspects of C and C++
 omitted and a few ideas from other languages  included. Java is intended
 to be a production language, not a research language, and so, as C. A. R.
 Hoare suggested in his classic paper on language design, the design of
 Java has avoided including new and untested features."

               -  'The Java Language Specificaton' -Gosling, Joy & Steeles


A Brief History of Computing



In learning Java, we are tuning in to an ongoing process of man-
machine interaction that
has been an intensely pursued for over a
half a century. This process finds it's beginnings
in the ideas and
machines that were spawn by the industrial revolution.

// old computers at over 60 years old qualify as antiques

Early Processors, Input & Output Devices

To give credit where it is due, the abacus holds the title of being
the oldest digital calculator
known to man.  The mechanical type-
writer was destined provide the key computer input device used
today. The type-writer stores information on paper and is still very
much in use today.
  Remarkably, the basic QWERTY pattern
Christopher Sholes adopted circa 1867 is still in use today.
The
classic typewriter had no processing ability.
If you could mix the
abacus
with a type-writer we might be getting somewhere.

// the abacus is a processor, the typewriter is an input/output device

Some looms in England were controlled by paper scrolls with holes
in them. The location of the holes were
interpreted mechanically to
place stitches into weaves of cloth. The complicated fabric patterns
such as the
Paisley design are well known today. The output of this
process was the woven cloth.

Output is another aspect we include in today's definition of what is
a computer. The
patterns these machines created were not unlike
the graphics we ask the computer
to produce today. These looms
were really the first digital sequencers as they took
sequences of
digital signals (there is is a hole, there isn't a hole) and interpreted
the
combinations of these marks as instructions. Being able to
execute a series of instructions
is a also part of the basic notion
we have of what constitutes a computer.

// looms were early digital sequencers, with scrolls for programs

Punched paper scrolls were also used to control scrolls in player
pianos. At
the turn of the century punch cards were being used
to record and process statistics on immigrants
coming into the
United States. 'Digital' has been around for a while!

A mathematician named Charles Babbage invented the first mechanical
calculator which
apparently frustrated him all his life. This sounds like
it may have been the world's first
real computer.
 
 
 Digital Thinking
 When we say digital thinking we are speaking of the fact that a machine
 really can only be instructed to do something using a switch of some sort.
 Turn the switch on turn it off.  This 'all or none' process is also called a 
 binary process as there are only two states involved.
 
 As with the looms a series holes or the absence of holes can represent 
 more complex instructions. While the loom and the piano scroll interpreted
 the presence or absence of a hole as a digital one or zero the electric
 computer sees combinations of switches turned 'on' or 'off' as instructions.
 
 A long row of switches representing one row in a matrix inside a square
 of silicon can be individually set 'on' or 'off' to create an instruction a
 machine can understand. This 'language' is the native language of the
 machine and is called machine language.

  A binary system counts the  same way a decimal system is used except,
 instead of changing columns when 10 is reached, columns are changed
 when 2 is reached. The following table shows how a hypothetical computer
 can count to 8.

 Decimal   Binary 
 0   0
 1   1 
 2   10
 3   11 
 4   100
 5   101
 6   110
 7   111
 8   1000



ENIAC

Computing didn't take off in the modern sense until the idea was
coupled with electricity.

The invention of the tube made possible the monster tube computers
such as ENIAC that
were created near the end of the Second World
War. The tube was an analog device. The tube
could allow different
flow rates of electrons to pass through it. It was like a continuously

adjustable valve. It could be operated digitally. It could be turned flat
'on' or 'off'. In this
sense it became a digital switch. This switching on
and off
was very hard on the tubes physically. The tube systems were
always breaking down and
generated a huge amount of heat.

// Turing's mechanical deciphering device used in the World War II
// was really an electro-mechanical computing device and could also
// contend for the title of the 'first real computer'.
Tommy Flowers,
// who worked for the British Secret Service developed
a computer
// called Colossus that predates ENIAC and is a strong contender
// for the 'title'. In fact ENIAC is an American knock
off of Colossus.

To the rescue came the transistor. The transistor run much cooler
than tubes and was much
smaller. This began the long lasting trend
of miniaturization of computers. The IC or Integrated
Circuit allowed
further reductions in size, allowing whole circuits to be put on a single
chip.
The terrific advances in computer technology have allowed
computers that once filled rooms
to be fit into devices you can now
hold in your hand.

// the transistor saves the day

The Internet and modern computer networks also owe their beginnings
to older technology. The telegraph and phone systems that were
developed late in the last century allowed communications across long
distances and laid the basis for today's global computer networks. 

// the Internet started as a single phone connection between
// two university mainframe computers


Von Neumann Model

The famous mathematician and physicist, John Von Neumann made a
study of the ENIAC computer and in 1946 proposed a virtual computer
where programs and data would be stored in primary memory that could
be accessed quickly and sequentially by a processor and long term
memory would be provide a secondary area to store information. The
virtual computer he described then is today called the Von Neumann
architecture and remains the blueprint for modern computer operation.
 

Diagram of the basic components of the computer

                               MEMORY
             /* primary memory loaded with the programs and data */
                                 |
/* keyboards or mice*/   INPUT --->  CPU ---> OUTPUT /* printer or monitor*/
                                 |
                              STORAGE
           /* Persitent memory devices like hard disks, tape or CDs */

 

Languages

Each computing device needs to be instructed using it's own particular
language or set of instructions which it is designed to understand.
You
couldn't, for instance, use a piano scroll to provide a pattern on a weaving
loom. The instructions
would make no sense. These two code examples
represent different machine languages custom
designed to work within
the context of that device.

Computer languages have grown from their early beginnings as
lists of instructions written in the native machine language
to
abstract high-level dialects that can be used on a large number
of different machine
types.

Machine Language

The first computers were programmed without languages! An operator
would bring in a switch board and physically switch the computer to
instruct it! The computer itself understands machine language, which
are sets of switch patterns that are applied to the processor's registers. 
In the early days, command combinations would have only been
informally documented. The machine was being programmed in the
rawest form in patterns that constituted it's machine language.

Assembly Language

It soon became apparent certain sequences would be repeated
over and over again. These instructions, were assembled into
short sequences that could be supplied using some medium to
the computer to save time. Whole collections of these assembled
instructions became known as the assembly language for the
machine.

Higher Level Languages

Later sets of assembly language instructions were collected in longer
routines or functions. Sets of routines became known as higher level
programming languages. Fortran is one of the earliest of these higher
level languages. C and Basic are also older examples.
 

Operating Systems

When storage, memory, and input and output devices were integrated
with the central processing unit, they collectively formed a 'system'. A
program was needed to coordinate their collective activity as a system.
This was the task that the operating system was invented to perform.
The operating system's main role is to coordinate the activity of the
collection of hardware devices that form the computer system.

The OS manages the allocation of memory, the activities of the IO
devices and all the other peripherals that make up the system. The
operating system also brokers the messaging that goes on between
the user via input and output devices, the programs that are running
on the system and the hardware that the programs are running on.

// In the early days of Microsoft, before Windows, DOS was the
// companies first software offering. It was based on CMP which
// later became DR-DOS. DOS or disk operating system, implied
// that one or the operating systems principle tasks was to coordinate
// the storage and retrieval of data from memory disks. It was
// designed to work on what was then the novel, Personal Computer
// or 'PC'.

// Unix had a different focus, founded in Bell Laboratories, where
// the focus was to serve many users supplying them processing
// as a utility. In this system, the storage is conceived as being more
// centralized, where one data source supplies storage for many users.

Another important role the OS supplies is loading itself on the machine
in what is called the 'boot' process. When a machine is turned on, the
first step that has to occur is the system has to 'boot'. What this means
is that the first program, the operating system must somehow install itself
on the machine. There is a short exchange between a storage disk and
the CPU to say where to start loading the OS code program from. The
program is loaded into short-term memory and all the initial variable
values that are needed to run the process are provided. Then the OS
effectively takes control of the computer system.

A Typical OS Operating Scenario 

Once the operating system is running, a shell is supplied to the user, either a
command
line prompt or a visual graphical user interface, (GUI). The shell or
the GUI is itself a
program that supplies a number of commands the operating
system defines which the
user can use. We might use the shell to input a request
for a program to run. The
operating system interprets this message and accesses
the hard disk to find the requested
program, transfers it to primary memory,
from where the cpu can run the contained list
of instructions. Any data that
might be generated for storage may then be written out to
the hard drive,
again coordinated by the operating system. Finally the system has to be
turned off in an orderly fashion, in a sort of reverse order of the boot
process. All these
activities are coordinated by the operating system.


Compilers and Interpreters

Languages like C require a compiler that processes a programmers
list of instructions into the binary, machine language that the processor
uses natively. Binary instructions are the most efficient and fastest
running program form because they interact directly with the machine's
registers at the machine language level. Some languages were created
to take advantage the shell commands that were provided by operating
systems. These scripts were not compiled, rather were interpreted to
run in an Operating System shell.

Some languages like Java and Pascal are both interpreted and compiled.
Why the extra steps? There are certain advantages conferred by the two
stage process which leads us to our discussion of Java.
 

Java, a Next Generation Language

Java is arguably what might be called a fifth generation language.
We can count machine language as first generation, Assembly
language as the second and the first high level languages, such
as Fortran and PL/1, as third generation languages.

The fourth generation comprised a number of newer high level
language were invented that were based on different approaches
to problem solving. Examples of fourth generation languages
were LISP and PROLOG. One of these new languages was
Simula 67 which Alan Kaye the inventor of SmallTalk. Simula 67,
presumably invented circa 1967, Kaye described as the world's
first object-oriented language.

It was Alan Kaye that formulated the key ideas of object-oriented
programming and the language he invented, SmallTalk became
the first widely known object-oriented language. Efforts were made
to make other languages object-oriented, and so hybrid dialects
like Nicklaus Wirth's Pascal were released in object-oriented versions.
(Delphi is the child of this movement). Also, Bjarne Stroustrup in
the early 80s created an object-oriented extension of C called C++.

Java may be described as the first 5th generation object-oriented
language, ( well at least four and a half) in that it builds on the strengths
of the fourth generation languages that preceded it.

The following table shows the sources that the Java language authors
consulted and referenced in the creation of Java.
 

Table showing the references used in creating the Java language
 
 The Java Language Specification 
  Gosling, Joy & Steeles, Abridged references (titles and authors)


 The following references show Java was built in light of the best features of  many
 languages, and to this extent is a next generation, state-of-the art programming language. 

1).Dylan Reference Manual, Apple Computer
 2) Common Lisp Object System Specification, Bobrow, Daniel G., Linda G. 
     DeMichiel, Richard P. Gabriel, Sonya E. Keene, Gregor Kiczales,and David A. Moon.
 3) The Annotated C++ Reference Manual, Ellis, Margaret A., and Bjarne Stroustrup
 4) Modula-3 Harbison, Samuel
 5) Hints on Programming Language Design. Hoare, C. A.
 6) IEEE Standard for Binary Floating-Point Arithmetic. ANSI/IEEE Std. 754-1985.
 7) The C Programming Language Kernighan, Brian W., and Dennis M. Ritchie.
 8) Object-Oriented Programming in the Beta Programming Language, Madsen, 
     Ole Lehrmann, Birger Møller-Pedersen, and Kristen Nygaard.
9) The MesaProgramming Language Pr, Mitchell, James G., William Maybury, and Richard Sweet.
10) The C++ Progamming Language,Stroustrup, Bjarne
11) The Unicode Standard: Worldwide Character Encoding, Ver.1.0 , The Unicode Consortium


This is a nice point to detour to a brief History of Java.

Java History                     
reference: 'Core Java', Horstmann & Cornell  


In 1991 a group of Sun engineers led by James Gosling
and Patrick Naughton set out to create a small computer
language for consumer devices. Because CPUs from
different manufacturers would be used, they wanted the
language to be platform independent . The project was
code named "Green". They resurrected USCD Pascal,
pioneered by Niklaus Wirth.

The Pascal programming language generated intermediate
code for a hypothetical machine, often called a virtual
machine, (hence the JVM or Java Virtual Machine). The
intermediate code could be used on any machine that
deployed the interpreter. It was advantageous that both
the intermediate code and the interpreter itself could be kept
quite small. Because the Sun people were from a C/Unix
background, they based their language on C++. Gosling
at first called the language 'Oak', but having discovered
another language named Oak the name was changed to
Java.

The Green project developed a 'smart' remote control no
one really was interested in. The group dissolved in 1994.
Meanwhile, in 1994, Mosaic was the most popular browser,
a non-commercial browser developed at the University of
Illinois.  (Mosaic was partly written by Marc Andressen who
was paid $6.85 an hour while he was an undergraduate
student. He later cofounded Netscape.) Gosling realized
they had a lot of code adaptable to building a browser so
they decided to create one. The original was built by Patrick
Naughton and Jonathan Payne and evolved into the
'HotJava ' browser. The browser was given the ability to
interpret the intermediary bytecode of applets written in
Java. The demo of applets being displayed in the 'HotJava'
browser at 'SunWorld' of 1995 is the 'flash point' from
where Java's popularity snowballed.

The breakthough in Java's use is attributed to Netscape's
adoption of Java in their 1996 version 2.0 browser. The
technology was licenced by IBM, Symantec, Inprise and
Microsoft. Release of Java proceeded from version 1.x to
1.1.x to the release of 1.2.x in 1998. Currently, 2006, there
are three releases of the the JDK, the Enterprise, Standard
and Micro editions. The most current standard JDK version
is now in version 1.5.x.
.
   


Java Builds in Network and Database Support

Java had the advantage of being developed at a time when many
Internet and database standards had been ironed out. The TCP/IP
protocol of the Internet had become the accepted Internet
communication protocol. HTML and HTTP were also established
as the dominant protocol being used on the world wide web.
SQL, the language used to communicate with relational databases
was also standardized in version SQL/92. Unicode the ISO supported,
character set which supports over 35,000 international was another
standard that was being largely accepted as a workable way of
organizing the many different characters sets that were in use to
describe world languages.

// Java was fortunately timed to incorporate many 'settled' information
// standards such as TCP/IP, HTML & HTTP, SQL and Unicode.

Java was created with Unicode being the default character set of the
language itself so it by nature lends itself to internationalization. Java
adds the ability to work with databases, networks and the World Wide
Web right into the language seemlessly via it's library packages.
 

Java, A Simpler Object-Oriented Language

From the beginning the Java programming language was designed
to be a simple and practical object-oriented language. Simple is a
relative term. Java is simpler than C++. It is really a sibling of C and
C++, sharing many features and much of the same syntax. It is simpler
than C++ as many features provided in C++ have been omitted in Java.

// skip the next paragraph if you are new to pointers

As an example, C++ has a couple of ways to reference variables,
either by the dot operator or by pointers. Java picks one way. Java
has the dot operator but drops the pointers. Dropping pointers also
gets rid of one of the areas where C and C++ tends to introduce errors
into code. Pointers are also difficult to read and maintain. By dropping
pointers the language becomes easier and the time it takes to develop
bug-free code is reduced.
 

The Java Virtual Machine

A platform is the unique combination of machine and operating
system. The machine is composed of a central processing unit (CPU),
a motherboard and all the peripheral items we need to interact with
the computer. Really what defines the platform at the machine level
is the machine language that the CPU uses. For instance both AMD
and Intel processors use the assembly language that has evolved
with the CPUs that have powered the IBM PCs since 1982. Though
the chips are produced by different vendors, computers that run on
x86 machine code are referred to as IBM PCs.

The other part of the platform is the operating system. There are a
number of different operating systems being used, including several
dialects of Windows and Unix, Linux (really a Unix variant), the Mac
OSes, BeOS and other less famous OSes. Each of these operating
systems if they were to run on the same machine would still represent
a different platform. A 'C' program would need to be compiled
seperately for each and every one of these platform it runs on.


Java Eliminates Platform Dependance

An executable file for a computer game like 'Doom ' that runs on an
PC running Windows 98 won't run on a PC running Linux or a Mac
running OSX or on a Sparcs machine running Solaris. This incompatibility
of different platforms has been a major problem for large organizations
from a user, communications and maintenance point of view.

Acheiving CPU independence was one of the reasons the authors
of Java selected to use an interpreter. The Java virtual machine
abstracts' away the differences between the machines and provides
a single 'virtual machine' to interface to a Java program.

This means a Java program compiled to run on the virtual machine
will run on any platform where the interpreter, the JVM, is installed.
Corporations are attracted to this as the Java Virtual Machine in a
sense creates the illusion that all the companies machine are running
the same platform.

// Java itself may be thought of as a platform


Disadvantages of an Intrepreter

The biggest disadvantage of the interpreter is that it slows down
a program's execution.
Not so long ago this would have been a
potentially fatal weakness. Thanks to the
relentless improvements
in new processors this is at the point of being a non-issue. Just

as a 286 struggled to render Windows 3.1, so did a 486 or early
Pentium struggle with
running Java with it's interpreter.

At processors speeds over 500MHz the interpreter's presence
became hardly noticeable. At speeds exceeding the 1000MHz
the interpreter's
effects are no longer a consideration. Today,
with machines running in the 3000 MHz range, coupled with
advances incorporated into interpreters, Java's speed is no
longer an issue.
 

Advantages of the Java Virtual Machine

The Java Virtual Machine is an interpreter, a translational program
that runs on different platforms. There is a Java Virtual Machine
(JVM) defined for each popular platform. Whether the Mac, the
PC running Windows or the PC running Linux,  the JVM takes
care of translating Java code which has been created to run on
the JVM to the platform which the JVM is running on.

Strong Typing   // Java types are one size fits all

// Types are the way information units are defined. Words
// are created using character types. Numbers are discrete
// or decimal. Each programming language defines types
// to represent different sorts of basic information unit.

Because the Java source code is written to run on a single
virtual machine, the details of the length and type of allowed
variables do not vary from platform to platform. Strong typing
refers to the fact for any variable type, it can have only one
size and set of properties. An int type is 32 bits long once and
for all and forever! A char type is a 2 byte, unsigned value
regardless of the platform, software vendor or operating system.
This allows Java to define a small, tightly group of variable
types to store data that will be consistently rendered regardless
of the machine the program is running on.

Improved Security  // JVM interpreter also does security screening 

This leads to improved security as every value can be tested
to see that it meets the criteria of strong typing. The Java Virtual
Machine also adds a security gateway at the junction where
class bytecode enters the interpreter. The JVM does different
types of runtime checking. One thing it checks for is to see that
arrays are not written out of bounds. Besides being a program
error this is also a security issue as this prevents bad things
from being written purposely to memory areas that are written
out of bounds. The runtime environment can also check objects
that are loaded dynamically (at runtime) to be sure they conform
to object type they are suppose to be.
 

Fewer Errors and Easier Memory Management

// remember we said the OS manages memory. Programs have
// to manage memory via calls to the OS. In languages like C if
// you allocate memory for some code and you are finish with that
// code, you have to take steps to free up the memory for re-use.
// Java has an automatic process that frees up memory for you

The Java Virtual Machine provides a background thread
called the garbage collector that collects memory that has
been allocated but is no longer being used. This gets rid
of a class of errors called memory leaks. This, coupled
with the fact that Java doesn't allow pointers another
major bug producer, makes Java less susceptible to error,
easy to debug and a faster environment to develop in.
 

JVM Terms

The Java compiler, javac, converts java source code into
architecturally neutral bytecode. The Java Virtual Machine,
started by the
java command takes this bytecode and
translates it into the given platform's native instruction set.


Java Compile and Execution Process


  Source Code
       |
       V
    javac // java compiler
       |
       V
Architecturally Neutral
    Bytecode
       |
       V
    java   // interpreter
       |
       V
Native Code execution

 
// stop and type in java, then enter and
// javac then enter at the command line

// assumes you have Java installed


Growth of Java Use

Since Java's public release in summer of 1995, the number of Java
users has more or less doubled every year. By the summer of 1999,
over 1 million copies of the JDK 1.2
were downloaded from the net.
By May of the year 2000, 3 million copies of the Java
2 Developer's
kit had been downloaded. A magazine article issued in January 2000

stated that the Java development community consisted of about 2.5
million users.
A report from Sun's JDK download site shows that as of
June 2001 there had been over 5 million downloads of the Java Standard
Edition.

Where Are We in 2008?

While not a hard indicator, O'Reilly supplies a recent trend
chart are based on book sales it appears,  Java would appear
to be the most used programming language today. One major
flaw, though, there are few books written about COBOL and
Fortran and yet they remain, in massive use throughout the
world.

http://radar.oreilly.com/archives/2006/08/programming_language_trends_1.html


The Java Microsoft War

At first Microsoft accepted Java. As it grew Microsoft tried to
co-op Java in it's support for J++. Finally Microsoft began
resisting Java openly by adopting a long term strategy to support
it's own C# language which is a hybrid of C++ and Java.

The Open Source Threat

Today Open Source software threatens commercial shops
like Sun and Microsoft. For instance a 2006 headline showed
Red Hat, the major Linux provider has bought JBoss the leading
open-source Java Web application to compete head on with all
other proprietary vendors.

Now there seems to be a sort of truce between Sun and Microsoft.
Scott McNeily at Sun has always made sure Java ran well on
Windows machines and now there are many excellent IDEs that
support Java to run on Windows.

It is probably the popularity of Linux and the open source movement,
which must be seen as a threat by both Microsoft and Sun, that has
brought these antagonists together at least for the moment.


For Those Who Come From C and C++

Java is like a simplified C++. It has no pointers, nor does it have
pre-processing. Also it is more object-oriented in that it has no global
functions or variables. (Even the main( ) method is enclosed in a
class.) Java also only supports single-inheritance rather than the
multiple inheritance supported by C++. (Java has a special structure
called an interface that defines abstract methods and constants.
Interfaces are exceptional and are allowed to multiply inherit.)
 

Device API's 'Small, Smaller and Smallest'

Recently Sun did some house cleaning and divided the various API's
into three divisions. The Java Micro Edition acts as an umbrella for
everything small and aimed at the comsumer and embedded market.
It includes the following older categorizations (API's) as well as spec's
for devices such as set top boxes, screenphone, wireless car phones
and digital assistants.

// should check in 2008 if these categories are still current
// For instance there is new moniker 'Wireless Java to cover
// these categories

Personal Java   -'small', for consumer device, core and opt. extensions, fully upwards compatible
EmbeddedJava   -'smaller', PersonalJava subset for smaller processing resourses i.e cell phones
JavaCard   -'smallest', for whatever processing you can miniaturize on a 'smart card'

  // ~2007 loaded java_card_kit2_2_2

The JDK releases which now refer to the Java Standard Edition to date are:

1)  JDK 1.0 and 1.0.2
2)  JDK 1.1. through 1.1.8 (current)
3)  JDK 1.2.x now called Java 2 sdk ver. 1.2 ,1.2.1 and 1.2.x
// + few more minor releases

4)   JDK 1.3.1
5)   JDK1.4
6)   JDK 1.5 // had major new features added
7)   JDK 1.6.x
.
  

The Enterprise Edition is a third version of the JDK which appends
to standard edition support for Servlets and Java Server Pages,
Enterprise Java Beans. In addition there are APIs such as the Java
Naming and Directory Interface, the Java Messaging Servive, Java
Transaction Service and Java Mail. The J2EE also has tools to test
and support these API's includes the CloudScape, all Java database. 
 

Applications, Applets, Servlets & Enterprise JavaBeans

The stand-alone application is one of four contexts for writing Java
programs. The
other three are essentially variations of the same theme
where the program runs in
a greater context supplied by another
program. The Applet,
Servlet and Enterprise JavaBean, all run in
the context supplied by another environment.

Applets

Applets run in the context of the web browser, referenced from
within the context of an HTML page. Applets lost favor for while
partly due to the time it required to get the java runtime environment
up and running. More pragmatic solutions like 'javascript' and filled
the space applets might have occupied. Applets have become
increasingly relevent with the advent of signed applets which can
be made secure for commercial and privacy interests. Personal
hardware has improved to the point that the overhead of running
applets is no longer as significant a factor. Applets are shining in
commercial web applications, providing complicated front-ends for
net applications, which connect back to the server via a socket or
by tunnelling the http protocol.
 

Servlets

Servlets are similar in architecture to applets but run in the context
supplied by a
web server. Servlets are providing a better means
of handling the web client / server interface than more traditinal
solutions like CGI. A cousin of servlets, are JSPs or Java Server
Pages that are a very popular means of blending java's con
nectivity
features into the context of the web page.
 

Enterprise JavaBeans

Enterprise JavaBeans are designed to provide data resource support
for the back
end of a companies network architecture. Again, like applets
and servlets, EJB's run in the context of another program. In the case
of Enterprise JavaBeans, the program is called the container.
 

Hello World in Java

An introductory discussion of a programming language would not be
complete without looking at a "Hello World" program in the language
being studied. Open a plain text editor and enter the following code.

Example

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

Analysis

Lets analyse this code. The first word is 'class'. This word is a Java
keyword, a word reserved exclusively for Java's use. It identifies
the structure which includes everything written inside the zone
defined by the first and the last curly brace. This range is called
the scope of the class. The class is the fundamental unit that Java
code is defined in. Most everything written in Java is contained
within the class definition. The only thing written outside the class
definition is stuff to do with packages which are directory level
structures that can be used to group sets of related classes. Notice
even the main method is included inside the class.
 

The word HelloPlanet following the class keyword is the unique
identifier that provides a name for the class. Identifiers can't be
keywords like 'class'.

The curly brace, following the identifier begins what is called the
scope of the class. It is a range or block that is ended by the
opposite curly brace located at the very end of the code.

Inside the class is one method. It is the main method. This is a
special method that defines the start or entry point of the section
of code that is contained in the class.

The main method has a return type which is void. This means main(  )
doesn't return anything. It has two modifiers, one public means that
the method is available from any Java object in any package.
It means public access. The static word means main exists only
in the class definition. It is defined only once.

The System.out.println("Hello Planet"); statement is a line which
prints the enclosed String literal, "Hello Planet" to console, in other
words the monitor.

The main method ends with a curly brace. The already mentioned
class ends with a curly brace and the code is finished. After this
we could tune into the Compiling and Executing Instructions
provided below, starting in at number 2. This list summarizes
the steps that are taken to write compile and run java code.


Compiling and Executing Java Source Code

1. Install Java
2. Create your source code in a straight ASCII editor
3. Name the file identically to the class name + the .java extension
4. Open a Dos/Unix window and type javac Name.java and enter // to compile
5. If the compilation was successful the Dos prompt returns, then type java Name
    // to run the interpreter
 


Exercise

                     

class Planets3{
  public static void main(String[] args){
    String s1= "Mercury";
    String s2="Venus";
    String s3="Earth";

    System.out.println("The planets of the solar system are \n" +
    s1 + ", " + s2 + " and " + s3  + ".");
   }
}
// The + signs can be used for addtion but also to concatenate strings
// together.
the \n is an example of what is called an escape sequence
// and is used here to
cause the output to be placed on a new line.
     


Exercise     


    
First you should run the HelloWorld class from the command
line. Then, run the Planets3 code as is. Finally finish the job
the programmer didn't finishby adding the missing planets.
Rename the class appropriately.

Do this by carefully imitating the code that is there.

Submit a paper copy of the newly created code together
with a screen shot of the command line output.  Title the
assignment 'Assignment 1, Introduction and put your name
at the top of the paper.