Java Unit 1: The History, Evolution, and Structural Principles of Java
The History and Evolution of Java
- Understanding Java's Context: To fully comprehend Java, one must look at the reasons for its creation, the forces that shaped it, and the legacy it inherits. Java is described as a blend of its heritage's best elements combined with innovative concepts required for its unique mission.
- Foundational Reasons for Language Innovation: Computer language development generally occurs for two primary reasons:
- To adapt to changing environments and uses.
- To implement refinements and improvements in the art of programming.
- Java's Lineage and Heritage:
- Java is related to , which is a direct descendant of .
- Syntax: From , Java derives its syntax.
- Object-Oriented Features: Many of Java's object-oriented features were influenced by .
- Character: Several defining characteristics of Java come from or are responses to its predecessors, rooted in a decades-long process of refinement and adaptation.
- Problem-Solving Nature: Each innovation in language design has been driven by the need to solve a fundamental problem that preceding languages could not address; Java is no exception.
The Birth of Modern Programming: C
- Purpose and Impact: The language changed the approach to programming by providing a structured, efficient, and high-level alternative to assembly language for systems programming.
- Language Design Trade-offs: The creation of involved managing specific trade-offs:
- Ease-of-use versus power.
- Safety versus efficiency.
- Rigidity versus extensibility.
- The Pre-C Era: Before , languages like FORTRAN, BASIC, and assembly lacked structure, power, or ease of use.
- Many relied on GOTO statements, which produced what is known as "spaghetti code."
- Pascal was structured but lacked efficiency for systems programming.
- Creation and Standardization:
- Created by Dennis Ritchie in the early .
- It evolved from earlier languages BCPL and B.
- It was standardized by ANSI in .
- Legacy: was designed by and for programmers, a legacy that was later inherited by Java.
C++: The Next Step and Managing Complexity
- Drivers for Evolution: During the late and early , program complexity increased to the point where structured programming reached its limits.
- Programming Evolution Path:
- Binary machine instructions.
- Assembly language.
- High-level languages like FORTRAN (the first widespread language, though it did not encourage clear programs).
- Structured programming (championed by ), which became unmanageable beyond a certain size.
- Object-Oriented Programming (OOP): OOP was the response to this complexity, organizing programs using inheritance, encapsulation, and polymorphism.
- Invention of C++: Invented by Bjarne Stroustrup in and named later. It extends by adding object-oriented features while retaining all of 's benefits. This blend allowed programmers to break the complexity threshold of larger programs.
The Creation of Java
- The Development Team: Conceived at Sun Microsystems in by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and Mike Sheridan.
- Naming History: It was initially called ‐Oak‐ and renamed ‐Java‐ in .
- Initial Mission: Initially intended for consumer electronics, with the goal of being a platform-independent language.
- Pivot to the Internet: As the World Wide Web and the Internet reached critical mass in the early , Java's focus shifted to Internet programming.
- Defining Traits:
- Designed for portability across different CPUs and environments.
- Refined by real working programmers, making it a "programmer's language."
- Not intended as a replacement for , but as a solution to a different set of problems.
- Introduced enhancements such as multithreading and simplified Internet access.
Java and the Internet Environment
- Java Applets: These are small programs that download and run automatically in a browser. They shifted functionality from the server to the client, expanding the types of objects moving between the two.
- Security Features: To ensure safety, applets run in a controlled environment, preventing unauthorized access to the user's system.
- Portability: The same Java code can run on multiple CPUs, operating systems, and browsers.
- Bytecode and the JVM:
- Programs run on the Java Virtual Machine (JVM) rather than directly on local hardware.
- Java code is compiled into an intermediate form called Bytecode.
- This eliminates the need for multiple CPU-specific versions.
- HotSpot Technology: Introduced Just-In-Time (JIT) compilation, which converts bytecode to native code on demand for improved performance without sacrificing portability or safety.
- Servlets (Java on the Server Side):
- A servlet is a small program that executes on the server.
- Just as applets extend browser functionality, servlets extend web server functionality.
- Example Usage: An online store might use a servlet to look up a price in a database and dynamically generate a web page for the client.
- Advantages: Offers increased performance over mechanisms like CGI (Common Gateway Interface) and maintains high portability due to the JVM.
The Java Buzzwords
While portability and security are fundamental, the Java team identified several "buzzwords" to describe the language's key considerations:
- Simple: Designed to be easy for professional programmers to learn and use. Experienced users find the transition straightforward.
- Object-Oriented: Uses a pragmatic, simple, and extendable object model while keeping primitive types high-performance.
- Robust: Enforces early error detection, strict typing, runtime checks, automatic memory management (garbage collection), and exception handling.
- Multithreaded: Supports synchronized multitasking for interactive, networked programs.
- Architecture-Neutral: Aims for "write once; run anywhere, any time, forever," ensuring code longevity across processor upgrades.
- Interpreted and High Performance: Uses bytecode for platform independence but employs JIT compilers for native-code speed.
- Distributed: Supports TCP/IP protocols and Remote Method Invocation (RMI) for networked method calls.
- Dynamic: Carries runtime type information to verify object access and allow safe dynamic linking and updating of bytecode fragments.
- Secure / Portable: Foundational pillars for Internet-based software.
Evolution of Java Versions
- Java 1.1: Added new library elements, redefined event handling, and deprecated some original attributes.
- Java 1.2 (Java 2 / J2SE): Marked the modern age of Java. Introduced Swing, the Collections Framework, and enhanced JVM tools. Deprecated class methods like , , and .
- J2SE 1.3: Focused on functionality additions and environment refinement; maintained compatibility with .
- J2SE 1.4: Introduced the keyword, chained exceptions, and a channel-based I/O subsystem.
- J2SE 5 (Revolutionary Release): Significantly expanded the scope and power of the language. Changes were so major that Sun jumped the version number from to . Key features include:
- Generics
- Annotations
- Autoboxing and auto-unboxing
- Enumerations
- Enhanced for-each style loop
- Variable-length arguments (varargs)
- Static import
- Formatted I/O
- Concurrency utilities
- Java SE 6: Officially dropped the "2" from the name. Enhanced API libraries and the runtime without major syntax changes.
- Java SE 7: The first major release after Oracle acquired Sun Microsystems. Internal version is .
- Project Coin Features: Small changes with high impact, including:
- control in statements.
- Binary integer literals.
- Underscores in numeric literals (e.g., for readability).
- Try-with-resources: Supports automatic resource management (e.g., closing streams automatically).
- Type inference via the diamond operator () for generics.
- Multi-catch: Catching multiple exceptions in a single catch block.
- Library Improvements: Updates to the NIO Framework (NIO.2) and the Fork/Join Framework for parallel programming on multicore processors.
- Project Coin Features: Small changes with high impact, including:
Packages and Name Space Management
- The Problem of Name Collisions: As the number of classes grows, unique naming becomes difficult. Packages provide a mechanism to partition the class name space into manageable chunks.
- Definition of a Package: A package is both a naming and a visibility control mechanism.
- Syntax for Defining a Package: The command must be the first statement in a Java source file:
- Example:
- Hierarchy: Packages can be organized into levels using the period (.) separator:
- Example: correlates to the directory path .
- The Default Package: If no package is specified, classes are placed in a default package with no name. This is common for small programs but inadequate for real applications.
- Storage Rules: Files belonging to a package must be stored in a directory with the exact same name as the package (case-sensitive).
Finding Packages and the CLASSPATH
- Search Mechanism: The Java runtime system finds packages in three ways:
- Looking in the current working directory as the starting point.
- Scanning directory paths set in the environment variable.
- Using the option with the and commands.
- Example Pathing: If a package is named and is stored at , the must be set to (the directory containing the package, not the package directory itself).
- Execution Rule: When executing a class within a package, the class name must be fully qualified.
- Command:
- One cannot simply run if it is inside a package.
Access Protection
Java features four categories of visibility for class members based on the interplay between packages and classes:
- Subclasses in the same package.
- Non-subclasses in the same package.
- Subclasses in different packages.
- Classes that are neither in the same package nor subclasses.
Visibility Summary:
- Public: Accessible from anywhere.
- Private: Not visible outside of its own class.
- Default (No modifier): Visible to subclasses and other classes within the same package.
- Protected: Visible to subclasses in other packages, but not to non-subclasses outside the package.
- Encapsulation: Packages act as containers for classes and other packages, while classes act as containers for data and code. The class is the smallest unit of abstraction.