Java Programming Complete Tutorial
Beginner to Advanced

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:

HelloWorld.java
// 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
Best Practice: Follow Java naming conventions: Class names in PascalCase, method/variable names in camelCase, constants in UPPER_CASE.

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
Choosing a Language:
  • 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

Using JDK (Recommended):
  1. Download JDK from Oracle website
  2. Set JAVA_HOME environment variable
  3. Add Java to PATH: %JAVA_HOME%\bin
  4. Verify: java -version and javac -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

1-7
Week 1: Java Fundamentals

Syntax, Data Types, Operators, Basic I/O

8-14
Week 2: Control Flow & Methods

Conditionals, Loops, Methods, Arrays

15-21
Week 3: Object-Oriented Programming

Classes, Objects, Inheritance, Polymorphism

22-30
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.

Variables Example
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
byte1-128 to 1270
short2-32,768 to 32,7670
int4-231 to 231-10
long8-263 to 263-10L
float4±3.4e-38 to ±3.4e380.0f
double8±1.7e-308 to ±1.7e3080.0d
char20 to 65,535'\u0000'
boolean1 bittrue/falsefalse
Variable Naming Rules:
  • Cannot start with a digit
  • Can contain letters, digits, underscore (_), dollar sign ($)
  • Cannot be Java keywords
  • Case-sensitive
  • Use meaningful names (camelCase for variables)