Welcome to the world of Java programming! Whether you want to build mobile apps, web applications, or high-performance software, Java is one of the best programming languages to start your developer journey.
1. What is Java?
The Core Concept
Java is a high-level, class-based, object-oriented programming language. It was created to follow a powerful philosophy: “Write Once, Run Anywhere” (WORA).
What Does “Write Once, Run Anywhere” Mean?
In traditional programming languages like C or C++, code compiled on a Windows computer can usually only run on Windows. If you want it to run on macOS or Linux, you have to recompile or rewrite parts of it.
Java solves this problem. When you write Java code, it is compiled into an intermediate format called Bytecode. This bytecode doesn’t run directly on your operating system – it runs on a virtual machine (the Java Virtual Machine, or JVM). Because the JVM is available for almost every operating system, your Java code runs seamlessly across different platforms without any modifications.
2. History and Key Features of Java
A Brief History
-
Created By: James Gosling and his team (known as the Green Team) at Sun Microsystems.
-
Launch Year: Released in 1995.
-
Original Name: Initially named Oak (after an oak tree outside Gosling’s office), but later renamed to Java (inspired by Java coffee).
-
Current Ownership: Acquired by Oracle Corporation in 2010.
Key Features of Java
A. Platform Independent
As discussed in WORA, Java bytecode executes inside the JVM, making the language hardware and operating-system independent.
B. Object-Oriented Programming (OOP)
Everything in Java is organized around objects (real-world entities) and classes (blueprints). This modular structure makes software easier to maintain, scale, and reuse.
C. Simple and Clean Syntax
Java removed complex, error-prone features found in earlier languages (like explicit memory pointers and operator overloading), making it easier to learn and write reliably.
D. Secure
Java programs run inside a restricted sandbox environment provided by the JVM. It also lacks direct memory pointers, preventing unauthorized access to system memory.
E. Robust and Automatic Memory Management
Java manages memory automatically using an inbuilt system called the Garbage Collector (GC). The Garbage Collector automatically destroys unused objects to free up system memory and prevent memory leaks.
F. Multithreaded
Java allows you to execute multiple tasks or threads simultaneously within a single program—essential for building interactive apps and games.
3. Java Installation & JDK Setup
To write and run Java applications on your computer, you need to set up the Java Development Kit (JDK).
Step-by-Step Installation Guide
Step 1: Download the JDK
- Go to the official Oracle Java download page or use an open-source distribution like Eclipse Temurin (Adoptium) or Amazon Corretto.
- Download the latest LTS (Long Term Support) version of the JDK (e.g., JDK 17 or JDK 21) for your operating system (Windows, macOS, or Linux).
Step 2: Install the JDK
-
Run the downloaded installer file.
-
Follow the on-screen installation wizard (use the default installation path).
Step 3: Configure Environment Variables (Windows)
To run Java commands from any command prompt or terminal window, set the system environment path:
- Open System Properties → Advanced System Settings → Environment Variables.
- Under System Variables, click New… and create:
- Variable name:
JAVA_HOME - Variable value: Path to your JDK folder (e.g.,
C:\Program Files\Java\jdk-21).
- Variable name:
- Find the existing
Pathvariable under System Variables, click Edit, and add:- %JAVA_HOME%\bin
Step 4: Verify the Installation
Open your Terminal (macOS/Linux) or Command Prompt (Windows) and type:
java -version
javac -version
If the installation was successful, both commands will display the installed version number.
4. Writing Your First Java Program
Let’s write, compile, and run the classic “Hello, World!” program.
Step 1: Write the Code
Create a new file named FirstProgram.java using any text editor (like VS Code, Notepad, or IntelliJ IDEA) and enter the following code:
public class FirstProgram {
public static void main(String[] args) {
System.out.println(“Hello, World! Welcome to Java Programming.”);
}
}
Line-by-Line Breakdown of the Code
-
public class FirstProgram
-
public: An access modifier making this class accessible anywhere. -
class: The keyword used to define a class in Java. -
FirstProgram: The name of the class. Rule: The class name MUST exactly match the filename (FirstProgram.java).
-
-
public static void main(String[] args)
-
This is the entry point of every Java application. Execution always starts here.
-
public: Allows the JVM to execute this method from anywhere. -
static: Allows the JVM to invoke the method without creating an instance/object of the class first. -
void: Indicates that this method does not return any value. -
main: The predefined method name that the JVM looks for when starting the application. -
String[] args: An array of strings used to pass command-line arguments to the program.
-
-
System.out.println("...");-
System: A built-in class providing access to system resources. -
out: An output stream connected to the console. -
println(): A method that prints the specified text to the screen and moves the cursor to a new line.
-
Step 2: Compile and Run the Program
Open your terminal in the directory where your FirstProgram.java file is saved:
- Compile the code:
javac FirstProgram.java
What happens: The Java compiler (javac) checks for syntax errors and generates a compiled bytecode file named FirstProgram.class.
2. Run the program:
java FirstProgram
What happens: The JVM loads the bytecode file and prints the output:
Hello, World! Welcome to Java Programming.
Â
5. Understanding JVM, JRE, and JDK
Beginners often confuse JDK, JRE, and JVM. Here is a clear breakdown of how they relate to each other:
Â
1. JVM (Java Virtual Machine)
-
What it is: The execution engine that runs Java bytecode.
-
Role: Reads
.classfiles, translates bytecode into machine code for your computer’s specific CPU, and executes it line by line. -
Key Detail: JVM is platform-dependent (there are different JVM builds for Windows, macOS, and Linux).
Â
2. JRE (Java Runtime Environment)
-
What it is: The software package that provides the runtime environment to execute existing Java applications.
-
Formula: JRE = JVM + Core Class Libraries + Runtime Files
-
Key Detail: Used by end users who only want to run Java apps, not write code.
Â
3. JDK (Java Development Kit)
-
What it is: The complete software development environment required to develop Java applications.
-
Formula: JDK = JRE + Development Tools such as
javaccompiler, debuggers,javadoc). -
Key Detail: Used by developers to write, compile, and run Java software.
Â