SE 109 - Lecture 3: Introduction to Java

Outline of Lecture 3

Introduction to Java

Definition: Java is a class-based, general-purpose, object-oriented programming language designed to have minimal implementation dependencies.

Core Principle: WORA (Write Once, Run Anywhere) - Compiled Java code (bytecode) can run on any platform with a compatible Java Virtual Machine (JVM) without recompilation.

It is widely used and particularly designed for internet applications.

(Slides 4, 5, 6, 7 showed Java's prevalence: standard for development, mobile apps, enterprise software, cloud platform, 9+ million developers, 3 billion phones).

History of Java

(Slide 8 showed a picture of James Gosling. Slide 9 discussed the initial concept and shift to web focus).

Why Java was created?

Timeline Highlights:

YearDevelopment
1990Sun Microsystems team (Gosling) developed software for electronic devices.
1991New language "Oak" introduced, using C++.
1993WWW emerged, transforming the internet.
1994Sun developed "HotJava" web browser to run applet programs.
1995Oak was renamed to Java.
1996Java established as an Object-Oriented Programming Language (OOPL).
1996JDK 1.0 released.
1997JDK 1.1 released.
1998JDK 1.2 (Beginning of J2SE, J2EE, J2ME naming).
...Subsequent versions released (JDK 1.4, 5, 6, 7, 8, 9...).
2010Oracle acquires Sun Microsystems.

(Slide 12 showed a detailed version history with specific release years for JDK versions, indicating Sun Microsystems origin and later Oracle acquisition).

Java Editions

Java technology comes in different editions tailored for specific types of development:

How Java is Different from C++

(Slide 19 showed a comparison diagram: `MyProg.java` -> `javac` -> `MyProg.class` (Bytecode) -> JVM -> OS, versus `MyProg.cpp` -> `gcc` -> `myprog.exe` (Executable) -> OS).

New Features Added in Java (Compared to C/C++)

Java Applications

Java is used in a wide variety of applications:

Characteristics of Java (Summary)

  1. Simple: Familiar C/C++ syntax, automatic memory management (garbage collection).
  2. Object-Oriented: Everything (except primitives) is an object; programs structured around classes.
  3. Distributed: Designed for network environments (supports TCP/IP, RMI, etc.).
  4. Robust: Strong compile-time error checking, strict runtime checking, exception handling, garbage collection reduce errors.
  5. Secure: Security features (Security Manager, bytecode verification, sandboxing for applets) prevent destructive actions.
  6. Platform Independent (Architecture Neutral): Achieved through bytecode and JVM. WORA.
  7. Portable: Platform independence plus standard library definitions ensure code runs similarly everywhere.
  8. Interpreted: JVM interprets bytecode at runtime (though JIT compilation makes it fast).
  9. High Performance: JIT compilers translate bytecode into native machine code for near-native speed.
  10. Multithreaded: Built-in support for concurrent programming.
  11. Dynamic: Classes loaded on demand; supports reflection.

Compiled and Interpreted Nature

Java uses a two-stage process:

  1. Compilation: The Java compiler (javac) translates human-readable source code (`.java` file) into platform-independent Bytecode (`.class` file). The compiler performs syntax checking.
  2. Interpretation/Execution: The Java Virtual Machine (JVM) on the target computer reads the Bytecode.
    • It may interpret the bytecode line by line, translating it into native machine instructions.
    • More commonly, it uses a Just-In-Time (JIT) compiler to translate frequently executed sections of bytecode into native machine code *during* runtime for much better performance.

The Bytecode itself is platform-independent, but the JVM is platform-specific (there are different JVMs for Windows, Linux, macOS, etc.). This is how Java achieves "Write Once, Run Anywhere".

(Slide 28 showed a diagram: Java Code -> JAVAC Compiler -> Byte Code -> JVM (Windows/Linux/Mac) -> Execution).

Note: The Java compiler is typically written in Java, while the JVM (including the interpreter/JIT) is often written in C/C++ for performance.

Java Environment

The Java development and runtime environment consists of several key components:

(Slides 40, 41, 42, 43 showed diagrams illustrating the relationship between JDK, JRE, JVM, and how Java programs run on different OS via their respective JRE/JVM).

JVM Components

Java Program Types

Fundamental Language Elements

The Simple Structure of a Method

Methods define the behavior of objects. They have:

The main Method

Compiling and Running a Simple Program

File: Hello.java

// A program to print "hello, world".
class Hello {
    // main method - program execution begins here
    public static void main(String[] args) {
        // Print the text "hello, world" to the console
        System.out.println("hello, world");
    }
}

The Edit-Compile-Run Cycle:

  1. Edit: Create/save the source code in a file named `Hello.java` (the filename must match the public class name).
  2. Compile: Use the Java compiler:
    javac Hello.java
    This generates the bytecode file `Hello.class`. If there are errors, edit the code and recompile.
  3. Run: Use the Java Virtual Machine (interpreter):
    java Hello
    (Note: Do not include the `.class` extension when running). The JVM loads `Hello.class`, finds the `main` method, and executes it.

Bytecode Files (`.class`)

Simple Java Application Example Walkthrough

Let's re-examine the example from the slides:

File: TestGreeting.java

// code1 The TestGreeting.java Application
// Sample “Hello World” Application
public class TestGreeting { // Class name matches filename

    public static void main (String [] args) { // Main method entry point

        // 1. Create an object (instance) of the Greeting class
        // 'new Greeting()' calls the constructor and allocates memory
        // 'hello' is a variable holding the reference (address) to the object
        Greeting hello = new Greeting();

        // 2. Send a message (call the greet() method) to the object
        // referred to by the 'hello' variable
        hello.greet();

    } // end main
} // end class TestGreeting

File: Greeting.java

// Code 2-2 The Greeting.java class
public class Greeting { // Another class definition

    // Method definition for the greet behavior
    // 'public' means it can be called from other classes (like TestGreeting)
    // 'void' means it doesn't return any value
    public void greet() { // Between the parentheses, don’t write void

        // Use the System class's 'out' object (standard output stream)
        // and call its 'println' method to print text to the console
        System.out.println("Hello ALL");

    } // end greet method
} // end class Greeting

Explanation Notes:

Working with Java Tools (Command Line)