Site hosted by Build your free website today!

Session 1 Lecture Notes for First Course in Java

Session Date Topic Homework due on the next session
1 17 June Ch. 1: Java Fundamentals
Download SDK, run HelloWorld, and write a program that does arithmetic

Introduction to class and explanation of the schedule and syllabus

Five Major Releases of Java

“Hello World” Java code and demonstration

Demonstration of the Java Glossary applet

What is Java Programming?

Applications, Applets and Servlets

Java compared to C and C++

Java and C#

Compiling and running

Platform Architecture

Syntax and reserved words




Homework Assignment

Introduction to class and explanation of the schedule and syllabus


bullet “Hello World” Java application
bullet Hello World applet (see Session 9)
bullet Java Glossary applet

The promise of Java:

Five Major Releases of Java

bullet 1.0    1995 web-based release; lowest common denominator for Web browsers; total classes = 212
bullet 1.1    1997 major revision introduces JavaBeans (reusable components); total classes = 504
bullet Java 2: [marketing name that did not affect the version names]
bullet 1.2 with SDK 1.2    1998 major release introduces Swing; total classes = 1520
bullet 1.3 (textbook)          2000 release improves performance with the HotSpot virtual machine (as opposed to the "classic" VM) 
bullet Optimizes arrays and loops, memory management, and multi-threaded execution
bullet Smaller footprint for memory: "The first header word contains information such as the identity hash code and garbage collector status information. The second is a reference to the object's class. Only arrays have a third header field, for the array size."
bullet Note: Mac OS X, which is UNIX-based, supports 1.3
bullet 1.4 (most current)    2002 release improves network and file I/O, supports 64-bit memory (larger datasets); more comprehensive debugging, security, and support for XML, JDBC, and CORBA

Free download of the current SDK, Software Development Kit at

Types of languages
Object-oriented programming Procedural Web scripting Page description Data description
Java, C++, SmallTalk, Objective-C, C# C, Basic, Fortran, Cobol, Lisp, Perl JavaScript, VBScript HTML XML

Note: The runtime environment is for users who only need to run existing Java bytecode and do not write the code.  The Java Runtime Environment consists of the Java virtual machine, the Java platform core classes, and supporting files. It is the runtime part of the Java 2 SDK, but without the development tools such as compilers and debuggers. (The JRE download is only 8 MB, compared to 38 MB for the SKD download.) The installer for certain programs might install the runtime environment so that end users can run the application.

Hello World Code (preview)

public class HelloWorld 
  public static void main (String[] args)
    System.out.println("Hello World");

Explanation: public means the class is visible outside the class. Class declares that HelloWorld is a class.
Every application must have an entry point for execution, and this entry point must be a method called main.
This method supports command-line arguments, which are implemented as an array of strings.
To print a string to the console on a new line, enclose the string in quotation marks, and pass the string to the println method.
The core package of the Java language, java.lang, provides a class called System.
System enables you to write to output with an output stream:

Let's see the array of command-line strings in action:

Command-line args

The output is:

args[0]: sky
args[1]: is
args[2]: blue
args[3]: 4
args[4]: you

What is Java Programming?

Java programming is the process of creating applications, applets, servlets, and can include creating Java Server Pages (JSP), and Enterprise Java Beans (EJB). 

Sun Microsystems likes to think of the Java platform as a target for software development, much as traditionally software developers targeted an operating-system or processor-based platform such as MacOS/Motorola, Windows/Intel, or Sun/Sparc.

To develop code for the Java platform, the software developer uses: 

bullet the Java programming language (syntax and semantics)
bullet the Java API (core classes and interfaces) 
bullet a Java compiler, such as the compiler in Sun's Java 2 Platform, Standard Edition (J2SE) software development kit (SDK): javac
bullet a Java debugger (optional)
bullet for servlets or JSPs, a servlet-enabled web server, such as TomCat or JRun
bullet for EJB, an application server, such as BEA WebLogic or IBM WebSphere
bullet a text editor for source code
bullet standalone (Notepad, Word (save as text), TextPad) - recommended
bullet an integrated debugging environment (IDE) - optional but additional learning curve for this First Course. Although there are visual programming environments for Java, this course focuses on text-based programming.

