Chapter 1: Introduction to Java Programming
Welcome to the first chapter of your Java programming
journey. In this chapter, we'll explore the fundamentals of Java, its purpose,
architecture, and how to get started writing your first program.
What is Java?
Java is a platform-independent, object-oriented
programming language designed to build secure, scalable, and portable
applications.
Why Was Java Invented?
Java was invented to:
- Achieve
platform independence
- Develop
internet-based business applications
- Build
both:
- Stand-alone
Applications (SA) using Core Java
- Internet
Applications (IA) using Core + Advanced Java
Who Invented Java?
Java was invented by James Gosling at Sun
Microsystems in June 1991.
The goal was to create secure, network-based business applications.
Types of Applications
Java helps in building two main types of applications:
- Stand-alone
Applications (SA):
- Installed
on a single computer
- Examples:
Calculator, MS Office, Antivirus, Games
- Internet
Applications (IA):
- Installed
on a server, accessed via internet
- Examples: Amazon, Facebook, Gmail, YouTube
What is a Platform?
A platform is the environment where a program is
loaded, executed, and produces output.
- It
includes both hardware (RAM, processor) and software (OS, JVM,
etc.)
- Examples
of platforms:
- Java
→ JVM (Java Virtual Machine)
- Python
→ PVM (Python Virtual Machine)
- .NET
→ CLR (Common Language Runtime)
- HTML
→ Browser
Platform Dependency vs. Platform Independence
|
Type |
Meaning |
|
Platform Dependent |
Program runs only on the OS it was compiled for (e.g., C,
C++) |
|
Platform Independent |
Program can run on any OS using intermediate software
(e.g., Java, Python) |
Java achieves platform independence using JVM, allowing the same compiled code (bytecode) to run on any OS — a principle called WORA (Write Once, Run Anywhere).
Why C and C++ Are Platform Dependent?
Why C/C++ program cannot execute in different OS?
-
The C and C++ program is compiled targeting one particular OS in which we are compiling this program.
-
Hence the C and C++ program compiled code contains directly Machine Language of a particular OS whose format is not understandable to different OS.
-
Hence C and C++ program cannot be executed in different OS. Then C and C++ programs are called PD (Platform Dependent).
How Java Achieved Platform Independence?
To achieve PI (Platform Independence), unlike in C and C++, in Java language, the program is not compiled targeting to OS and in source code also we do not write any statement specific to one OS.
-
In Java, compiled code does not contain ML (Machine Language), instead it contains an intermediate code called byte code. It is JVM-understandable code, not understandable to humans or the OS.
-
While execution, JVM converts the Java compiled code (bytecode) into current OS-specific ML and executes it in a Platform Independent way.
To achieve PI, compared to C, in Java:
-
ML generation is moved from compilation phase to execution phase for generating ML for the client OS in which Java program is being executed, by introducing new compiler software, bytecode, and JVM.
-
The new compiler software converts Java source code into byte code.
-
The byte code is the Java compiled code which is not native to any OS or processor. This is native code to JVM.
-
JVM is the Java platform. It is a translation software responsible to execute Java byte code by converting it into current OS ML.
-
To run a Java program we must have JVM installed in our system.
-
Because we have JVM for every OS to run the same compiled code in all OS, Java program becomes platform independent.
Means Java program supports WORA (Write Once Run Anywhere).
- In the Java programming language, all source code is first written in plain text files ending with the “.java” extension.
- Those source files are then compiled into “.class” files by the
javaccompiler.
- A “.class” file does not contain code that is native to your processor; it instead contains bytecodes — the native language of the Java Virtual Machine.
- The
javalauncher tool then runs your application with an instance of the Java Virtual Machine.
Important facts on Java program and Java software
-
Java Slogan: WORA – Write Once Run Anywhere
-
Compiler is responsible to take Java source code, verify syntaxes, if not errors, convert source into bytecode and save this bytecode in a separate new file with ".class" extension with class name as file name.
-
JVM is responsible to take Java bytecode, execute this bytecode by converting it into current OS ML. After execution, generated ML is destroyed.
-
Java supports only two extension files: .java and .class
-
In Java, we do not have a file with machine language code. Java does not support .exe file because it is platform dependent, not safe, easily affected by viruses, and mostly compiled code is downloaded from the server. Hence ML code saved in client system is useless.
-
Java compiler is OS independent because both input and output files (source code and bytecode) are Java-related files.
-
JVM is OS dependent because it must generate OS-dependent ML from the given bytecode.
-
Java software contains:
-
Compiler → PI
-
JVM → PD
-
API → PI / PD
-
-
Java software is platform dependent because JVM is platform dependent. Therefore, separate Java software is needed for every OS. Based on our system OS and processor, we must download and install Java software.
-
Java program is platform independent and Java software is platform dependent, both because of JVM.
-
Java program is platform independent, because JVM is available for every OS, so Java program can be executed in all OS.
-
Java software is platform dependent, because we have separate JVM for every OS, so the same Java software cannot be installed on all OS.
-
-
For all OS, JVM is developed by SUN (Oracle), not by OS vendors.
-
JVM will not be installed inbuilt along with OS installation.
-
We must install JVM explicitly in our computer after OS installation.
Programmer is responsible for developing, compiling, and executing a Java program, whereas
-
Customer/Client is responsible for only executing Java program.
-
Based on programmer and customer responsibilities, Java software is divided into two types.
-
Java Software Types
Java consists of multiple components. Most importantly:
- JDK
(Java Development Kit) – includes compiler and JVM
- JRE
(Java Runtime Environment) – runtime only (removed in Java 11)
- JVM
(Java Virtual Machine) – executes bytecode
- JIT
(Just-In-Time Compiler) – improves runtime performance
From Java 11 onwards, JRE is removed — JDK
includes all required tools.
How to Install JDK?
Steps:
- Search
for “Download Latest JDK” on your browser.
- Go
to Java Downloads and select the Windows tab.
- Download
jdk.exe.
- Install
in C:\jdk-22\ (or your preferred directory).
- Set
this as JAVA_HOME.
- Test
installation using command prompt:
- javac
-version → shows compiler version
- java
-version → shows JVM version
Software Required for Java Development
|
Purpose |
Software |
|
Writing code |
Notepad, VS Code, Eclipse, IntelliJ, etc. |
|
Compiling and Running |
JDK (includes Compiler and JVM) |
|
Terminal |
Command Prompt |
Java Programming Elements
Java supports 8 core programming elements:
|
Element |
Purpose |
|
Module |
Grouping packages |
|
Package |
Grouping classes |
|
Class |
Representing real-world objects |
|
Variables |
Storing data |
|
Methods |
Defining behavior or logic |
|
Constructors |
Initializing values |
|
Blocks |
Defining execution blocks |
|
Inner Classes |
Creating nested object behavior |
Essential Java Statements
To write and run a Java program, you must include:
- Class
declaration
- Main
method
- System.out.println();
for printing output
Java Program Structure
class ClassName {
public static void
main(String[] args) {
System.out.println("Message");
}
}
Steps to Create and Run a Java Program
1. Write the code:
Use Notepad or any editor to write Java code and save as Example.java.
2. Compile using Command Prompt:
javac Example.java
3. Run the Program:
java ClassName
Example Program 1: Print a Message
class FirstProgram {
public static void
main(String[] args) {
System.out.println("QAWorld!");
}
}
Command Line Steps:
D:
cd D:\FSJD\01CJ\01JB
javac Example.java
java FirstProgram
Output:
QAWorld!
Compilation and Execution Workflow
- Compilation
(javac): Converts .java to .class (bytecode)
- Execution
(java): JVM runs the bytecode from .class file
javac Example.java
→ FirstProgram.class
java FirstProgram
→ Output on console
Example Program 2: Print Your Name
class MyName {
public static void
main(String[] args) {
System.out.println("Your Name");
}
}
Speaking Language vs Programming Languages
| Speaking Language | Programming Languages |
|---|---|
| 1. Letters (26) | → 1. Programming Elements (8) |
| 2. Words (n) | → 2. Reserved Keywords (71) |
| 3. Sentences (n) | → 3. Statements (19) |
| 4. Grammar (20) | → 4. Syntax (19) |
| 5. Paragraph (n) | → 5. Logic (method) (2) |
| 6. Page (n) | → 6. Program (class) (n) |
| 7. Text Book (n) | → 7. Project (n) |
1. Programming Elements
Java supports 8 programming elements.
They are:
-
Module – A group of packages
-
Package – A group of classes
-
Class – A group of variables and methods
-
Variables – Memory for storing values
-
Methods – An operation that contains logic
-
Constructors – A kind of method used for storing values
-
Blocks – A kind of method used for storing values
-
Inner Classes – A class created inside another class for grouping sub-variables and methods
Written by: QAWorld Team



