0% found this document useful (0 votes)
2 views13 pages

Chapter1 Introduction To Java

Chapter 1 introduces Java, explaining its necessity due to the limitations of C and C++, particularly in portability, simplicity, and security. It details how Java's use of bytecode and the Java Virtual Machine (JVM) allows for platform independence, enabling the principle of 'Write Once, Run Anywhere.' The chapter also highlights Java's influence on modern programming languages and its legacy in the computing world.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views13 pages

Chapter1 Introduction To Java

Chapter 1 introduces Java, explaining its necessity due to the limitations of C and C++, particularly in portability, simplicity, and security. It details how Java's use of bytecode and the Java Virtual Machine (JVM) allows for platform independence, enabling the principle of 'Write Once, Run Anywhere.' The chapter also highlights Java's influence on modern programming languages and its legacy in the computing world.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Chapter 1

Introduction to Java
Why Java exists, how it works, and why it took over the world

Before We Begin — Two Important Warnings


Welcome to the Core Java series. A lot of you have been waiting for this, and here it is.
But before we dive in, there are two warnings you need to hear.

WARNING This series does not cover basic Java syntax. If you want a quick syntax
1 tour, there are plenty of playlists for that. Here, we are going deep —
internal architecture, how things work under the hood, why Java was
designed the way it was. Syntax you can always look up from any AI model
if you forget it. Core understanding is what we are building.

WARNING You cannot just passively watch this series and finish it. You need to show
2 up every day, watch a video, and immediately implement everything — the
theory and the code. If questions are given, you practise them. Active
participation is mandatory.

If you are ready with those two warnings and you want to really understand Java from
the inside out — starting from absolute zero and going all the way to advanced — then
let us get started together.

1. Why Java? The Story That Started It All

This is a question most Java courses skip. They tell you what is special about Java, but
never why those things were not in C or C++ already. Let us answer that properly.

1.1 The World Before Java — The 1980s and 1990s


If we talk about the 1980s and 1990s, the entire programming world was dominated by
just two languages: C and its successor C++. These were the most popular languages
around. Why were they so popular?

• They were fast.


• Compared to older languages, they were simpler to write.
• They were low-level — meaning they sat very close to the hardware.

Low-level means the language interacts almost directly with your processor. There were
not many layers of abstraction. If you wanted something done — print to screen, read a
file, allocate memory — you had to write the code for it yourself. Nothing happened
magically behind the scenes.

So in the 80s and 90s, everything was going smoothly. Most work was getting done with
C and C++. Then the question arose: why did we need a new language? Why Java?

1.2 Three Problems That Created Java


C and C++ had three specific problems that became serious enough to demand a new
language. These three problems are the reason Java was born.

Problem What It Meant


Portability C/C++ code compiled for one platform could not run on
another without recompiling.
Simplicity C++ had features like pointers and manual memory
management that made it unnecessarily complex.
Security C++ had no built-in model to safely run code from
untrusted sources.

2. The Portability Problem — Deep Dive

2.1 What Is a Compiler?


Before we get into portability, let us make sure we know what a compiler is. A compiler
is simply a program — software — that reads code written in a human-readable
language like C++ and converts it into machine code. Machine code is sequences of
zeros and ones, the only language a computer actually understands.
Before C and C++, programmers used assembly language — a language with fewer
English words and much closer to the hardware. And before assembly, people literally
wrote sequences of zeros and ones by hand. Compilers exist so we do not have to do
that.

// Simple C++ source code ([Link])


#include <iostream>
using namespace std;

int main() {
cout << "Hello World" << endl;
return 0;
}

// Compiler converts this to machine code (binary) for a specific


platform
// Example machine code for Platform P1: 01001000 01100101 01101100 ...
// Example machine code for Platform P2: 10110100 00001001 11001101 ...

2.2 What Is a Platform?


We keep using the word "platform." Let us define it precisely. A platform has two
components:

• The processor (CPU) — the physical chip doing the computation.


• The operating system — the software managing the hardware resources.

Together, processor + operating system = platform. Here are two examples:

Platform Components
Platform P1 Intel x86 processor + Windows operating system
Platform P2 ARM processor + macOS operating system
2.3 Why Does C/C++ Need to Be Recompiled for Each Platform?
This is the heart of the portability problem. When you compile the same C++ "Hello
World" code for P1 and P2, you get different binary outputs. Why? There are two
reasons.

Reason 1: The Operating System Difference


When your C++ code does something like printing to the console, reading a file, or
allocating memory, it cannot do that alone. It has to ask the operating system to do it.
The OS provides system libraries — compiled code with methods you call to get things
done.

// In your C++ code, you write something like:


cout << "Hello" << endl; // print to console

// Internally, Windows might provide: writeToConsole()


// Internally, macOS might provide: writeThru()

// These are DIFFERENT system library calls.


// So when compiled, the resulting binary code is different for each
OS.

Because Windows and macOS provide different system library methods, the compiled
binary code for the same source file is different on each operating system. This alone
makes the binary platform-specific.

Reason 2: The Processor Difference


