Introduction To Java
History
Java is an object-oriented language based on earlier languages, mainly
C++ and Smalltalk. It was originally developed as a language for
embedded programming; however with the huge growth that the Internet
experienced in the 90's Java quickly became embraced as the preferred
development tool for the Internet, first with the use of Applets,
programs embedded in Web browsers, then later with server-side
programming using Servlets, JSP, EJB, JDBC, and JMS. Today Java is one
of the most common development environments used in business.
Technology Overview
These days Java is really a pretty complex technology. Sun has produced
three main flavours of Java: J2SE, J2EE, and J2ME. J2SE (standard
edition) is the standard Java language with a set of common libraries,
and it's what we will focus on in this class. J2EE (enterprise edition)
is an extension of Java for enterprise development. J2EE has a lot to
offer, but the main components are Servlets, JSP, EJB, and
JMS. Finally, J2ME (mobile edition) is a version of Java tailored for
embedded environments; it's especially used in the mobile phone market.
Java Compiler and Virtual Machine
There are two basic ways of running computer programs: 1) Compiled
Programs, and 2) Interpreted Programs. Traditionally, compiled programs
have been used in situations where resources like CPU and memory are
scarce. Such programs have to be recompiled every time you change the
source code. Once the code is compiled, the end product is native
machine code. Interpreted programs on the other hand rely on an
interpreter. An interpreter is a program that executes the source code
directly, one line at a time. Therefore, you can modify the source
code, and run it immediately without having to compile it. Also, if you
have an interpreter for your language on Windows, and also on Unix, you
can run your program on either machine without having to recompile it
(this is called Platform Independence).
Finally, interpreted programs are more dynamic in nature than compiled
programs. I will discuss this later in the course when and if it comes
up. So is Java a compiled language or an interpreted language?
Actually, Java exists in a kind of limbo in between the two. Java
source code is compiled into instructions which are called bytecode.
There is an
interprer which then executes the bytecode (as opposed to the source
code!). This gives Java some of the advantages of both paradigms. The
Java bytecode interpreter is called the Java Virtual Machine or the
Java Runtime. To make
things even more murky, there is a technology in Java today called JIT,
or Just In Time compilation. This technology enables Java programs to
be executed more quickly by compiling parts of the bytecode into native
code as the program is running. This is an advaned topic, and all you
really need to know about it at a high level is that it improves the
performance of Java programs significantly.
Hello World: How to Make and Run a Simple Java Program
First, you need to install the Java JDK. The JDK includes both the
compiler and the Java runtime (aka the Virtual Machine), as well as
some other tools. Next, you need to type your program into a text
editor. The following program needs to be written to a file called
HelloWorld.java:
public class HelloWorld {
public static
void main(String[] args) {
System.out.println("Hello, world!");
}
}
Then you need to compile the program at the command line:
C:>javac HelloWorld.java
This program is compiled into a HelloWorld.class file. This file
contains the bytecode needed by the runtime to execute the program. To
run your program, you invoke the virtual machine as follows:
C:>java HelloWorld
Java Buzzwords:
- Simple
- Object-Oriented
- Distributed
- Robust
- Secure
- Platform-Independent/Portable
- High Performance
- Multi-Threaded
- Dynamic
- Garbage Collection