Complete Core Java Tutorial — All Topics with Examples
A full end-to-end Core Java tutorial. Each section is self-contained with runnable examples.
📚 Table of Contents
- Basics & Syntax
- Variables, Data Types & Wrappers
- Operators
- Control Flow
- Arrays & Varargs
- Strings
- OOP — Classes & Objects
- OOP — 4 Pillars
- Keywords (static, final, this, super, instanceof)
- Access Modifiers
- Packages & Imports
- Object Class Methods
- Inner Classes
- Enums
- Interfaces (Advanced) & Functional Interfaces
- Abstract Classes
- Exceptions
- Generics
- Collections Framework
- Comparable & Comparator
- Iterator
- Lambda Expressions
- Streams API
- Optional
- Date & Time API
- Regular Expressions
- File I/O & NIO
- Serialization
- Multithreading Basics
- Concurrency Utilities
- Annotations
- Reflection
- JVM, Memory & Garbage Collection
- Modern Java (var, records, sealed, text blocks)
- Interview Essentials
1. Basics & Syntax
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, Java!"); } } - Class name = filename (for public class)
-
main= JVM entry point - Compile:
javac HelloWorld.java→ Run:java HelloWorld
2. Variables, Data Types & Wrappers
public class DataTypes { public static void main(String[] args) { // Primitives byte b = 127; // 1 byte short s = 32000; // 2 bytes int i = 100_000; // 4 bytes long l = 8_000_000_000L; // 8 bytes float f = 3.14f; // 4 bytes double d = 3.14159; // 8 bytes char c = 'A'; // 2 bytes boolean flag = true; // 1 bit // Wrappers Integer boxed = 42; // autoboxing int unboxed = boxed; // unboxing Integer parsed = Integer.parseInt("123"); Double dbl = Double.valueOf("3.14"); // Constants final double PI = 3.14159; } } Wrapper classes: Byte, Short, Integer, Long, Float, Double, Character, Boolean
3. Operators
public class Operators { public static void main(String[] args) { int a = 10, b = 3; // Arithmetic System.out.println(a + b); // 13 System.out.println(a / b); // 3 System.out.println(a % b); // 1 // Relational System.out.println(a > b); // true System.out.println(a == b); // false // Logical System.out.println(a > 5 && b < 5); // true System.out.println(a > 5 || b > 5); // true System.out.println(!(a > 5)); // false // Bitwise System.out.println(5 & 3); // 1 System.out.println(5 | 3); // 7 System.out.println(5 ^ 3); // 6 System.out.println(~5); // -6 System.out.println(5 << 1); // 10 System.out.println(5 >> 1); // 2 // Increment int x = 5; System.out.println(x++); // 5, x=6 System.out.println(++x); // 7 // Ternary String r = (a > b) ? "a" : "b"; } } 4. Control Flow
public class ControlFlow { public static void main(String[] args) { int score = 85; // if-else if (score >= 90) System.out.println("A"); else if (score >= 80) System.out.println("B"); else System.out.println("C"); // switch (traditional) int day = 2; switch (day) { case 1: System.out.println("Mon"); break; case 2: System.out.println("Tue"); break; default: System.out.println("Other"); } // switch expression (Java 14+) String type = switch (day) { case 1, 7 -> "Weekend"; case 2, 3, 4, 5, 6 -> "Weekday"; default -> "Invalid"; }; // for for (int i = 1; i <= 3; i++) System.out.print(i + " "); // while int n = 3; while (n > 0) { System.out.print(n-- + " "); } // do-while (runs at least once) int k = 0; do { System.out.print(k++ + " "); } while (k < 3); // for-each int[] arr = {1, 2, 3}; for (int v : arr) System.out.print(v + " "); // break / continue for (int i = 0; i < 5; i++) { if (i == 2) continue; // skip if (i == 4) break; // exit System.out.print(i + " "); } } } 5. Arrays & Varargs
import java.util.Arrays; public class ArrayDemo { // Varargs static int sum(int... nums) { int total = 0; for (int n : nums) total += n; return total; } public static void main(String[] args) { // 1D int[] arr = {5, 2, 8, 1}; Arrays.sort(arr); System.out.println(Arrays.toString(arr)); // 2D int[][] matrix = {{1, 2}, {3, 4}}; for (int[] row : matrix) for (int v : row) System.out.print(v + " "); // Jagged array int[][] jagged = new int[3][]; jagged[0] = new int[]{1}; jagged[1] = new int[]{2, 3}; // Varargs System.out.println(sum(1, 2, 3, 4)); // 10 } } 6. Strings
public class StringDemo { public static void main(String[] args) { String s = "Hello World"; // Common methods System.out.println(s.length()); // 11 System.out.println(s.charAt(0)); // H System.out.println(s.substring(0, 5)); // Hello System.out.println(s.toUpperCase()); // HELLO WORLD System.out.println(s.toLowerCase()); // hello world System.out.println(s.contains("World")); // true System.out.println(s.startsWith("He")); // true System.out.println(s.endsWith("ld")); // true System.out.println(s.indexOf("o")); // 4 System.out.println(s.replace("World", "Java")); // Hello Java System.out.println(s.trim()); System.out.println(s.split(" ")[0]); // Hello System.out.println(s.equals("Hello World")); // true System.out.println(s.equalsIgnoreCase("HELLO WORLD")); // true System.out.println(" ".isBlank()); // true System.out.println(String.join("-", "a", "b")); // a-b // String immutability String a = "abc"; a.concat("def"); // "abcdef" discarded System.out.println(a); // abc // StringBuilder (fast, mutable) StringBuilder sb = new StringBuilder(); for (int i = 0; i < 5; i++) sb.append(i).append(","); System.out.println(sb); // 0,1,2,3,4, // StringBuffer (thread-safe) StringBuffer sbf = new StringBuffer("Hi"); sbf.append(" there"); // String pool String x = "hi"; String y = "hi"; System.out.println(x == y); // true (pool) String z = new String("hi"); System.out.println(x == z); // false (new object) System.out.println(x.equals(z)); // true } } Rule: Use StringBuilder in loops, String for constants, StringBuffer for threads.
7. OOP — Classes & Objects
class Student { // Fields (encapsulation) private String name; private int age; // Static field private static int totalStudents = 0; // Constructor public Student(String name, int age) { this.name = name; this.age = age; totalStudents++; } // Copy constructor public Student(Student other) { this(other.name, other.age); } // Getters / Setters public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } // Instance method public void display() { System.out.println(name + ", " + age); } // Static method public static int getTotalStudents() { return totalStudents; } } public class OOPDemo { public static void main(String[] args) { Student s1 = new Student("Alice", 20); Student s2 = new Student(s1); s1.display(); System.out.println("Total: " + Student.getTotalStudents()); } } 8. OOP — 4 Pillars
8.1 Encapsulation
class Account { private double balance; public double getBalance() { return balance; } public void deposit(double amt) { if (amt > 0) balance += amt; } } 8.2 Inheritance
class Animal { void sound() { System.out.println("Some sound"); } } class Dog extends Animal { @Override void sound() { System.out.println("Woof"); } void fetch() { System.out.println("Fetching"); } } 8.3 Polymorphism
// Compile-time (overloading) class MathUtil { int add(int a, int b) { return a + b; } double add(double a, double b) { return a + b; } int add(int a, int b, int c) { return a + b + c; } } // Runtime (overriding) public class Poly { public static void main(String[] args) { Animal a = new Dog(); // upcasting a.sound(); // Woof (runtime) } } 8.4 Abstraction
abstract class Shape { abstract double area(); void info() { System.out.println("Area = " + area()); } } class Circle extends Shape { double r; Circle(double r) { this.r = r; } @Override double area() { return Math.PI * r * r; } } 9. Keywords
public class Keywords { static int staticVar = 10; // shared final int FINAL_VAR = 100; // constant String name = "A"; String name2 = name; // this.name equivalent static { System.out.println("Static block"); } // runs once Keywords() { // this() — call another constructor this("default"); } Keywords(String name) { this.name = name; // this — refers to current object } void show() { System.out.println(this.name); // this keyword } public static void main(String[] args) { Object o = "hello"; if (o instanceof String s) { // pattern matching System.out.println(s.length()); } } } class Base Source:https://dev.to/vivek1030/crash-course-core-java-1p0b PreviousThe $30M Question Nobody's Answering RightNextRoute Cloudflare Worker issue alerts to an agent without polling
Related posts
DeepSeek's Open-Source "Super-Agent" Codes Without Being Asked
0dev.to GitHub
All-in-one platform or a stack you assemble? Where do coaches and educators land?
0dev.to Discuss
I Wanted to Live on Coruscant. PAT Scared Me. Now My Alexa Lives on Mute.
0dev.to Discuss
Detect What You Can Name: PaliGemma 2 Object Detection with Transformers
0dev.to Machine Learning
When Should an LLM Router Stop Being Sticky? llm-d 1.0 Cache-Affinity Routing, Break-Even Math, and a Simulation
0dev.to Machine Learning

Understanding HackerTube: Karpathy’s Two-Hour Experiment in Better Recommendations
0dev.to Machine Learning