A processor is a physical device — hardware. Inside it are billions (sometimes trillions)
of transistors. A transistor has one job: either current flows (1) or it does not (0). This is
why computers only understand binary.

Now, Intel x86 and ARM are two completely different physical processors. They were
designed differently. They have different transistor arrangements, different pins, and
most importantly — a different ISA.

ISA Instruction Set Architecture. This is the grammar of a processor — the set
of low-level instructions it understands, like ADD, LOAD, STORE, JUMP.
Intel x86 and ARM have different ISAs. So the same operation, like adding
two numbers, results in different binary sequences on each processor.

// Your source code just says: add two numbers


int result = a + b;

// On Intel x86, the machine instruction might look like:


// ADD EAX, EBX → binary: 00000011 11000011

// On ARM, the machine instruction might look like:


// ADD R0, R1, R2 → binary: 11100000 10000001 00000000 00000010

// Different ISA = different binary = different machine code

So because both the OS and the processor differ between platforms, the machine code
produced for the same source file is different. This is why C and C++ are called
platform-dependent languages — you have to compile separately for each platform you
want to support.

KEY C/C++ is platform-dependent. Every time you want to run your code on a
POINT new platform, you must recompile it first. That is the portability problem
Java came to solve.

3. How Java Solved the Portability Problem

3.1 The Translator Analogy


Here is a simple analogy to understand Java's approach. You are Indian. You speak
Hindi. You want to travel to two countries: China (Mandarin) and Spain (Spanish).

C/C++ approach: You learn Mandarin before going to China. Then you learn Spanish
before going to Spain. If you move to a third country tomorrow, you learn that language
too. You compile separately for every platform.
Java approach: You bring along one multilingual translator friend. You speak Hindi to
your friend. Your friend translates into whatever language is needed — Mandarin in
China, Spanish in Spain. You do not learn any new language. Your translator handles it.

In Java, that translator friend is called the JVM — the Java Virtual Machine.

3.2 Bytecode — The Intermediate Language


Java introduced a concept called bytecode. Instead of compiling your Java source code
directly into platform-specific machine code, the Java compiler converts it into an
intermediate format called bytecode.

• Bytecode is NOT machine code. It does not contain zeros and ones in the
processor-specific format.
• Bytecode is platform-neutral. The same bytecode file runs on any platform.
• A Java source file has the extension .java. After compilation, you get a .class file
containing bytecode.

// Your Java source file: [Link]


public class Hello {
public static void main(String[] args) {
[Link]("Hello World");
}
}

// Compile with: javac [Link]


// This produces: [Link] (bytecode — NOT machine code)

// Run on any platform: java Hello


// The JVM on that platform reads [Link] and converts it to
machine code.

3.3 The JVM — Java Virtual Machine


The JVM is just software. Despite the impressive name, it is a program that sits on a
specific platform and has one core job: read bytecode and convert it into the machine
code of that platform.
Component Role
Java Compiler (javac) Converts [Link] into [Link] (bytecode)
Bytecode ([Link]) Platform-neutral intermediate code — same file for all
platforms
JVM on Platform P1 Reads bytecode, converts to machine code for Intel x86 +
Windows
JVM on Platform P2 Reads bytecode, converts to machine code for ARM +
macOS

The crucial thing to understand: the bytecode is platform-independent, but the JVM itself
is platform-dependent. Each platform needs its own version of the JVM. But you only
write and compile your code once.

// The workflow:

// Step 1: Write source code (once)


// [Link]

// Step 2: Compile to bytecode (once)


javac [Link] // produces [Link]

// Step 3: Run on any platform (as many as you want)


java Hello // JVM on P1 converts bytecode to P1 machine code
java Hello // JVM on P2 converts bytecode to P2 machine code
java Hello // JVM on P3 converts bytecode to P3 machine code

// Same [Link] file — different JVMs handle the platform-specific


work

WORA Write Once, Run Anywhere. This is Java's famous principle. Compile your
source code once to bytecode. That bytecode can then run on any platform
that has a JVM installed. No recompilation needed.
3.4 Why Portability Was a Practical Need
You might wonder: why does it matter if you have to recompile? In the 1980s and
1990s, new devices were emerging rapidly — set-top boxes, smart TVs, embedded
systems, and the early internet. Each of these had its own platform. The internet also
meant front-end and back-end servers running on completely different hardware.

Developers needed one language they could write once and deploy everywhere.
Recompiling for every new device or server configuration was a serious operational
burden. Java eliminated that burden.

One correction worth making: you do not have just one single JVM friend. You have one
JVM installed on each platform. Each JVM is specific to its platform, but your bytecode
remains universal.

4. Java Is Simple

C++ was simpler than the languages before it, but it still had features that made it
unnecessarily complex for most use cases. Java looked at these and removed them.

C++ Feature Java's Decision


Pointers Removed. Java handles memory references internally.
You never manipulate memory addresses directly.
Multiple Inheritance Removed (for classes). Java uses interfaces instead to
achieve similar results cleanly.
Manual Memory Removed. Java introduced automatic garbage collection —
Deallocation the JVM cleans up unused memory for you.