Some people say JDK (Java Development Kit), and this is the same as the Java SDK (Software Development Kit).
However, the JRE (Java Runtime Environment) is a subset of the SDK for running Java programs that does NOT include development tools.

To run your code, you and whoever uses your code will need:

bullet a Java runtime environment (included in the SDK) with a Java virtual machine that interprets the compiled bytecode (the java interpreter: java)
bullet For applets with Java 1 features: web browsers generally have a built-in runtime with Java 1 support.
bullet For applets with Java 2 features: the Java Plug-in software that enables web browsers to use the Java 2 Runtime Environment.

Applications, applets, and servlets

An application is software that runs on top of the operating system, has a specific purpose, and usually allows the user to interact. An example of an application is Microsoft Word. On the other hand, the software that runs your printer (a "driver") is something for the operating system or an application, rather than for the human user.

Applets run in a browser

Java introduced the concept of applets, which are applications that can run inside of a web browser on the client computer. A web browser is an application. 

An applet is a "safe" application that can only run inside of another, very specific application. Some of the key restrictions on an applet 

bullet confine the applet inside a "sand box" within the client's browser
bullet prevent the applet from accessing the client's file system or running native code
bullet give web surfers confidence  to download applets from the web, even if the user does not know the company that created the applet

Note: If an applet provider registers a specific applet with a registration company, the "signed applet", if you accept it, can have access to your file system and the native operating system.

Note: A Java application, like any application, does have access to your file system.

Java Plug-in
The Java Plug-in allows you to run applets using Sun's Java 2 Runtime Environment, Standard Edition (JRE) instead of the web browser's default virtual machine. Sun's JRE provides a Java CompatibleTM environment for today's widely adopted web browsers. That means consistency and reliability when running applets.

[Swing is part of the Java 2 Foundation Class library that extends the Java 1 Advanced Windowing Toolkit (AWT) to provide new GUI components and features, better event handling, and selectable look and feel]

Servlets run on the server
JavaTM Servlet technology provides web developers with a simple, consistent mechanism for extending the functionality of a web server and for accessing existing business systems to give them a web interface. A servlet can almost be thought of as an applet that runs on the server side -- without a face. Java servlets have made many web applications possible.

bullet dynamically generated HTML
bullet interfacing with legacy systems or native server applications
bullet advantages compared to the alternatives:
bullet server-side scripting, which has limited capabilities and limits you to one web server: IIS or NES
bullet Common Gateway Interface (CGI), which requires a process for each request and is not easily ported to other platforms

Java compared to C, C++

A computer platform is the combination of the operating system and the central processing unit (CPU). 

Examples of operating systems include 

bullet Mac OS
bullet UNIX (Solaris, HP/UX, BSD)
bullet Linux
bullet Windows 

Examples of CPUs include 

bullet Intel Pentium (x86 family)
bullet PowerPC (Apple/IBM/Motorola)
bullet UltraSparc (Sun)
bullet Alpha (Compaq)

A standard, compiled application runs machine code that is specific for a platform. Machine code is the raw zeros and ones that a computer "chip" understands. If you want your UNIX application to run on Windows, for example, you must "port" the code and recompile it using a compiler that produces machine code that is "native" to the target CPU.

Platform independence

Java code is different from standard, compiled code. You compile your Java source code into a special language, "bytecode", that a special application, the Java virtual machine (JVM), interprets. 

The Java interpreter (java) is different from the "HotSpot" just-in-time (JIT) compiler. The interpreter interprets bytecode and makes application programming interface (API) calls into the native operating system, for example, calls to Win32API. The JIT compiler can improve performance with optimization capabilities that allow it to compile into native machine code parts that are most heavily used. 

