Java: Introduction to Java
Java is one of the most popular programming languages in the world. This lesson gives you a complete overview and helps you set up your development environment.
1. What is Java
Java is a programming language created by Sun Microsystems in 1995, now maintained by Oracle. Its core design principle is "Write Once, Run Anywhere."
(1) Key Features of Java
| Feature | Description |
|---|---|
| Cross-platform | Write once, run anywhere (Windows/Mac/Linux) |
| Object-oriented | Supports encapsulation, inheritance, and polymorphism |
| Easy to learn | Syntax similar to C++, but without the complex features |
| Secure and reliable | Built-in memory management, exception handling, and strong type checking |
| Rich ecosystem | Massive collection of open-source libraries and frameworks (Spring, Hibernate, etc.) |
| Wide job market | Web development, Android, big data, enterprise applications |
(2) What Can You Build with Java
- Web development: Spring Boot, Servlet/JSP
- Android development: The primary language for Android apps
- Enterprise applications: Banking, e-commerce, ERP systems
- Big data: Hadoop, Spark, Flink
- Game development: Minecraft is written in Java
- IoT: Embedded device development
2. JDK, JRE, and JVM
These three concepts are core to Java. Understanding their relationship is important.
(1) Concept Explanation
| Concept | Full Name | Purpose |
|---|---|---|
| JVM | Java Virtual Machine | The virtual machine that runs Java bytecode |
| JRE | Java Runtime Environment | JVM + core libraries (for running Java programs) |
| JDK | Java Development Kit | JRE + development tools (compiling, debugging, etc.) |
(2) Relationship Diagram
JDK (Development Kit)
├── JRE (Runtime Environment)
│ ├── JVM (Virtual Machine)
│ └── Core Libraries
└── Development Tools (javac, java, jshell, etc.)
3. Environment Setup
(1) Download the JDK
JDK 17 or 21 is recommended (LTS - Long Term Support versions).
Download at: https://www.oracle.com/java/technologies/downloads/
(2) Verify Installation
After installation, open a terminal and run these commands to verify:
# Check Java version
java -version
# Check compiler version
javac -version
Expected output:
java version "17.0.9" 2023-10-17 LTS
Java(TM) SE Runtime Environment (build 17.0.9+11-LTS-201)
Java HotSpot(TM) 64-bit Server VM (build 17.0.9+11-LTS-201, mixed mode, sharing)
4. Getting Started with JShell
JShell is an interactive tool introduced in Java 9. It lets you run Java code directly in the terminal, making it perfect for learning and experimentation.
(1) Launch JShell
jshell
Output:
| Welcome to JShell Version 17.0.9
| For an introduction type: /help intro
jshell>
(2) Basic Operations
// Arithmetic
jshell> 3 + 4
$1 ==> 7
// Define a variable
jshell> int age = 25
age ==> 25
// Print output
jshell> System.out.println("Hello, Java!")
Hello, Java!
// Exit
jshell> /exit
| Goodbye
5. Why Learn Java
| Reason | Description |
|---|---|
| High demand | Java is the go-to language for enterprise development |
| Competitive salaries | Java developers earn above-average salaries |
| Mature ecosystem | Massive collection of open-source frameworks and tools |
| Abundant learning resources | Active community, comprehensive documentation |
| Cross-platform capability | Write once, run anywhere |
| Long-term stability | Continuously updated since 1995 |
▶ Example
public class HelloJava {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}
❓ FAQ
📖 Summary
- Java is a cross-platform, object-oriented programming language with wide application areas
- JDK is for development, JRE is for running, and JVM is the core of Java's cross-platform capability
- JShell lets you execute Java code interactively, perfect for learning
- JDK 17 or 21 (LTS versions) are recommended for installation
📝 Exercises
- Setup: Install the JDK on your computer and verify that the java and javac commands work correctly
- JShell practice: Use JShell to calculate: How many seconds are in a day? How many seconds are in a year?
- Think about it: List 3 project ideas you'd like to build with Java
6. Next Lesson
In the next lesson, we'll learn about Your First Java Program — writing and running your first Java program.