1. What is Java?
Java is a widely used, high-level, object-oriented programming language that was created by James Gosling and his team at Sun Microsystems in 1995. Since then, it has grown into one of the most popular programming languages in the world, used for developing desktop applications, mobile apps (Android), enterprise systems, web applications, and even embedded systems.
One of Java’s biggest advantages is its platform independence. This is achieved through the concept of the Java Virtual Machine (JVM), which allows compiled Java code (bytecode) to run on any device or operating system without modification. This is why Java’s slogan is often: "Write Once, Run Anywhere" (WORA).
Some key characteristics of Java:
- Simple: Easy to learn if you have basic programming knowledge.
- Object-Oriented: Code is organized around objects, making it reusable and modular.
- Secure: Provides a safe execution environment with features like bytecode verification and automatic memory management.
- Robust: Handles errors effectively and avoids crashes.
- Portable: Code runs on different platforms without recompilation.
- Multithreaded: Can execute multiple tasks in parallel for better performance.
2. Understanding JDK, JRE, and JVM
Before we start writing Java programs, it’s important to understand the three key components:
2.1 JVM (Java Virtual Machine)
The JVM is a virtual machine that runs Java bytecode. When you compile your Java program, it gets converted into bytecode. The JVM reads and executes this bytecode. JVM is platform-dependent, but Java bytecode is platform-independent.
2.2 JRE (Java Runtime Environment)
The JRE contains the JVM and the standard Java class libraries required to run Java programs. If you only need to run Java applications (not develop them), installing the JRE is enough.
2.3 JDK (Java Development Kit)
The JDK includes the JRE, JVM, and development tools such as the Java compiler (javac
), debugger, and documentation generator (javadoc
). If you are developing Java programs, you must install the JDK.
3. Installing the JDK
Follow these steps to install Java on your system:
- Visit the official Java download page:
- Download the latest LTS (Long-Term Support) version, such as Java 17 or 21.
- Run the installer and follow the setup instructions.
- After installation, set the
JAVA_HOME
environment variable:- On Windows: System Properties → Advanced → Environment Variables.
- On macOS/Linux: Add
export JAVA_HOME=/path/to/jdk
in your.bashrc
or.zshrc
.
- Add
JAVA_HOME/bin
to your system PATH. - Verify installation:
java -version javac -version
4. Choosing and Setting Up an IDE
While you can write Java code in any plain text editor (like Notepad), using an IDE (Integrated Development Environment) will make your life easier. IDEs provide code suggestions, debugging tools, and error checking.
Popular Java IDEs:
- IntelliJ IDEA: Modern, powerful, widely used in industry.
- Eclipse: Open-source, good for enterprise projects.
- NetBeans: Easy to start with, official Oracle IDE.
- VS Code (with Java Extension Pack): Lightweight option for beginners.
Setting up IntelliJ IDEA as an example:
- Download IntelliJ IDEA Community Edition from JetBrains.
- Install and launch the IDE.
- Create a new Java project and select the installed JDK.
- Inside the
src
folder, create a new Java class file.
5. Your First Java Program: "Hello World"
Let's write the simplest Java program. This will display Hello, World! on the screen.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Step-by-Step Explanation
public class HelloWorld
: Declares a public class namedHelloWorld
. The filename must match the class name.public static void main(String[] args)
: This is the entry point of every Java application. The JVM calls this method to start the program.System.out.println("Hello, World!");
: Prints the text to the console and moves to a new line.
6. How to Compile and Run the Program
- Save the file as
HelloWorld.java
. - Open the terminal in the file location.
- Compile the program:
This generatesjavac HelloWorld.java
HelloWorld.class
(bytecode file). - Run the program:
java HelloWorld
7. Output
Hello, World!
8. Modifying the Program
Let’s change the message:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Welcome to Java Programming!");
}
}
9. Common Beginner Mistakes
- File name mismatch: The file name must be exactly the same as the public class name (case-sensitive).
- Missing semicolon: Each Java statement must end with a
;
. - Wrong capitalization: Java is case-sensitive;
System
andsystem
are different. - Forgetting quotes: Strings must be in double quotes (
" "
).
10. Understanding the Compilation Process
- You write source code in a
.java
file. - The compiler (
javac
) converts it into.class
bytecode. - The JVM executes the bytecode on your machine.
11. Extra Example: Multiple Print Statements
public class MultiPrint {
public static void main(String[] args) {
System.out.println("Line 1: Java is fun!");
System.out.println("Line 2: Let's learn together.");
System.out.println("Line 3: Keep practicing every day.");
}
}
12. Summary
In this lesson, you learned:
- What Java is and why it’s popular.
- The difference between JVM, JRE, and JDK.
- How to install Java and verify the installation.
- How to set up an IDE for Java development.
- How to write, compile, and run your first Java program.
- Common mistakes and how to avoid them.
Tip: Practice compiling and running small programs daily to get comfortable with Java syntax.