Learn Java programming from basics to advanced concepts including OOP, Collections, Multithreading, and more.
What Is Java? Java is a general-purpose, object-oriented programming language created at Sun Microsystems by James Gosling and his team in 1995. It was designed with one central ...
A Short History The Java project began in 1991 under the name Oak . It was originally created for consumer electronic devices like set-top boxes. When the internet became popular, ...
Real-World Uses of Java Java powers some of the largest systems in the world because it scales well and runs reliably for years. Enterprise applications: banking, insurance and ...
Three Layers, One Stack Java software is arranged in three related components that beginners often mix up. JVM (Java Virtual Machine) The JVM is a program that executes Java ...
Choosing a JDK The two most common free JDKs are Oracle JDK and OpenJDK . For most learners and projects OpenJDK is the recommended choice because it is fully open source. ...
Hello, World Save, Compile, Run Save the file as HelloWorld.java . The file name must match the public class name exactly. Compile with javac HelloWorld.java . This produces ...
From Source to Execution Many languages compile directly to machine code. Java adds a middle step, which is what gives it portability. Step 1: Compilation The compiler ( javac ) ...
The Skeleton of a Java File Naming Rules for Identifiers Identifiers are the names you give to classes, methods, variables and packages. May contain letters, digits, underscore ...
Why Comments Matter Comments are notes for humans that the compiler ignores. They explain why code exists, document tricky logic, and remind the next developer (often yourself) ...
What Is a Variable? A variable is a named box in memory that holds a value while the program runs. Every variable in Java has a declared type, which decides how much memory it ...
Why Conversion Matters When you mix values of different number types, Java must convert between them. Some conversions happen automatically; others need an explicit cast. Implicit ...
Operator Families Arithmetic Operators Unary Operators Relational and Logical Operators Note that && and || short-circuit: the second operand is only evaluated when needed. ...
Printing Output The format specifiers used in printf : %d integer, %f decimal number, %s string, %n new line. Reading Input with Scanner Common Scanner Methods nextLine() - reads ...
if and else The condition inside the parentheses must produce a boolean . If it is true the block runs, otherwise control moves to the next else if or else . Nested if switch ...
for Loop A loop repeats a block while a condition holds. The classic counting loop: The three parts are initialisation, condition and update: for (start; condition; step) . while ...
break break immediately exits the loop, skipping any remaining iterations. continue continue skips the rest of the current iteration and jumps to the next one. return Inside a ...
Why Methods Methods group statements into named, reusable units. They let you write logic once and call it from many places, which keeps programs shorter and easier to test. ...
What Is an Array? An array is a fixed-size container that stores many values of the same type at consecutive memory positions. Each slot is reached by an index starting at 0. ...
Strings Are Objects In Java, a String is not a primitive; it is a class that stores a sequence of characters. The class is immutable , meaning once a String object is created its ...
The OOP Mindset Procedural programming focuses on functions that operate on data. Object-oriented programming ties the data and the functions that act on it into a single unit ...
Anatomy of a Class Using the Class The this Keyword this refers to the current object. It is needed when a parameter name shadows a field name, as in the constructor above where ...
What Is a Constructor? A constructor is a special method that runs when an object is created with new . It initialises the new object's fields so it is usable immediately. Its ...
Why Hide Data? Encapsulation bundles the data of a class with the methods that operate on it, and keeps the data private . Outsiders cannot assign invalid values accidentally ...
What Inheritance Does Inheritance lets a new class ( subclass ) derive from an existing class ( superclass ) and automatically receive its fields and methods. You then add or ...
One Name, Many Behaviours Polymorphism means the same method name can express different behaviour depending on the context. Java has two flavours: compile-time and runtime. ...
Hiding the How Abstraction means exposing only what an object does, while keeping how it works hidden. In Java you achieve it with abstract classes and interfaces . Abstract ...
What Is a Package? A package is a folder-like namespace that groups related classes. It prevents name clashes (two classes called Utils in different packages can coexist) and ...
Belongs to the Class, Not an Object A static member belongs to the class itself and is shared by every object of that class. You may call it without creating an object. Static ...
Three Jobs of final The final keyword makes something unchangeable. Where it applies changes its meaning. Final Variables (Constants) Note: final on a reference type prevents ...
Dealing With the Unexpected An exception is an event that interrupts the normal flow of a program - like reading a file that was deleted or dividing by zero. Rather than letting ...
Doing Many Things at Once A thread is an independent path of execution inside a program. By splitting work across threads, a program can use more of the CPU and keep the user ...
The Shared Data Problem When two threads read and write the same variable at the same time, the value can become corrupted. Run this long enough and the total goes wrong. The ...
Data Structures, Ready-Made The Collections Framework in java.util provides ready-made implementations of the most useful data structures, so you do not have to write linked lists ...
Three List Families All three implement the List interface with the same behaviour, but they store data in very different ways, which changes performance. ArrayList Backed by a ...
Hash vs Tree Sets and Maps come in hash-based and tree-based variants. The hash versions offer near-instant lookups with no ordering; the tree versions keep everything sorted but ...
Code That Works for Many Types Generics let you write a class or method once and use it with different types safely. The famous example is collections: List<String> is a list of ...
Passing Behaviour as Data A lambda expression is a compact block of code you can pass around like a value. It is the modern, readable replacement for anonymous classes that ...
Declarative Data Processing Streams let you process collections with a pipeline of operations - filter, map, sort, collect - instead of writing loops with temporary variables. ...
Reading and Writing Text Files Text I/O is the most common file task. Modern Java offers Files and BufferedReader for clean, readable code. Reading a File Writing a File Exception ...
The Modern Time API java.time (Java 8 onward) fixes the old, confusing Date and Calendar classes. Its objects are immutable, so they are safe to share between threads. LocalDate, ...
Pattern Matching in Text A regular expression is a miniature language for describing text patterns. Java supports it through the classes Pattern and Matcher , and through ...
Java Talks to a Database JDBC (Java Database Connectivity) is the standard API for sending SQL statements from Java to a relational database. You need three things: the driver, ...
Who Cleans Up the Memory? In Java you never free memory yourself. Objects live on the heap ; a background process called the garbage collector (GC) finds objects that the program ...