Introduction History:: Java is a programming language created by James Gosling from Sun Microsystems in 1991. The first public available version of Java (Java 1.0) was released 1995. Over time several version of Java were released which enhanced the language and its libraries. The current version of Java is Java 1.6 also known as Java 6.0. From the Java programming language the Java platform evolved. The Java platform allows that code is written in other languages then the Java programming language and still runs on the Java virtual machine. Overview:: The Java programming language consists of a Java compiler, the Java virtual machine, and the Java class libraries. The Java virtual machine (JVM) is a software implementation of a computer that executes programs like a real machine. The Java compiler translates Java coding into so-called byte-code. The Java virtual machine interprets this byte-code and runs the program. The Java virtual machine is written specifically for a specific operating system. The Java runtime environment (JRE) consists of the JVM and the Java class libraries. Characteristics of Java:: The target of Java is to write a program once and then run this program on multiple operating systems. Java has the following properties: Platform independent: Java programs use the Java virtual machine as abstraction and do not access the operating system directly. This makes Java programs highly portable. A Java program which is standard complaint and follows certain rules can run unmodified all several platforms, e.g. Windows or Linux.
Object-orientated programming language: Except the primitive data types, all elements in Java are objects.
Strongly-typed programming language: Java is strongly-typed, e.g. the types of the used variables must be pre-defined and conversion to other objects is relatively strict, e.g. must be done in most cases by the programmer.
Interpreted and compiled language: Java source code is transfered into byte-code which does not depend on the target platform. This byte-code will be interpreted by the Java Virtual machine (JVM). The JVM contains a so called Hotspot-Compiler which translates critical byte-code into native code.
Automatic memory management: Java manages the memory allocation and de-allocation for creating new objects. The program does not have direct access to the memory. The so-called garbage collector deletes automatically object to which no active pointer exists.
The Java syntax is similar to C++. Java is case sensitive, e.g. the variables myValue and myvalue will be treated as different variables.
Development with Java:: The programmer writes Java source code in an text editor which supports plain text. Normally the programmer uses an IDE (integrated development environment) for programming. An IDE support the programmer in the task of writing code, e.g. it provides auto-formatting of the source code, highlighting of the important keywords, etc. At some point the programmer (or the IDE) calls the Java compiler (javac). The Java compiler creates platform independent code which is called bytecode. This byte-code is stored in ".class" files. Bytecode can be executed by the Java runtime environment. The Java runtime environment (JRE) is a program which knows how to run the bytecode on the operating system. The JRE translates the bytecode into native code and executes it, e.g. the native code for Linux is different then the native code for Windows. By default, the compiler puts each class file in the same directory as its source file. You can specify a separate destination directory with -d Classpath The classpath is the connection between the Java compiler and Java interpreter. It defines where the compiler and interpreter look for .class files to load. The classpath in Java defines which Java class are available for your Java program. For example if you want to use an external Java library you have to add this library to your classpath to use it in your your program.
Your first Java program 1. Write source code The following Java program is developed under Microsoft Windows. The process on other operating system should be similar but will not be covered here. Select a directory which should contain your code. I will use the directory c:\temp\java which will be called javadir.
Open a text editor which supports plain text, e.g. notepad under Windows and write the following source code. You can start notepad via Start->Run-> notepad and pressing enter.
// The smallest Java program possible
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World"); } } Save the source code in your directory "javadir" under the name "HelloWorld.java".The name of a Java source file must always equals the class name (within the source code) and end with .java. In our case the filename must be HelloWorld.java because the class is called HelloWorld. 2. Compile and run your Java program Switch to the command line, e.g. under Windows Start-> Run -> cmd. Switch to the "javadir" directory with the command cd javadir, for example in my case cd c:\temp\java. Use the command dir to see that the source file is in the directory. javac HelloWorld.java
Check the content of the directory with the command "dir". The directory contains now a file "HelloWorld.class". If you see this file you have successfully compiled your first Java source code into byte-code. Run -> cmd. Switch to the directory jardir. To run your program type in the command line:
java HelloWorld The system should write "Hello World" on the command line. 3. Using the classpath You can use the classpath to run the program from another place in your directory. Switch to the command line, e.g. under Windows Start-> Run -> cmd. Switch to any directory you want. Type: java HelloWorld If you are not in the directory in which the compiled class is stored then the system should result an error message Exception in thread "main" java.lang.NoClassDefFoundError: test/TestClass To use the class type the following command. Replace "mydirectory" with the directory which contains the test directory. You should again see the "HelloWorld" output. java -classpath "mydirectory" HelloWorld