Introduction to Java Programming and Architecture

Learning Objectives

  • Describe the primary features of Java technology, including the Java Virtual Machine (JVM), garbage collection, and code security.

  • Describe the three sequential phases of a Java program: write, compile, and run.

History and Background of Java

  • Creation Details:

    • Year Created: 1991

    • Creators: James Gosling and colleagues at Sun Microsystems.

  • Naming History:

    • Original Name: Oak, named after an oak tree standing outside James Gosling's window.

    • Reason for Name Change: A programming language named Oak already existed, prompting the change to Java.

  • Original Motivations:

    • Designed out of the need for a platform-independent language capable of being embedded in consumer electronics (such as toasters and refrigerators).

    • One of the earliest Java-based projects was Star 7, a personal hand-held remote control.

  • Transition to the Web:

    • The growth and popularity of the World Wide Web occurred around the same time.

    • Gosling's team recognized that Java's design for embedded electronics made it uniquely suited for Internet programming.

  • Summary Progression: Oak \rightarrow Java; Consumer electronics focus \rightarrow led to platform independence \rightarrow made Java ideal for the Web.

Conceptual Framework of Java Technology

Java technology encompasses four simultaneous definitions:

  • A Programming Language:

    • Allows developers to build all categories of applications, functioning like conventional programming languages.

  • A Development Environment:

    • Provides a full suite of software development tools, including:

      • Java Compiler

      • Java Interpreter

      • Documentation generator

      • Class file packaging tool

  • An Application Environment:

    • Java applications function as general-purpose programs capable of running on any system that has a Java Runtime Environment (JRE) installed.

  • A Deployment Environment:

    • Includes two main deployment pathways:

      1. The JRE bundled with the Java 2 SDK, which contains all standard class files.

      2. Web browsers, most of which contain an embedded Java interpreter and runtime.

Key Features of Java Architecture

Java Virtual Machine (JVM)

  • Definition: An imaginary machine implemented via emulating software running on a physical machine.

  • Specification: Provides the hardware platform specification to which all Java source code is compiled.

  • Bytecode:

    • A specialized machine language understood directly by the JVM.

    • Hardware-independent: Any computer equipped with a Java interpreter can execute compiled Java code, regardless of which machine originally compiled it.

Garbage Collection

  • Mechanism: A dedicated garbage collection thread automatically frees memory that is no longer needed throughout the lifespan of the program.

  • Developer Benefit: Eliminates the requirement for programmers to manually deallocate memory, unlike unmanaged languages such as C or C++.

Code Security

Code security is managed directly by the Java Runtime Environment (JRE) using a three-step pipeline:

  1. Class Loading (Class Loader):

    • Loads all required classes needed to run the program.

    • Maintains a strict separation between local file-system classes and network-imported classes.

    • Determines the memory layout of the executable at runtime, protecting against unauthorized access to restricted code regions.

  2. Code Verification (Bytecode Verifier):

    • Tests the underlying format of code fragments.

    • Inspects code for illegal instructions that could violate access rights to objects.

  3. Code Execution:

    • The JRE executes the validated bytecode compiled for the JVM.

  • Memory Aid for Security Pipeline: Load \rightarrow Verify \rightarrow Execute (handled sequentially by Class Loader \rightarrow Bytecode Verifier \rightarrow JRE execution).

Lifecycle and Phases of a Java Program

The lifecycle of a Java program consists of three distinct tasks, utilizing three separate tools, producing specific file outputs:

  • Phase 1: Write the Program

    • Tool Used: Any standard text editor.

    • Output File Extension: .java file (e.g., Hello.java).

  • Phase 2: Compile the Program

    • Tool Used: Java Compiler.

    • Output File Extension: .class file containing Java bytecode (e.g., Hello.class).

  • Phase 3: Run the Program

    • Tool Used: Java Interpreter.

    • Output: Program execution output.

Step-by-Step Flow of Files

Text Editor \rightarrow Hello.java \rightarrow Java Compiler \rightarrow Hello.class \rightarrow Java Interpreter \rightarrow Output

  • Execution Frequency Rules:

    • Compiling (Editor \rightarrow Compiler): Performed once per code revision.

    • Running (Compiler Output \rightarrow Interpreter): Performed every time the program is executed.

Self-Check Review Questions

  • Question 1: Who created Java, and in what year?

    • Answer: Created in 1991 by James Gosling and colleagues at Sun Microsystems.

  • Question 2: What was Java originally called, and why was the name changed?

    • Answer: Originally called Oak after a tree outside Gosling's window; changed to Java because another programming language named Oak already existed.

  • Question 3: What consumer-tech problem motivated Java's design?

    • Answer: The need for a platform-independent language to embed software into consumer electronic devices like toasters and refrigerators.

  • Question 4: Name the four things "Java technology" refers to.

    • Answer: A programming language, a development environment, an application environment, and a deployment environment.

  • Question 5: What is bytecode, and why does it make Java platform-independent?

    • Answer: Bytecode is a hardware-independent machine language understood by the JVM. Because any computer with a Java interpreter can execute it, the code can run anywhere regardless of where it was compiled.

  • Question 6: What does the garbage collection thread do, and why does it matter to programmers?

    • Answer: It automatically frees unused memory throughout execution, relieving programmers from manual memory management (deallocation) required in languages like C/C++.

  • Question 7: What are the three steps the JRE performs for code security, and which component handles each?

    • Answer: Class loading handled by the Class Loader, Code verification handled by the Bytecode Verifier, and Code execution handled by the JRE.

  • Question 8: What are the three phases of running a Java program, and what file extension is produced after each of the first two phases?

    • Answer: Write (produces .java), Compile (produces .class), and Run (produces program execution output).