Introduction to Java
## Learning Objectives
- Understand Java history and platform
- Set up Java development environment
- Write and run your first Java program
- Understand Java syntax basics
## What is Java?
Java is a high-level, object-oriented programming language developed by Sun Microsystems (now Oracle).
```java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
```
## Java Platform
### Key Features
- **Write Once, Run Anywhere** - Java code compiles to bytecode
- **Object-Oriented** - Everything except primitives is an object
- **Strongly Typed** - Must declare variable types
- **Automatic Memory Management** - Garbage collection
### Platform Components
| Component | Description |
|-----------|-------------|
| JDK | Java Development Kit |
| JRE | Java Runtime Environment |
| JVM | Java Virtual Machine |
## Installing Java
### Download
Get JDK from or OpenJDK from
### Verify Installation
```bash
java --version
javac --version
```
## Your First Program
### File: HelloWorld.java
```java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
```
### Compile and Run
```bash
javac HelloWorld.java # Creates HelloWorld.class
java HelloWorld # Runs the program
```
## Java Program Structure
```java
public class ClassName { // Class declaration
public static void main(String[] args) { // Main method
// Statements go here
}
}
```
### Components
1. **Class**: Template for creating objects
2. **Main method**: Entry point of the program
3. **Statements**: Commands to execute
## Comments
```java
// Single-line comment
/*
* Multi-line comment
*/
/**
* Documentation comment (Javadoc)
*/
```
## The main Method
```java
public static void main(String[] args) {
// args: command-line arguments
for (String arg : args) {
System.out.println(arg);
}
}
```
## System Output
```java
// Print with newline
System.out.println("Hello");
// Print without newline
System.out.print("Hello ");
System.out.print("World");
// Print formatted (Java 15+)
System.out.printf("Name: %s, Age: %d%n", "Alice", 30);
```
## Command Line Arguments
### Hello.java
```java
public class Hello {
public static void main(String[] args) {
if (args.length > 0) {
System.out.println("Hello, " + args[0]);
} else {
System.out.println("Hello, World!");
}
}
}
```
```bash
javac Hello.java
java Hello Alice
# Output: Hello, Alice
```
## IDEs
### Popular IDEs
- IntelliJ IDEA (recommended)
- Eclipse
- VS Code with Java extension
- NetBeans
## Java Versions
```bash
java --version # Check installed version
```
| Version | Year | Feature |
|---------|------|---------|
| Java 8 | 2014 | Lambdas, Streams |
| Java 11 | 2018 | LTS, var |
| Java 17 | 2021 | LTS, Sealed Classes |
| Java 21 | 2023 | LTS, Virtual Threads |
## Summary
- Java is platform-independent through bytecode
- JDK includes compiler and runtime
- Every Java program has a class with main method
- `javac` compiles `.java` to `.class`
- `java` runs the bytecode
- Comments: `//`, `/* */`, `/** */`
Comments
Comments powered by Giscus
To enable comments, add your Giscus embed code here.
Learn more about Giscus →