The JVM is an application that emulates a computer. It translates Java bytecode into native machine code.

There is a JVM for Mac OS, UNIX, Linux, and Windows. This means that the same source code should be valid for multiple platforms. You do not need to "port" the application to another platform. It should be "write once, run anywhere", assuming there is a JVM for that platform.

There are even JVMs for smaller platforms, such as the PalmOS that runs on the PalmPilot.

Advantages and disadvantages of the Java platform compared to C++

Pro Con
Harder to create careless bugs (memory leaks, pointer errors) performance is slow because of CPU-intensive interpretation or compilation of byte code into machine code
Relatively platform independent: "write once, run anywhere", which is particularly valuable on the server vision is not fully realized
write once, debug everywhere
strict object-oriented model; cannot mix with procedural code  can use more CPU and memory because C++ can be optimized with lower-level code
Dynamic linking: supports introducing new objects at runtime without recompiling everything [RMI (remote method invocation)] slower because of look up; mitigate with JIT compilation

For more on static versus dynamic linking:

Java and Microsoft C#

From Sun's point of view, Java 1.4.1 is part of Sun's J2EE platform. C# is a Microsoft programming language that closely resembles Java. C# is for use in Microsoft's .NET platform, which relies on libraries of the Microsoft Windows operating system. Insofar as .NET has an intermediate language (IL) and a common language runtime, there is a something analogous to a JVM-like portability, but only within the Microsoft family of programming languages, such as Visual Basic, and only for use on Microsoft operating systems. For details, see

