JAVA PROGRAMMING
Unit 1 – Introduction to Java | Lecture Notes
1. History of Java
Java has its roots in a 1991 Sun Microsystems research effort, but the key events that shaped the
language happened through the mid-1990s:
• 1991–1995: Sun Microsystems wanted to create a new programming language for use inside its own
consumer electronics products (set-top boxes, smart appliances), independent of any single
hardware platform.
• A dedicated team called the “Green Team” was formed to build this language. The team leader was
James Gosling, along with Mike Sheridan and Patrick Naughton.
• The language was ready by the end of 1995. It was initially named “Oak” (after an oak tree outside
Gosling's office), but the name Oak was already trademarked by another company, so it could not be
used.
• The team finally renamed the language “Java” (inspired by Java coffee) and officially released it in
1996 as Java 1.0, under the slogan “Write Once, Run Anywhere.”
• Java is an object-oriented programming (OOP) language and quickly became one of the most
popular programming languages in the world due to its portability and simplicity.
• James Gosling is widely known as the “Father of Java.”
• From 1996 to 2010, Java was owned and maintained by Sun Microsystems.
• In 2010, Oracle Corporation acquired Sun Microsystems and, with it, ownership of Java. Oracle
continues to maintain Java at present.
• Java has evolved through many versions since then; as of the current release cycle, Java has
reached version 23, with new feature releases now issued every six months.
2. What is an Object-Oriented Programming (OOP) Language?
An object-oriented programming language is a language that supports the creation of objects and
follows the core OOP principles:
• Inheritance – acquiring properties and behavior from another class.
• Polymorphism – the ability of a method or object to take many forms.
• Encapsulation – wrapping data and methods together as a single unit and restricting direct access.
• Abstraction – hiding implementation details and showing only essential features.
Languages that support these four principles are called OOP languages. Examples: C++, Java,
Python, C#, etc.
3. What is a Platform?
A platform is the combination of the underlying operating system and the hardware (such as the
microprocessor or co-processor) on which a program runs. Different platforms include Windows on
Intel hardware, Linux on ARM hardware, and so on. A program's behavior with respect to a platform
determines whether it is platform-dependent or platform-independent.
4. Platform-Dependent vs Platform-Independent Languages
4.1 Platform-Dependent Languages – Example: C, C++
Programs written in C/C++ are compiled directly into native machine code for a specific operating
system. This means the program must be developed, compiled, and executed on the same
operating system (or recompiled separately for each target OS). A C++ executable built for Windows
will not run on Linux without recompilation.
4.2 Platform-Independent Languages – Example: Java, Python
Java and Python programs are developed and compiled once, on any one operating system, but the
compiled output can then be executed on any other operating system without modification. This
flexibility — write the code once, and let it run anywhere — is one of the biggest advantages that made
Java so popular for enterprise and web applications.
5. How Java Achieves Platform Independence
When we compile a Java program, the Java compiler (javac) does not directly generate machine-level
(ML) code the way a C/C++ compiler does. Instead, it produces an intermediate form of code called
byte code.
• Byte code is neither pure high-level language code nor pure machine-level code — it sits in between
the two, stored in a .class file.
• The microprocessor of the computer cannot directly understand or execute byte code.
• The Java Virtual Machine (JVM) is the software component that reads this byte code and converts
it into the machine-level code appropriate for the underlying operating system and hardware.
• Since every operating system has its own JVM implementation, the same .class byte code file can
be handed to the JVM on Windows, Linux, or macOS, and each JVM converts it correctly for that
machine.
• This is exactly why Java is described using the principle “WORA” – Write Once, Run Anywhere.”
The JVM itself is platform-dependent (a different JVM build exists for each OS), but because that
dependency is hidden inside the JVM, the Java program and its byte code remain
platform-independent.
Summary of file types: Source file (.java) → contains High-Level Language (HLL) code | Class file (.class)
→ contains byte code, generated by the Java compiler.
6. Compiler and Interpreter in Java (JIT)
6.1 What is an Interpreter?
An interpreter is a piece of software that converts high-level code into machine-level (ML) code line
by line, executing each line immediately rather than translating the whole program up front.
6.2 Java – Both Compiled and Interpreted
Java is unique in that it is both a compiled and an interpreted programming language:
• The javac compiler first converts the .java source file into byte code (.class file) — this is the
compilation step.
• Internally, the JVM uses a combination of a JIT (Just-In-Time) compiler and an interpreter to
convert this byte code into machine-level code for execution.
• JIT stands for Just-In-Time compilation. It compiles frequently-used byte code sections directly into
native machine code at run time, which significantly improves performance compared to pure
interpretation.
7. Where to Develop Java Programs
Java programs can be written using very simple text editors, or using full-featured development
environments:
• Simple editors: Notepad, EditPlus, Notepad++, etc.
• IDEs (Integrated Development Environment): Eclipse IDE, IntelliJ IDEA, STS – Spring Tool Suite,
etc.
While simple editors work for small programs, it is highly recommended to use an IDE for real
development, since IDEs provide auto-completion, error detection, debugging tools, and project
management features that greatly increase productivity.
8. Java Installation
8.1 Steps to Install Java (JDK)
• Search for “Java download” or go directly to the official Oracle download page:
[Link]
• Select your operating system (Windows, macOS, Linux).
• Download the appropriate installer – for 64-bit Windows systems this is usually the x64 Installer
(.exe / .msi).
• Run the installer and follow the on-screen instructions to complete installation.
8.2 Setting the PATH Environment Variable
After installation, the bin folder of the JDK must be added to the system PATH so that Java commands
can be run from any command prompt location:
C:\Program Files\Java\jdk-23\bin
• Go to Search → Environment Variables (or System Properties → Advanced → Environment
Variables).
• Under the “User variables” section, locate the Path variable.
• Click Path → Edit → New, then paste the JDK bin path copied above, and click OK to save.
8.3 Checking the Installed Version
• Open the Command Prompt.
• Run javac -version to check the compiler version.
• Run java -version to check the JVM/runtime version.
9. Features of Java (Buzzwords)
Java's design is commonly summarized using the following set of “buzzwords” that describe its key
features:
Feature Feature
Simple Distributed
Portable Object-Oriented
Architecture Neutral Robust
Platform Independent Interpreted
Multithreaded High Performance
Secure
Dynamic
Each of these reflects a design goal: for example, Simple means Java removed complex features of
C++ like pointers and multiple inheritance; Portable and Platform Independent both stem from the
byte code + JVM mechanism explained in Section 5; Multithreaded means Java has built-in support
for running multiple tasks concurrently within a program.
10. Robust and Secure Nature of Java
10.1 Robust
Java is called robust because it provides a powerful memory management system and strong error
handling, which together reduce the chances of program crashes:
• Garbage Collector (GC): Automatically reclaims memory occupied by objects that are no longer in
use, so the programmer does not need to manually free memory (unlike C/C++).
• Powerful Exception Handling Mechanism: Runtime errors (exceptions) can be caught and
handled gracefully using try-catch blocks, preventing abrupt program termination.
10.2 Secure – How Java Verifies a Program Before Running It
Java's security model includes a verification step that happens before byte code is ever executed:
• A .java file (high-level language code) is compiled by the Java compiler into a .class file containing
byte code.
• Before execution, the Byte Code Verifier checks the byte code for illegal instructions, memory
access violations, or signs of virus/tampering.
• If verification passes — the byte code is handed to the JVM, converted to machine-level code, and
the program executes normally.
• If a virus or illegal instruction is present — the verifier discards execution, preventing a corrupted
or malicious .class file from running the full program (it may halt execution partway, resulting in only a
partially executed / halted program rather than a full run).
This byte-code verification step is one reason Java is considered more secure by design than languages that
compile directly to native machine code.
11. JDK, JRE, and JVM
11.1 JDK – Java Development Kit
The JDK contains all the tools required to develop a Java application — including the compiler
(javac), debugger, and other development utilities. It also includes the JRE within it.
11.2 JRE – Java Runtime Environment
The JRE provides everything required to run a compiled Java application (but not to develop one). It
contains the libraries and classes needed by Java programs at run time, and it generates a single,
consolidated runtime environment from the many separate class/library files.
11.3 JVM – Java Virtual Machine
The JVM is the core engine that actually runs Java programs. It provides the runtime memory area
(working with the system RAM) and is responsible for:
• Loading .class byte code files into memory.
• The complete execution of the Java program (interpreting/JIT-compiling byte code to machine
code).
• Memory allocation and de-allocation for objects created during program execution (working
closely with the Garbage Collector).
Relationship: JDK ⊃ JRE ⊃ JVM — the JDK contains the JRE, and the JRE contains the JVM along with the
supporting libraries.
12. Introduction to Class Loader and Methods
12.1 Class Loader
The class loader is a part of the JVM responsible for loading .class byte code files into the JVM's
runtime memory (RAM) so that the program can be executed. It locates, reads, and dynamically
loads classes as they are needed during program execution.
12.2 What is a Method?
A method in Java is equivalent to what is called a function, module, routine, subroutine, or
procedure in other programming languages — a named block of code that performs a specific task
and can be called (invoked) whenever that task needs to be performed.
12.3 Components of a Method
• Access Modifier: Controls the visibility of the method — common modifiers are public, private, and
protected.
• Method Name: An identifier used to call the method, e.g. sum, main, etc.
13. First Java Programs
13.1 Program 1 – Welcome Message
class welcome
{
public static void main(String[] args)
{
[Link]("Welcome to GDT.");
}
}
Compiling and Running:
F:\> javac [Link]
F:\> java welcome
Output:
Welcome to GDT.
13.2 Program 2 – Hi / Hello Message
class hello
{
public static void main(String[] args)
{
[Link]("Hi..");
[Link]("Hello..");
}
}
Compiling and Running:
F:\> javac [Link]
F:\> java hello
Output:
Hi..
Hello..
Note: The class name in the code must match the source file name (e.g. [Link] for class hello), and the
main method — public static void main(String[] args) — is the entry point from which the JVM begins executing
every Java program.