Introduction to Java
Goal of this chapter: Build a strong mental model of what Java is, why it was created, how Java programs run, why Java is called platform-independent, and where Java fits in the software world.
Learning style: Simple language first, technical details second. Do not worry if terms such as JVM, bytecode, JDK, and JRE are new. They will become clear step by step.
1. What is Java?#
Java is a high-level, general-purpose programming language used to build software.
In simple words:
Java is a language that lets us give instructions to a computer in a structured and readable way.
With Java, we can create:
- Backend applications
- Web applications
- Enterprise software
- Banking systems
- Desktop applications
- Large distributed systems
- Android applications and Android-related software
- APIs and services
- Cloud applications
- Data-processing applications
- Development tools
- Educational and academic software
Java is especially popular for large applications because it provides a large standard library, automatic memory management, strong type checking, object-oriented programming, concurrency support, and a mature ecosystem.
Java is not just the syntax you type into a .java file.
A complete Java environment includes several important pieces:
Your Java Code
↓
Java Compiler
↓
Bytecode
↓
JVM
↓
Machine Instructions
↓
CPU
Understanding this pipeline is one of the most important foundations of Java.
2. Why Do We Need Programming Languages?#
A computer ultimately works with very low-level instructions that the processor can execute.
Humans, however, do not naturally want to write programs as raw machine instructions.
Imagine having to write something like:
10110000 01100001
10110001 01100010
...
for every operation.
That would be extremely difficult.
Programming languages solve this problem.
Instead of thinking directly in machine instructions, we can write something much closer to human reasoning:
int a = 10;
int b = 20;
int sum = a + b;
System.out.println(sum);
The Java compiler and runtime system handle the difficult translation and execution work.
So we can think of a programming language as a bridge:
Human Thinking
↓
Programming Language
↓
Compiler / Runtime
↓
Machine Instructions
↓
Computer
3. Before Java: Why Was Java Needed?#
Java did not appear in isolation.
Before Java became popular, languages such as C and C++ were widely used.
C and C++ are powerful languages and are still extremely important.
However, software developers faced several challenges when building portable applications.
One major problem was that a program compiled for one machine or operating system could depend on details of that environment.
For example:
Program
↓
Compiled for Windows
↓
Machine-specific executable
That executable cannot automatically be treated as the same executable for every other operating system and CPU architecture.
A different environment may require a different compilation target.
Java took a different approach.
Instead of normally compiling Java source code directly into a native executable for every target machine, Java source code is compiled into an intermediate form called bytecode.
Java Source Code
↓
javac
↓
Java Bytecode
↓
JVM
↓
Native Machine Instructions
This architecture became one of Java's most famous characteristics.
4. A Very Short History of Java#
Java was developed at Sun Microsystems.
The language began in the early 1990s as part of a project called the Green Project.
The project was associated with engineers including James Gosling.
The language was originally called Oak.
Later, it was renamed Java.
Java became publicly available in the mid-1990s and grew rapidly as the internet and enterprise software expanded.
Over time, Java evolved from a relatively new language into a large platform and ecosystem.
Today, Java is maintained through an ongoing release process and is used in many kinds of software.
Important historical idea#
Java's original goal was not simply:
"Make another programming language."
A major goal was to create a language and runtime environment that could support portable software across different systems.
That idea strongly influenced Java's design.
5. What Does "Java" Actually Mean?#
When people say "Java", they may mean several related things.
For example:
Java Language
Java Platform
JDK
JRE
JVM
Java Standard Library
Java Ecosystem
These are related, but they are not exactly the same thing.
A beginner often hears:
"Java runs on the JVM."
That is true, but incomplete.
A better mental model is:
Java Platform
│
┌──────────────┴──────────────┐
│ │
JVM Runtime Java Libraries
│
│
Executes Bytecode
│
↓
Operating System
│
↓
CPU
Later chapters will go much deeper into each part.
6. Why Is Java Called a High-Level Language?#
A high-level language hides many low-level details from the programmer.
For example:
int age = 20;
You do not normally need to manually tell the CPU which physical register should contain the value.
You also do not normally manually allocate and free every piece of memory.
Java provides abstractions that make programming easier.
Compare the idea:
Low-level programming
↓
More control over machine resources
↓
More responsibility
High-level programming
↓
More abstraction
↓
Less low-level responsibility
This does not mean Java is "weak".
It means Java provides a higher level of abstraction.
7. Is Java a Compiled Language or an Interpreted Language?#
This is a common beginner question.
The short answer is:
Modern Java uses both compilation and runtime execution techniques.
First, Java source code is compiled into bytecode.
.java
↓
Java Compiler
↓
.class
The .class file contains Java bytecode.
Then the JVM executes that bytecode.
.class
↓
JVM
↓
Execution
The JVM can use interpretation, JIT (Just-In-Time) compilation, and other runtime techniques.
So saying:
"Java is only an interpreted language"
is an oversimplification.
Likewise:
"Java works exactly like a traditional ahead-of-time native compiler"
is also an oversimplification.
The important beginner model is:
SOURCE CODE
↓
javac
↓
BYTECODE
↓
JVM
↓
MACHINE CODE / EXECUTION
8. What is Bytecode?#
Bytecode is the intermediate instruction format produced by the Java compiler.
Suppose we write:
public class Main {
public static void main(String[] args) {
System.out.println("Hello Java");
}
}
The source file may be:
Main.java
After compilation:
javac Main.java
we get:
Main.class
The .class file contains bytecode.
The important point is:
Java source code
≠
Native CPU machine code
Instead:
Java source code
↓
Bytecode
↓
JVM
↓
Native execution
This intermediate bytecode is central to Java's portability model.
9. What is the JVM?#
JVM stands for:
Java Virtual Machine
The JVM is the runtime environment responsible for executing Java bytecode.
You can imagine the JVM as a software machine that understands Java bytecode.
It is called a "virtual machine" because it behaves like an abstract machine.
For example:
Java Program
↓
Bytecode
↓
┌───────────────┐
│ JVM │
│ │
│ Loads classes │
│ Executes code │
│ Manages memory│
│ Runs GC │
│ JIT compiles │
└───────────────┘
↓
Operating System
↓
CPU
The JVM itself is not the Java programming language.
It is the runtime machine that executes Java bytecode.
10. Why Do We Need the JVM?#
Suppose you have written a Java program.
You want it to run on:
Windows
Linux
macOS
The Java source code can be compiled into bytecode.
Then each operating system can have an appropriate JVM implementation.
Java Source
↓
Bytecode
↓
┌──────────┼──────────┐
↓ ↓ ↓
Windows Linux macOS
JVM JVM JVM
↓ ↓ ↓
CPU CPU CPU
The JVM provides the platform-specific layer.
This is a major reason Java applications can be portable.
11. "Write Once, Run Anywhere"#
You will often hear this phrase associated with Java:
Write Once, Run Anywhere (WORA)
The basic idea is:
Write Java Code
↓
Compile to Bytecode
↓
Run bytecode on a compatible JVM
The same bytecode can, in principle, run on different operating systems if suitable JVM implementations exist.
However, do not interpret WORA as:
"Every Java program will always run everywhere without any changes."
Real applications can depend on:
- Operating-system features
- Native libraries
- File-system behavior
- Environment variables
- External programs
- Hardware
- Platform-specific configuration
So the better statement is:
Java's bytecode + JVM architecture provides strong platform independence for Java applications.
12. Platform Independence#
Let's understand this carefully.
A platform generally includes things such as:
- Operating system
- CPU architecture
- Runtime environment
- System libraries and other environment details
For example:
Windows + x86-64
Linux + x86-64
Linux + ARM64
macOS + ARM64
These environments are different.
Java tries to place a portable bytecode layer between the Java application and the underlying machine.
Java Application
↓
Bytecode
↓
┌───────────┴───────────┐
↓ ↓
JVM JVM
↓ ↓
Windows Linux
↓ ↓
CPU CPU
The JVM handles the details of executing bytecode on its host platform.
13. Java vs C/C++#
This comparison is useful, but it must not become a "which language is better?" argument.
Different languages make different trade-offs.
C#
C is a procedural systems programming language.
It provides a relatively direct relationship with memory and hardware.
Typical characteristics include:
- Low-level control
- Manual memory management
- Native compilation
- High performance
- Small runtime abstraction compared with Java
C++#
C++ builds on C and adds powerful abstractions, including:
- Classes
- Templates
- RAII
- Operator overloading
- Generic programming
- Object-oriented programming
- Low-level control
C++ can also compile to native machine code.
Java#
Java focuses strongly on:
- Portability through bytecode and JVMs
- Automatic memory management
- Object-oriented programming
- Strong type checking
- Large standard libraries
- Runtime services
- Concurrency support
- Large ecosystem
A simplified comparison:
| Feature | C | C++ | Java |
|---|---|---|---|
| Typical compilation | Native | Native | Bytecode + JVM |
| Garbage collector | No built-in GC | No general GC | Yes |
| Manual memory control | Strong | Strong | Much less |
| OOP | Not class-based OOP | Yes | Yes |
| Runtime VM | No JVM | No JVM | JVM |
| Portability model | Recompile per target | Recompile per target | Bytecode + JVM |
| Pointer arithmetic | Yes | Yes | No direct pointer arithmetic |
| Standard library | Smaller | Large | Large |
| Runtime safety | Lower-level | Lower-level | More managed |
This table is simplified. Real implementations have many details.
14. Major Features of Java#
Java became popular because of a combination of language and platform features.
Important characteristics include:
14.1 Simple#
Java removed or avoided several complicated features found in some earlier languages.
For example, Java does not provide traditional pointer arithmetic to application programmers.
The language still has many advanced features, but its basic syntax is approachable.
14.2 Object-Oriented#
Java is heavily based on object-oriented programming.
You will eventually learn concepts such as:
Class
Object
Encapsulation
Inheritance
Polymorphism
Abstraction
Interface
Composition
This will become one of the largest sections of this course.
14.3 Platform Independent#
Java source code is compiled to bytecode.
Bytecode can run on a suitable JVM implementation.
14.4 Automatic Memory Management#
Java provides garbage collection.
Instead of manually freeing ordinary objects like in languages such as C, Java's runtime can identify objects that are no longer reachable and reclaim memory.
Example:
Student student = new Student();
If an object becomes unreachable:
student = null;
that does not mean the object is immediately destroyed.
It means the object may become eligible for garbage collection if there are no other references to it.
Garbage collection is discussed in much more detail later.
14.5 Strongly Typed#
Java is a statically typed language.
For example:
int age = 20;
The variable age has type int.
This allows the compiler to catch many type-related mistakes before the program runs.
14.6 Robust#
Java includes features designed to make programs safer and more reliable, including:
- Strong type checking
- Exception handling
- Automatic memory management
- Runtime checks
- Array bounds checks
This does not mean Java programs cannot have bugs.
It means the platform provides many mechanisms to prevent or detect common classes of problems.
14.7 Multithreaded#
Java has built-in support for concurrent programming.
You can create programs that perform multiple tasks concurrently.
Examples include:
Server handling many requests
Downloading data
Processing files
Running background tasks
Parallel computation
Multithreading and modern concurrency will be covered later.
14.8 Secure by Design#
Java was designed with several security-related mechanisms.
The language avoids direct pointer arithmetic and provides runtime checks.
However, "Java is secure" should not be interpreted as:
"Java programs can never have security vulnerabilities."
Security ultimately depends on the complete application, libraries, configuration, dependencies, deployment, and developer practices.
14.9 Portable#
Java bytecode and JVM implementations provide a strong portability model.
The goal is to separate the application bytecode from the details of a particular operating system and CPU.
14.10 High Performance#
Java is not simply an interpreted scripting language.
Modern JVMs can use JIT compilation to optimize frequently executed code at runtime.
A simplified idea is:
Bytecode
↓
JVM observes execution
↓
Frequently executed code identified
↓
JIT compilation / optimization
↓
Faster native execution
Modern JVMs contain sophisticated optimization systems.
15. Java Is Not Just One Thing#
A beginner may think:
Java = Programming Language
That is incomplete.
A better model is:
JAVA ECOSYSTEM
│
┌───────────────┼────────────────┐
│ │ │
Language JVM Standard Library
│ │ │
Syntax Runtime Collections
Classes Memory I/O
Types Threads Networking
Methods GC Utilities
│
└───────────────┬────────────────┘
↓
Java Platform
This distinction becomes important when we talk about the JDK, JRE, and JVM.
16. JVM vs JRE vs JDK#
These three terms confuse almost every Java beginner.
Let's start with the simplest version.
JVM#
JVM = Java Virtual Machine
Its primary job is to execute Java bytecode.
.class file
↓
JVM
↓
Execution
JRE#
JRE = Java Runtime Environment
Historically, the JRE concept referred to the environment needed to run Java applications, including the JVM and runtime libraries.
Simplified model:
JRE
├── JVM
└── Runtime Libraries
Modern Java distributions do not necessarily ship a separate end-user "JRE" product in the old way. The term is still useful conceptually.
JDK#
JDK = Java Development Kit
The JDK is what developers use to develop Java programs.
It provides tools such as the Java compiler and runtime-related tools.
Simplified:
JDK
├── Java compiler
├── Java launcher
├── Development tools
├── JVM
└── Java libraries
For learning and developing Java, you normally install a JDK.
17. The Relationship#
A useful conceptual relationship is:
JDK
│
├── Development Tools
│ └── javac
│
└── Runtime Components
├── JVM
└── Java Libraries
Remember:
JDK → Build and run Java programs
JVM → Execute Java bytecode
The historical JRE concept describes the runtime environment, but modern JDK distributions are the practical installation choice for developers.
18. What Happens When We Run a Java Program?#
Let's use the simplest example.
public class Main {
public static void main(String[] args) {
System.out.println("Hello Java");
}
}
Save it as:
Main.java
Now compile:
javac Main.java
The compiler produces:
Main.class
Then run:
java Main
The flow is:
Main.java
│
↓
javac
│
↓
Main.class
│
Bytecode
│
↓
JVM
│
↓
Execution
│
↓
Hello Java
This simple pipeline is worth remembering.
19. What Does javac Do?#
javac is the Java compiler command.
It converts Java source code into bytecode.
Example:
javac Main.java
Conceptually:
Main.java
↓
javac
↓
Main.class
The compiler also checks many things, such as:
- Syntax errors
- Type errors
- Invalid declarations
- Invalid method calls
- Other compile-time rules
For example:
int age = "hello";
This is invalid because a String cannot be assigned to an int.
The compiler can detect this before normal execution.
20. What Does java Do?#
The java command launches a Java application using a JVM.
For example:
java Main
This tells the Java runtime to load the Main class and start execution from its main method when it is a valid application entry point.
So:
javac Main.java
means:
Compile my source code.
While:
java Main
means:
Run the compiled Java application.
Do not normally write:
java Main.class
when launching a normal class by name.
Use:
java Main
21. Why Is the File Called .java?#
A Java source file normally ends with:
.java
Example:
Main.java
Student.java
Car.java
BankAccount.java
After compilation, the compiler normally produces class files ending with:
.class
Example:
Main.class
Student.class
Car.class
So:
.java
↓
source code
.class
↓
compiled Java bytecode
22. What Is a Class File?#
A .class file contains compiled Java bytecode and class-related information.
For example:
Student.java
may compile into:
Student.class
If a source file contains multiple classes, compilation can produce multiple .class files.
That is why the relationship is not always simply:
one .java = one .class
The important concept is that Java classes are represented in compiled class files.
23. Java's Execution Model#
Here is the full beginner-friendly picture:
┌──────────────────────┐
│ Java Source Code │
│ Main.java │
└──────────┬───────────┘
│
│ javac
↓
┌──────────────────────┐
│ Java Bytecode │
│ Main.class │
└──────────┬───────────┘
│
│
↓
┌──────────────────────┐
│ JVM │
│ │
│ Class Loading │
│ Bytecode Execution │
│ JIT Compilation │
│ Memory Management │
│ Garbage Collection │
│ Thread Management │
└──────────┬───────────┘
│
↓
┌──────────────────────┐
│ Operating System │
└──────────┬───────────┘
│
↓
┌──────────────────────┐
│ CPU │
└──────────────────────┘
Do not worry if every box is not clear yet.
Each one will be explored later.
24. What Makes Java Different From a Native Executable?#
Consider a traditional native compilation model:
Source Code
↓
Compiler
↓
Native Machine Code
↓
CPU
The output is designed for a particular target environment.
Java commonly uses:
Source Code
↓
Java Compiler
↓
Bytecode
↓
JVM
↓
Native Execution
This additional runtime layer gives Java its characteristic portability and runtime-management model.
25. Is Java Slow Because of the JVM?#
You may hear an old statement:
"Java is slow because it runs inside a virtual machine."
That statement is outdated and oversimplified.
Modern JVMs perform sophisticated optimizations, including JIT compilation.
The JVM can monitor code while the program runs and optimize frequently executed paths.
Conceptually:
Java Bytecode
↓
JVM
↓
Interpret / execute
↓
Observe program behavior
↓
Find hot code
↓
JIT compile + optimize
↓
Fast native execution
Java performance depends on:
- JVM implementation
- Application design
- Algorithms
- Memory behavior
- Garbage collection
- I/O
- Concurrency
- Hardware
- Configuration
- Workload
So the correct conclusion is:
Java has a runtime layer, but modern JVMs can achieve very high performance.
26. Where Is Java Used?#
Java has historically been and remains important in many areas.
Backend Development#
Java is widely used to build:
REST APIs
Web servers
Microservices
Enterprise applications
Distributed systems
Frameworks such as Spring build on Java.
Banking and Financial Systems#
Java is widely used in large enterprise systems where developers care about:
- Reliability
- Security
- Scalability
- Maintainability
- Large development teams
Enterprise Software#
Large organizations often have applications with:
Millions of lines of code
Many developers
Large databases
Many services
Long maintenance periods
Java's ecosystem is well suited to this type of development.
Android#
Java has historically played a major role in Android development.
Modern Android development also heavily uses Kotlin, but Java remains an important part of the Android ecosystem and existing codebases.
Cloud and Distributed Systems#
Java is commonly used for:
Backend services
Microservices
Distributed applications
Message processing
Cloud applications
27. Java Editions: Java SE, EE/Jakarta EE, ME#
You may encounter terms such as:
Java SE
Java EE
Java ME
These refer to different Java platform specifications and environments.
Java SE#
Java SE = Java Platform, Standard Edition
This is the core Java platform.
It includes fundamental Java language and library features.
Examples:
Classes
Objects
Collections
Strings
Exceptions
I/O
Threads
Streams
Date/Time
For this course, Core Java / Java SE is our main focus.
Java EE / Jakarta EE#
Java EE was the enterprise edition of Java.
It later evolved under the name Jakarta EE.
It focuses on enterprise application specifications and technologies.
Examples include technologies used for:
- Web applications
- Enterprise APIs
- Dependency injection
- Persistence
- Messaging
This course focuses on Core Java first because enterprise frameworks become much easier after the Java foundation is strong.
Java ME#
Java ME was designed for constrained devices and embedded environments.
It is much less relevant to a beginner learning modern general-purpose Java development, but the term is useful to recognize.
28. Java Is Case-Sensitive#
Java is case-sensitive.
These are different identifiers:
age
Age
AGE
For example:
int age = 20;
System.out.println(age);
works.
But:
System.out.println(Age);
does not refer to the same variable.
Case sensitivity is important throughout Java.
29. Java Naming Conventions#
Good naming makes code easier to understand.
Common conventions include:
Classes#
Use PascalCase:
Student
BankAccount
EmployeeManager
Variables#
Use camelCase:
studentName
accountBalance
totalMarks
Methods#
Use camelCase:
calculateSalary()
printDetails()
getName()
Constants#
Often use uppercase with underscores:
MAX_SIZE
DEFAULT_TIMEOUT
PI
Naming conventions do not usually determine whether code compiles.
They make code easier for humans to read.
30. The First Java Program#
Here is a minimal Java program:
public class Main {
public static void main(String[] args) {
System.out.println("Hello Java");
}
}
Output:
Hello Java
Do not worry if this syntax looks strange.
We will break it down later.
For now, recognize these parts:
public class Main
↓
class
main(...)
↓
program entry point
System.out.println(...)
↓
print output
31. Understanding the main Method#
The standard entry point of a simple Java application is:
public static void main(String[] args)
Each part has a purpose.
public
↓
accessible to the launcher
static
↓
belongs to the class rather than requiring an object
void
↓
does not return a value
main
↓
method name used as the conventional application entry point
String[] args
↓
command-line arguments
We will study methods, static, arrays, classes, and objects in depth later.
For now, remember the shape:
public static void main(String[] args) {
}
32. A Simple Mental Model of Java#
When learning Java, keep this picture in your head:
JAVA
│
┌─────────┴─────────┐
│ │
Language Platform
│ │
┌────┼────┐ ┌─────┼─────┐
│ │ │ │ │ │
Types Classes Methods JVM Libraries Tools
│
↓
Bytecode
↓
Execution
Java is both:
- A programming language.
- A platform/ecosystem around that language.
33. The Most Important Beginner Concepts#
After this chapter, you should be able to explain these terms in simple language:
Java#
A high-level, general-purpose programming language and platform ecosystem.
Source Code#
The human-readable Java code written in .java files.
Compiler#
A tool that translates Java source code into bytecode and performs compile-time checks.
Bytecode#
The intermediate code stored in .class files and executed by a JVM.
JVM#
The Java Virtual Machine that loads and executes Java bytecode.
JDK#
The development kit used to develop Java applications, including tools such as the compiler and runtime components.
JRE#
A historical/conceptual term for the runtime environment needed to run Java applications; modern Java development normally uses a JDK distribution rather than installing a separate JRE.
Platform Independence#
The ability to run the same Java bytecode on different systems when compatible JVM implementations are available.
34. Common Beginner Confusions#
Confusion 1: Java and JVM are the same#
They are not.
Java
↓
Programming language + platform ecosystem
JVM
↓
Runtime machine that executes bytecode
Confusion 2: Java source code directly becomes machine code#
The beginner model is:
.java
↓
bytecode
↓
JVM
↓
machine execution
Confusion 3: JVM means Java compiler#
No.
The compiler is commonly:
javac
The JVM executes bytecode.
javac → compilation
JVM → runtime execution
Confusion 4: JDK and JVM are the same#
No.
JDK
↓
Development environment
JVM
↓
Executes bytecode
Confusion 5: Platform independent means OS independent in every possible situation#
Not exactly.
Java bytecode is designed to be portable across compatible JVM implementations, but an application can still depend on operating-system-specific resources.
Confusion 6: Garbage collection means memory is freed immediately#
No.
An object becoming unreachable makes it eligible for garbage collection.
The programmer should not assume exactly when garbage collection will happen.
35. Why You Should Learn Java Internals Early#
Many students try to memorize syntax:
class
static
public
new
extends
implements
without understanding what is happening underneath.
That creates problems later.
For example, when you learn:
Student s = new Student();
you should eventually understand:
Student
↓
class/type
s
↓
reference variable
new Student()
↓
creates an object
object
↓
exists in memory
This becomes extremely important when we study OOP.
The goal of this course is therefore not just:
"Memorize Java syntax."
The goal is:
Understand what the syntax represents and why Java behaves the way it does.
36. Connection to OOP#
Java is strongly object-oriented.
Later we will write:
class Student {
String name;
int age;
void study() {
System.out.println(name + " is studying");
}
}
Then:
Student s1 = new Student();
s1.name = "Rahul";
s1.age = 20;
s1.study();
This introduces several concepts:
class
object
state
behavior
reference
method
field
constructor
encapsulation
inheritance
polymorphism
abstraction
Do not rush into all of them now.
The OOP part of this course will explain them slowly and deeply.
37. What Java Tries to Give the Programmer#
At a high level, Java tries to provide a balance:
Java
│
┌───────────┼───────────┐
↓ ↓ ↓
Abstraction Safety Productivity
│ │ │
↓ ↓ ↓
Easier code Runtime Large
checks libraries
It gives programmers abstractions while still allowing high-performance applications.
This balance is one reason Java has remained important for large-scale software development.
38. A Complete Java Mental Map#
Keep this map for the rest of the course:
JAVA
│
┌─────────────┴─────────────┐
│ │
Java Language Java Platform
│ │
┌─────┼─────┐ ┌──────┼──────┐
│ │ │ │ │ │
Types Classes Methods JVM Libraries Tools
│ │
│ ↓
│ Bytecode
│ ↓
│ JVM
│ ↓
│ Operating System
│ ↓
└──────────────────────── CPU
This diagram is the foundation for everything that follows.
39. Chapter Summary#
Java is a high-level, general-purpose programming language.
Java was developed at Sun Microsystems and became publicly available in the 1990s.
Java source code is normally stored in .java files.
The Java compiler converts source code into bytecode.
.java
↓
javac
↓
.class
The JVM executes the bytecode.
.class
↓
JVM
↓
execution
The JDK is the main development kit used by Java programmers.
The JVM is the runtime machine that executes Java bytecode.
Java's bytecode + JVM architecture provides strong platform independence.
Java is strongly typed, object-oriented, garbage-collected, concurrent, and supported by a large standard library and ecosystem.
Modern JVMs use sophisticated runtime techniques such as JIT compilation, so Java should not be thought of simply as a slow interpreted language.
Most importantly:
Java Source
↓
Compiler
↓
Bytecode
↓
JVM
↓
Execution
Remember this pipeline.
It will appear again and again throughout your Java journey.
40. Check Your Understanding#
Try answering these without looking back.
Basic Questions#
- What is Java?
- Why do we need programming languages?
- What is bytecode?
- What is a JVM?
- What does JVM stand for?
- What does
javacdo? - What does the
javacommand do? - What is a
.javafile? - What is a
.classfile? - What is the JDK?
Conceptual Questions#
- Why doesn't Java normally compile source code directly into a platform-specific executable?
- How does the JVM help Java achieve portability?
- What does "Write Once, Run Anywhere" mean?
- Is Java compiled or interpreted?
- Why is the answer to the previous question more complicated than simply saying "compiled" or "interpreted"?
- Why is Java called a high-level language?
- What is automatic memory management?
- What is garbage collection?
- Why is Java considered strongly typed?
- Why can Java programs still have security vulnerabilities even though Java has security-related features?
JDK/JRE/JVM Questions#
- Difference between JDK and JVM?
- What is the conceptual role of the JRE?
- Which one would a Java developer normally install: JDK or JVM?
- Can the JVM compile
.javasource files directly? - What happens after
javac Main.java?
Execution Questions#
- What happens when you execute:
javac Main.java
- What happens when you execute:
java Main
- Draw the complete Java execution pipeline from source code to CPU.
41. Small Practice Task#
Create a file called:
Main.java
Write:
public class Main {
public static void main(String[] args) {
System.out.println("I am learning Java");
}
}
Compile it:
javac Main.java
Run it:
java Main
Expected output:
I am learning Java
Then check the directory.
You should normally see:
Main.java
Main.class
The important observation is:
Main.java
↓
source code
Main.class
↓
compiled bytecode
42. Think About This#
Before moving to the next chapter, make sure you can mentally explain this:
Why can't the CPU simply execute my Java source code?
A good answer should contain the idea that:
CPU
↓
understands machine instructions
Java source
↓
human-readable high-level language
javac
↓
converts Java source to bytecode
JVM
↓
loads and executes bytecode using the underlying platform
If you understand this, you have the foundation for understanding the Java platform.
43. One Final Picture#
If you remember only one diagram from this chapter, remember this:
┌──────────────────────────┐
│ Java Source Code │
│ Main.java │
└────────────┬─────────────┘
│
│ javac
↓
┌──────────────────────────┐
│ Java Bytecode │
│ Main.class │
└────────────┬─────────────┘
│
↓
┌──────────────────────────┐
│ JVM │
│ │
│ Class Loading │
│ Bytecode Execution │
│ JIT Compilation │
│ Memory Management │
│ Garbage Collection │
│ Thread Support │
└────────────┬─────────────┘
│
↓
┌──────────────────────────┐
│ Operating System │
└────────────┬─────────────┘
│
↓
┌──────────────────────────┐
│ CPU │
└──────────────────────────┘
Once this model is clear, many later Java concepts become much easier.
Next Chapter#
Chapter 2 — Installing Java and Writing Your First Java Program
We will go from installing the JDK to compiling and running a Java program, while also explaining:
- JDK installation
- JDK versions
javacommandjavaccommand- PATH
JAVA_HOME- Java source files
- Classes
main()System.out.println()- command-line execution
- IDEs vs terminal
- common setup errors
- the complete compilation/execution process