These three removals made Java dramatically easier to learn and use, while still being
powerful enough for enterprise-scale applications. We will go deep into each of these
concepts — especially garbage collection — in later chapters.

5. Java Is Secure
5.1 Java on the Backend and Frontend
Once Java became popular, it was used everywhere. On the backend, Java Servlets
were used — Java programs that act as API endpoints, fetching data from a database
or processing requests. On the frontend, Java Applets were used — lightweight Java
programs that could be downloaded from a website and run directly inside your
browser.

FUN FACT JavaScript got its name because Java was the most popular language at
the time. The creators of JavaScript wanted their language to sound
equally trendy. Despite the similar name, JavaScript has nothing to do with
Java. It is a completely separate language.

With applets especially, there was a real danger: you click a link, an applet downloads,
and it runs code directly on your machine. What if that code was malicious? What if a
hacker packed harmful instructions inside an applet to steal your data or crash your
system?

5.2 The Sandbox Model


Java's answer to this was the JVM's Sandbox Model. The JVM does not just convert
bytecode to machine code — it also acts as a security gatekeeper. When it runs
bytecode (especially code downloaded from the internet), it runs it inside a restricted,
controlled environment.

• If the code tries to access system files it should not have access to — blocked.
• If the code tries to grab permissions beyond what it needs — denied.
• If the code tries to perform operations that could harm the system — restricted.

// Conceptual illustration of the Sandbox Model


// (This is not actual Java code — it shows the idea)

class JVM {
void runBytecode(byte[] bytecode) {
SecurityManager sandbox = new SecurityManager();

// Check each operation before executing


if ([Link](operation)) {
execute(operation);
} else {
throw new SecurityException("Access denied by sandbox");
}
}
}

SANDBOX A sandbox is a restricted space where code runs in isolation. It cannot


MODEL reach outside its boundaries to harm the real system. Java's JVM provides
this sandbox automatically for all bytecode it runs.

This means the JVM alone solves two of Java's three core properties: portability (via
bytecode conversion) and security (via the sandbox model). Java's syntax handles
simplicity. It is remarkable that so much of what makes Java great comes from one
piece of software.

5.3 Applets Are Gone — But the Principle Remains


Java applets were deprecated around JDK 9 and removed in JDK 11. Nobody uses
them anymore because JavaScript took over front-end development entirely. But the
security model that Java pioneered — running untrusted code in a sandboxed JVM — is
still relevant and still in use in Java backends today.

6. Java's Influence on Modern Languages

After Java proved that a language could be platform-independent using bytecode and a
virtual machine, other languages followed the same idea.

Microsoft looked at this and built C#. C# is a close cousin of C++ — in fact, that is why
its name starts with C. C# runs on the .NET runtime (Microsoft's version of the JVM)
and is also platform-independent. It never became as globally dominant as Java, but it
is widely used especially in enterprise and game development with Unity.

Python, which came into popularity in the 2000s, also follows the same principle
internally — source code is compiled to bytecode, which runs on a Python interpreter
(Python's version of the JVM).
Language Virtual Machine / Runtime
Java JVM — Java Virtual Machine
C# .NET CLR — Common Language Runtime
Python CPython interpreter (also uses bytecode internally)
Kotlin JVM (Kotlin compiles to the same bytecode as Java)
Scala JVM (same bytecode, runs alongside Java)

Java introduced the concept of platform independence that the whole industry adopted.
That is why it is not just a language — it is a milestone in the history of computing.

Chapter Summary

Concept Key Takeaway


Why Java? C/C++ had three problems: portability, simplicity, and
security. Java was built to solve all three.
Platform Processor + Operating System = Platform. Different
platforms produce different machine code from the same
source.
Compiler Software that converts source code into machine code.
C/C++ compilers are platform-specific.
ISA Instruction Set Architecture. The grammar of a processor.
Different processors have different ISAs.
Bytecode Platform-neutral intermediate code produced by the Java
compiler. Stored in .class files.
JVM Java Virtual Machine. Platform-specific software that reads
bytecode and converts it to machine code.
WORA Write Once, Run Anywhere. Compile to bytecode once.
Run on any platform with a JVM.
Simplicity Java removed pointers, multiple inheritance, and manual
memory deallocation from C++.
Sandbox Model The JVM runs bytecode in a restricted environment,
blocking harmful or unauthorised operations.
Java's Legacy Java introduced platform independence. C#, Python,
Kotlin, and Scala all follow the same principle.
Practice Exercises

1. In your own words, explain what a platform is. Give two examples of different
platforms with their processor and operating system.
2. What is the difference between source code, bytecode, and machine code? Draw
a diagram showing how a Java file goes from .java to running on two different
platforms.
3. Why can a static method in Java only call other static methods? (Hint: think about
when static methods exist versus when instance methods exist.)
4. Research: What is the JDK? What is the JRE? How do they relate to the JVM?
We will cover this in the next chapter, but see what you can find first.
5. Write a Java Hello World program, compile it using javac, and run it using java.
Observe the .class file that gets generated. Open it in a text editor and note what
you see — this is bytecode.

You might also like