Java Programming Language Tutorial
Master Java programming from basic syntax to advanced Object-Oriented Programming (OOP), Collections Framework, Multithreading, and modern Java features with practical examples.
Platform Independent
Write Once, Run Anywhere
OOP & Collections
Robust Framework
Enterprise Ready
Used in Enterprise Apps
1. Introduction to Java Programming
Java is a high-level, class-based, object-oriented programming language developed by Sun Microsystems (now owned by Oracle). It was designed to have as few implementation dependencies as possible, making it platform-independent with the "Write Once, Run Anywhere" (WORA) capability.
History of Java
- 1991: James Gosling starts "Oak" project
- 1995: Renamed to Java, first public release
- 1996: JDK 1.0 released
- 2004: J2SE 5.0 (major features added)
- 2014: Java 8 (Lambda expressions)
- 2017, 2018, 2021: Java 9, 11, 17 LTS releases
Why Learn Java?
- Platform Independent (WORA)
- Object-Oriented Programming
- Rich API and Libraries
- Used in Android Development
- Enterprise Applications
- Strong Memory Management
First Java Program
Every Java program must have a main() method. Here's the traditional "Hello, World!" program:
// Simple Java Program
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
2. Basic Syntax and Structure
Java programs are composed of classes, methods, variables, and statements following a specific syntax.
| Component | Description | Example |
|---|---|---|
| Class Declaration | Blueprint for creating objects | public class MyClass { } |
| Main Method | Entry point of Java program | public static void main(String[] args) |
| Variables | Named storage locations | int count = 10; |
| Methods | Blocks of code that perform tasks | void display() { } |
| Statements | Instructions that perform actions | System.out.println("Hello"); |
| Comments | Explanatory text ignored by compiler | // Single line comment |
3. Java vs C++ vs Python
| Feature | Java | C++ | Python |
|---|---|---|---|
| Type | Compiled & Interpreted | Compiled | Interpreted |
| Memory Management | Automatic (Garbage Collection) | Manual | Automatic |
| Performance | High | Very High | Moderate |
| Platform | Platform Independent | Platform Dependent | Platform Independent |
| Syntax | Verbose | Complex | Simple & Readable |
| Use Cases | Enterprise, Android | System, Game Dev | AI, Web, Scripting |
- Java: Enterprise applications, Android development, large-scale systems
- C++: System programming, game development, performance-critical applications
- Python: Data science, machine learning, web development, scripting
4. Running Java Programs
Windows Setup
- Download JDK from Oracle website
- Set JAVA_HOME environment variable
- Add Java to PATH:
%JAVA_HOME%\bin - Verify:
java -versionandjavac -version
Compile & Run (Windows)
# Compile Java file
javac HelloWorld.java
# Run compiled class
java HelloWorld
# With command line arguments
java HelloWorld arg1 arg2
Linux/Mac Setup
# Install OpenJDK
sudo apt install openjdk-17-jdk
# Set environment variables
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk
export PATH=$PATH:$JAVA_HOME/bin
# Compile and run
javac HelloWorld.java
java HelloWorld
30-Day Java Learning Roadmap
Week 1: Java Fundamentals
Syntax, Data Types, Operators, Basic I/O
Week 2: Control Flow & Methods
Conditionals, Loops, Methods, Arrays
Week 3: Object-Oriented Programming
Classes, Objects, Inheritance, Polymorphism
Week 4: Advanced Topics
Collections, Exception Handling, File I/O
5. Variables, Constants and Data Types
Java has two categories of data types: primitive types and reference types. Variables must be declared before use.
public class DataTypesExample {
public static void main(String[] args) {
// Primitive data types
int age = 25;
float salary = 45000.50f;
double pi = 3.1415926535;
char grade = 'A';
boolean isEmployed = true;
byte smallNumber = 100;
short mediumNumber = 32000;
long bigNumber = 1000000000L;
// Constants (use final keyword)
final double PI = 3.14159;
final int MAX_USERS = 1000;
// Reference types
String name = "John Doe";
// Display values
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
System.out.println("Grade: " + grade);
System.out.println("Is Employed: " + isEmployed);
System.out.println("Name: " + name);
System.out.println("PI Constant: " + PI);
}
}
| Data Type | Size (Bytes) | Range | Default Value |
|---|---|---|---|
byte | 1 | -128 to 127 | 0 |
short | 2 | -32,768 to 32,767 | 0 |
int | 4 | -231 to 231-1 | 0 |
long | 8 | -263 to 263-1 | 0L |
float | 4 | ±3.4e-38 to ±3.4e38 | 0.0f |
double | 8 | ±1.7e-308 to ±1.7e308 | 0.0d |
char | 2 | 0 to 65,535 | '\u0000' |
boolean | 1 bit | true/false | false |
- Cannot start with a digit
- Can contain letters, digits, underscore (_), dollar sign ($)
- Cannot be Java keywords
- Case-sensitive
- Use meaningful names (camelCase for variables)