Learn Java Programming
Master Java from basics to advanced topics with concise lessons and practical examples. Follow a structured path inspired by reputable learning resources.
Reference guide: Programiz — Getting Started with Java [0]
Arrays in Java
Arrays store a fixed-size, ordered sequence of elements of the same type. They provide fast indexed access (O(1)) but cannot grow or shrink after creation. For dynamic sizing, use ArrayList.
Declaring and Initializing Arrays
// Declare and initialize with values
int[] numbers = {10, 20, 30, 40, 50};
String[] fruits = {"Apple", "Banana", "Cherry"};
// Declare with size (elements get default values)
int[] scores = new int[5]; // [0, 0, 0, 0, 0]
String[] names = new String[3]; // [null, null, null]
boolean[] flags = new boolean[4]; // [false, false, false, false]
// Access and modify elements (zero-indexed)
numbers[0] = 100; // change first element
System.out.println(numbers[0]); // 100
System.out.println(numbers[4]); // 50
System.out.println(numbers.length); // 5
// ArrayIndexOutOfBoundsException if index >= length or < 0
// numbers[5] = 60; // RUNTIME ERROR!
Iterating Arrays
int[] nums = {5, 10, 15, 20, 25};
// 1. Traditional for loop (when you need the index)
for (int i = 0; i < nums.length; i++) {
System.out.println("Index " + i + ": " + nums[i]);
}
// 2. Enhanced for-each (preferred for simple iteration)
for (int n : nums) {
System.out.println(n);
}
// 3. While loop
int i = 0;
while (i < nums.length) {
System.out.println(nums[i]);
i++;
}
// 4. Streams (Java 8+)
import java.util.Arrays;
Arrays.stream(nums).forEach(System.out::println);
Multi-dimensional Arrays
// 2D array (matrix)
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Access: matrix[row][col]
System.out.println(matrix[0][0]); // 1
System.out.println(matrix[1][2]); // 6
System.out.println(matrix[2][1]); // 8
// Iterate 2D array
for (int row = 0; row < matrix.length; row++) {
for (int col = 0; col < matrix[row].length; col++) {
System.out.printf("%3d", matrix[row][col]);
}
System.out.println();
}
// Jagged array (rows with different lengths)
int[][] jagged = new int[3][];
jagged[0] = new int[]{1, 2};
jagged[1] = new int[]{3, 4, 5};
jagged[2] = new int[]{6};
Arrays Utility Class
import java.util.Arrays;
int[] data = {5, 2, 8, 1, 9, 3};
// Sort
Arrays.sort(data); // [1, 2, 3, 5, 8, 9]
// Binary search (array MUST be sorted first)
int index = Arrays.binarySearch(data, 5); // returns index of 5
// Fill
int[] zeros = new int[10];
Arrays.fill(zeros, 42); // all elements become 42
// Copy
int[] copy = Arrays.copyOf(data, data.length); // full copy
int[] partial = Arrays.copyOfRange(data, 1, 4); // [2, 3, 5]
// Compare
int[] a = {1, 2, 3};
int[] b = {1, 2, 3};
System.out.println(Arrays.equals(a, b)); // true (content comparison)
System.out.println(a == b); // false (reference comparison)
// Print (toString)
System.out.println(Arrays.toString(data)); // [1, 2, 3, 5, 8, 9]
System.out.println(Arrays.deepToString(matrix)); // [[1,2,3],[4,5,6],[7,8,9]]
// Convert to List
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
// Note: asList returns a fixed-size list backed by the array
Common Array Patterns
int[] numbers = {4, 7, 2, 9, 1, 5, 8};
// Find maximum value
int max = numbers[0];
for (int n : numbers) {
if (n > max) max = n;
}
System.out.println("Max: " + max); // 9
// Sum all elements
int sum = 0;
for (int n : numbers) {
sum += n;
}
System.out.println("Sum: " + sum); // 36
// Reverse an array in-place
int left = 0, right = numbers.length - 1;
while (left < right) {
int temp = numbers[left];
numbers[left] = numbers[right];
numbers[right] = temp;
left++;
right--;
}
// Check if array contains a value
boolean found = false;
for (int n : numbers) {
if (n == 9) { found = true; break; }
}
// Or with streams: Arrays.stream(numbers).anyMatch(n -> n == 9);
Key Takeaways
- Arrays have a fixed size set at creation; use
ArrayListfor dynamic sizing. - Arrays are zero-indexed: first element is
[0], last is[length-1]. - Use
Arrays.sort()for sorting,Arrays.equals()for content comparison (not==). - Prefer for-each for simple iteration; use indexed for when you need the position.
- Use
Arrays.toString()for printing; raw print shows a memory address. - Accessing out-of-bounds indices throws
ArrayIndexOutOfBoundsExceptionat runtime.
ArrayList<T> over raw arrays. ArrayLists grow dynamically, provide type safety with generics, and offer rich methods (add, remove, contains). Use arrays when performance is critical or when working with primitives in tight loops.Java Tutorial FAQs
How do I run Java online?
How do I install Java on Windows?
JAVA_HOME and update the Path variable to include the JDK bin directory. Verify with java --version. [0]How do I install Java on macOS?
export JAVA_HOME=$(/usr/libexec/java_home) and update PATH to include $JAVA_HOME/bin. Verify with java --version. [0]Do I need the JDK to compile Java?
javac) and tools needed to build Java applications. For quick tests, the online compiler is a convenient alternative.What is Java used for?
Learn Java the Practical Way
Whether you’re new to programming or expanding your skills, this Java tutorial focuses on hands-on learning. Each topic pairs clear explanations with short, working examples that you can run online. Move from fundamentals—variables, data types, and control flow—to core OOP concepts like classes, inheritance, and interfaces. Along the way, you’ll see idiomatic Java patterns and simple best practices that build confidence.
Why this guide? It’s designed for real-world use: fast to read, easy to try, and friendly on mobile. Bookmark it and return whenever you need a refresher or a quick example.
Java Programming Tutorial — Learn Java Step by Step
Java is one of the most widely-used programming languages in the world, powering Android apps, enterprise backends, cloud services, and scientific computing. This tutorial teaches Java from the ground up with practical, runnable examples you can try in our free Java Compiler or any local IDE.
Each topic includes multiple code examples with explanations, expected output, and best practices. Whether you are a complete beginner or refreshing your knowledge of a specific feature, every page gives you immediately usable code.
What This Tutorial Covers
- Basics: Hello World, variables, data types, operators
- Control Flow: if/else, switch, for, while, break/continue
- Methods: Parameters, return types, overloading, recursion
- Arrays & Strings: Declaration, manipulation, StringBuilder
- OOP: Classes, objects, constructors, encapsulation
- Inheritance: extends, super, abstract classes, final
- Interfaces: Abstraction, default methods, functional interfaces
- Collections: ArrayList, HashMap, HashSet, LinkedList
- Exceptions: try/catch/finally, custom exceptions
- Generics: Type parameters, bounded types, wildcards
- Streams (Java 8+): filter, map, reduce, collect
- File I/O: BufferedReader, Files API, serialisation
Why Learn Java in 2026?
- Enterprise dominance: Java powers most Fortune 500 backends via Spring Boot and Jakarta EE
- Android development: Java + Kotlin build apps for 3+ billion active Android devices
- Job market: Top 3 most-demanded language on LinkedIn, Indeed, and Stack Overflow survey
- Platform independence: Write once, run anywhere — bytecode runs on any JVM
- Modern evolution: Java 17/21 LTS added records, sealed classes, virtual threads, pattern matching
- Ecosystem: Maven/Gradle, IntelliJ IDEA, 100,000+ libraries, massive community
How to Get Started
- Run code online: Use our Java Compiler — no installation needed
- Install JDK locally: Download OpenJDK 17+ from adoptium.net
- Choose an IDE: IntelliJ IDEA Community (free), Eclipse, or VS Code with Java Extension Pack
- Follow topics in order: Start from Hello World and progress sequentially
- Build a project: After OOP, build a small CRUD app to solidify your knowledge
Frequently Asked Questions
No. This tutorial starts from absolute basics (Hello World, variables) and progresses to advanced topics. Each concept builds on the previous one.
Java 17 (LTS) is recommended. All examples in this tutorial work on Java 17+. Our online compiler uses the latest stable release.
Basics take 4–6 weeks of daily practice. Intermediate topics (collections, generics) add 4–6 more weeks. Job-readiness typically requires 3–6 months total.
Yes, completely free. No account, no sign-up. All topics and examples are available without any restriction.
Who Is This For?
Complete beginners choosing Java as their first language. CS students preparing for university courses and exams. Self-taught developers building a strong OOP foundation. Python/JS developers learning Java for backend or Android work. Interview candidates practising Java data structures. Professional developers needing a quick reference for specific Java features.