Introduction to Java Programming

A variant of Java programs called applets can be embedded inside a web page
and execute on the computer that is viewing the page, automatically and in a ....
In the Basic (or C, or C++) programming language, for example, you can create
code that specifies to multiply two integers 1000 and 2000 and store the result as
 ...

Part of the document


Introduction to Java Programming

Java is a powerful object-oriented programming language introduced by Sun
Microsystems in 1995, which has built-in support to create programs with a
graphical user interface (GUI), utilize the Internet, create client-server
solutions, and much more. Programs written in Java can run, without change,
on any of the common computer operating systems Windows 95/NT, Macintosh,
and Unix. A variant of Java programs called applets can be embedded inside
a web page and execute on the computer that is viewing the page,
automatically and in a secure environment.

As a language, Java is closely related to C++, which is also object-
oriented but retains a lot of idiosyncrasies inherited from its predecessor
language C. Java has removed the inconsistent elements from C++, is
exclusively object-oriented, and can be considered a modern version of
C++.[1] Because of its logical structure Java has quickly become a popular
choice as a teaching language,[2] and because of its extensive Internet
support and the promise of writing programs once and using them on every
operating system Java is becoming more and more accepted in industry.


Definition: Basic Java Programming Guidelines

Every Java program must follow these guidelines:


. Java is case sensitive, i.e. the word Program is different from
program.
. Curly brackets { and } are used to group statements together.
. An executable Java program must contain at least the following
lines as a framework:


public class Name
{ public static void main(String args[])
{ ... program code ...
}
}


. Every statement whose next statement is not a separate group must
end in a semicolon.
. A Java program containing the above framework must be saved using
the filename Name.java, where Name (including correct upper and
lower cases) is the word that follows the keywords public class and
the file extension is .java.

[pic]

In other words, to create a Java program you first create a text file
containing the lines

public class Name
{
public static void main(String args[])
{
... more lines ...
}
}

The file containing our code is called the source code file.

Definition: Source Code

A Java source code file is a text file that contains programming code
written according to the Java language specifications, resembling a
mixture of mathematical language and English. A computer cannot
execute source code, but humans can read and understand it.


Java source code files should be saved as Name.java, where Name is the
name that appears in the first line of the program: public class Name.
That Name is referred to as the name of the class, or program. By
convention its first letter is capitalized.

|[pic] | |[pic] |
| |[pic] | |


Figure: Saving a Java source code file

Here is an example of a Java source code file. We will later explain what
the various lines mean; for now it is simply a text file that looks as
shown.


Example: The first source code file
Create a source code file containing the necessary Java code to get the
computer to write "Hi - this is my first program" on the screen.

Our first Java program looks as follows:

public class Test
{
public static void main(String args[])
{
System.out.println("Hi - this is my first program");
}
}

This program, or class, is called Test and must be saved under the file
name Test.java.
?

Compiling a Java Program or Class


A source code file, which is more or less readable in plain English, needs
to be transformed into another format before the computer can act upon it.
That translation process is called compiling and is accomplished using the
Java compiler javac from the Java Developer's Kit (JDK), which could be
invoked by an IDE such as BlueJ.

Definition 1.03: Compiling

Compiling is the process of transforming the source code file into a
format that the computer can understand and process. The resulting
file is called the byte-code, or class, file. The name of the class
file is the same as the name of the program plus the extension .class.
The program javac from the Java Developer's Kit is used to transform a
source code file into a class file.

|[pic] | |[pic] | |[pic] |
| |[pic] | |[pic] | |


Figure: Compiling and creating class file


If a source code contains any errors, they are flagged by the compiler. You
need to fix them and re-compile until there are no further errors.

?

Tip: In case of an error, the javac compiler shows the line number and
position of where it thinks the error occurred in your source code.


. If the compiler points out an error, then there is an error at or
before the indicated position.
. If the compiler reports a certain number of errors, than this is the
least amount of errors.
. If one error is fixed, other errors may automatically disappear or new
ones may appear.


Fix your source code a few errors at a time. Recompile often to see if
the number of errors and the error messages change until no errors are
reported. If you can not find an error at the position indicated by the
compiler, look at the code before that position.




Executing a Java Program or Class


The Java compiler does not produce an executable file, so Java programs can
not execute under the operating system of your machine. Instead they
execute inside a Java Virtual Machine, which is invoked using the java
program of the JDK.

Definition 1.04: Executing a Class File

To execute a Java program the Java Developer's Kit provides a program
called java. When executing that program with your class file as
parameter the following happens:


. the Java Virtual Machine (JVM) is created inside your computer
. the JVM locates and reads your class files
. the JVM inspects your class file for any security violations
. the JVM executes, or interprets, your class file according to its
instructions if possible


Under Windows and Unix, execute a program by typing at the command
prompt java Name, where Name is the name of the program (no
extension). On a Macintosh, double-click the java icon and select the
appropriate class file.

Most IDE's allow for a convenient way to execute a file. In BlueJ you right-
click on a compiled class and select the "main" method.

|[pic] | |[pic] | |[pic] | |[pic] |
| |[pic] | |[pic]| |[pic]| |


Figure: Executing a class file

A good question at this point is which line in a Java program executes
first.

Definition 1.05: Default Program Entry Point

The default program entry point is that part of a class (or program)
where execution begins. For every Java class (or program), the
standard program entry point consists of the line:


public static void main(String args[])


If that line is not present in your source code, the JVM can not
execute your program and displays an error message.

At this point, we need to explain what the Java Virtual Machine is and how
it relates to the operating system and to Java class files.

Definition 1.06: Java Virtual Machine (JVM)

The Java Virtual Machine (JVM) is a platform-independent engine used
to run Java applets and applications. The JVM knows nothing of the
Java programming language, but it does understand the particular file
format of the platform and implementation independent class file
produced by a Java compiler. Therefore, class files produced by a Java
compiler on one system can execute without change on any system that
can invoke a Java Virtual Machine.[3]


When invoked with a particular class file, the JVM loads the file,
goes through a verification process to ensure system security, and
executes the instructions in that class file.

The JVM, in other words, forms a layer between the operating system and the
Java program that is trying to execute. That explains how one Java program
can run without change on a variety of systems: it can not! A Java program
runs on only one system, namely the Java Virtual Machine. That virtual
system, in turn, runs on a variety of operating systems and is programmed
quite differently for various systems. To the Java programmer, it provides
a unified interface to the actual system calls of the operating system.[4]

You can include graphics, graphical user interface elements, multimedia,
and networking operations in a Java program and the JVM will negotiate the
necessary details between the class file(s) and the underlying operating
system. The JVM produces exactly the same results - in theory - regardless
of the underlying operating system. In the Basic (or C, or C++) programming
language, for example, you can create code that specifies to multiply two
integers 1000 and 2000 and store the result as another integer. That code
works fine on some systems, but can produce negative numbers on others.[5]