The virtues of Java (as in theory of C#): :

bullet Simple (memory management and garbage collection
bullet Portable (write once, run on any platform with a Java Virtual Machine)
bullet Object-Oriented (unlike C++, enforces the object model)
bullet Robust (built-in protection from mismanaging memory and built-in system for managing errors)
bullet Multithreaded (supports multitasking, such as accessing data while rendering graphs)
bullet Architecture-neutral (in theory, upgrading operating systems and microchips will not break executables)
bullet Interpreted (supports portability)
bullet High performance ("just-in-time" compiler for direct translation into native machine code)
bullet Network-Savvy/Distributed (URL access to resources; remote method invocation)
bullet Dynamic (interfaces and freedom from pointers allow modules to hook up at runtime)

Compiling and Running

Like other programming languages, you compile your Java source code (in a text file) by invoking a compiler that outputs compiled code.
In this case, the compiled code is called "bytecode", which is in a .class file. You can install the same .class file on multiple operating systems.

To enable platform independence, the Java platform inserts "interpretation" between the compiled code (.class file of bytecode) and the operating system's native machine code.
The Java interpreter, named java, converts compiled "bytecode" into machine code.

The Java interpreter executes bytecode within the Java virtual machine, a sort of virtual computer inside the computer.

Or, if you prefer to visualize the sequence as numbered steps:

1 Write and save the source code file, which is a text file named after the class, such as .java  
2 Invoke the Java compiler, javac, and specify the .java file to compile:
3 The compiler which outputs a .class file of byte code, such as MyClass.class .class  
4 Invoke the Java interpreter, java, and specify the .class file to interpret:
java MyClass
5 Java Virtual Machine interprets the bytecode and outputs native code for the platform (Windows, UNIX, or Macintosh)    
6 Operating system runs the native code    

"Platform" Architecture

Sun defines three (3) Java platforms at

bullet Standard
bullet Enterprise (with Enterprise Java Beans)
bullet Micro (smart cards, pagers, set-top boxes)
The JavaTM 2 Platform, Standard Edition (J2SETM) has revolutionized computing with the introduction of a stable, secure and feature-complete development and deployment environment designed from the ground up for the Web. It provides cross-platform compatibility, safe network delivery, and smartcard to supercomputer scalability. It provides software developers with a platform for rapid application development, making it possible to deliver products to market in Internet time. It defies traditional software development and deployment models by delivering on the promise of cross-platform compatibility.

What are core APIs?

Core APIs are class and interfaces that are an integral part of (hence "core") the Java platform.
The Software Development Kit (SDK) is much more than a language. There are many convenient implementations you can use and customize in your applications.
The code provided for your applications is the application programming interface.
(You can think of APIs as "hooks" you can grab and use, without having to know the deepest level of Java, which is actually C++.)
The core APIs save you the work of "reinventing the wheel" and allow you to program at a higher level.
For example,

bullet lang package provides the classes and interfaces that support basic features of the language
bullet For example, why not re-use the pre-built functionality the Sun provides for getting the square root of a number?
bullet Another example, many applets are likely to have a check box:
bullet awt package provides the classes and interfaces that support basic features of the advanced windowing toolkit, that is, widgits for a graphical user interface (GUI)
bullet swing  package provides the classes and interfaces that support basic features of an even better graphical user interface (GUI)
bullet If you want to work with sound tracks, there are APIs to help you, such as Track class:


In the current J2SE, the JRE and the SDK include new core application programming interfaces (APIs), such as XML and Logging:


Syntax and reserved words

When you use any language, whether it is a computer language or a human language, you have to follow the rules. The term "syntax" refers to the rules of word order and word combination. Some languages, like French, have a flexible syntax that lets you say "the chocolate good" as well as "the good chocolate". Other language are more rigid about word order. Java syntax is more rigid than, say, Perl syntax. 

For example, Java has certain "reserved words" that you use according to Java's syntax and semantics. The Hello World application consists almost exclusively of reserved words or predefined words (in bold):

public class HelloWorld 
  public static void main (String[] args)
    System.out.println("Hello World");

The only words that are not reserved words or predefined words are the three variables: 

bullet the class name HelloWorld 
bullet the string that the JVM prints to the screen, "Hello World"
bullet the variable for command line arguments, args

Just as human languages have punctuation in addition to words, so Java has special symbols, such as the curly braces { }, parentheses ( ), the brackets [], the comma separator , the wildcard asterisk *, quotation marks " ", the forward slash /, the backslash \, the dot . and the semicolon ; that completes a statement.


The PATH system environment variable should point to the directory that has the java interpreter (java.exe), which is typically the "bin" subdirectory of the directory for JDK version.

For example, I went to my Windows Control Panel > System > Advanced > Environment Variables

and edited the path by adding a new entry:



I also created a new user variabled, CLASSPATH, and included:

bullet the current directory, which is dot: (.)
bullet the home directory of my SDK
bullet the exact location of the tools.jar file


Then I logged out (or rebooted), so the changes would take effect.


bullet Sun’s web site:
bullet Java API documentation:
bullet Documentation & Training:
bullet How to download Java:
bullet How to use Sun’s Java tutorial:

Quiz: Session 1

  1. What is the purpose of the JRE?

  2. What is the Java Plug-in?

  3. What is the difference between an application and an applet?

  4. What is the difference between an applet and a servlet?

  5. What does the JVM do?

  6. What are some differences between Java and C++?

  7. What is a key difference between Java and C#?

  8. What is javac?

  9. What is “bytecode” and why is it useful?

  10. What is a “core API”?

Homework Assignment for 24 June 

For a complete, step-by-step, illustrated guide, see Sun's tutorial, "Your First Cup of Java":

Be sure to read the installation instructions, including the section about updating the PATH variable:

  1. Download the Java SDK.
  2. Optional: Set the path environment variable by giving the class path.
    For additional instructions and examples, see:
  3. Write, compile, and run the HelloWorld application.
  4. Email your homework to the instructor:
  5. Go to and look at Sun's Java Tutorial "Getting Started"
    about Getting Started with the HelloWorld application and applet.

If you have problems, check this troubleshooting guide: