# How to learn Java from scratch

Java is a strictly typed language behind banking systems, the servers of large services and Android apps. The strictness slows you down at first, but the compiler catches many mistakes before the program ever runs. This plan takes about six months at an hour a day and leads from your first program to a small backend application.

## What to install

You need a JDK — the development kit that contains the `javac` compiler and the
runtime. Take the current LTS release: OpenJDK builds are free (Eclipse Temurin,
for example). For an editor, use IntelliJ IDEA Community Edition: it is free and
flags errors right in the editor as you type.

Build your first program by hand once, without the IDE, so you see what actually
happens:

```java
public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello, Java");
    }
}
```

`javac Hello.java` turns the source into the bytecode file `Hello.class`, and
`java Hello` runs it. Recent JDKs can also run a single-file program directly:
`java Hello.java`.

## Weeks 1–6: the language without objects

Primitive types (`int`, `long`, `double`, `boolean`, `char`) and how they differ
from reference types, strings and why they are immutable, operators, `if`,
`switch`, loops, arrays, static methods. Pay attention to integer division and
overflow: `7 / 2` in Java gives `3`, and the sum of two large `int` values can
turn negative. Good tasks for this stage: converting between number bases,
finding prime numbers, working with an array of grades.

## Weeks 7–12: object-oriented programming

Classes and objects, fields and constructors, encapsulation, inheritance,
interfaces and abstract classes, polymorphism. Work through `equals`, `hashCode`
and `toString` — many beginners trip over them. To make it stick, model a small
domain: a library with books, readers and loans, or bank accounts with transfers
and a transaction history. Learn exceptions at the same time: checked and
unchecked, and your own exception classes.

## Weeks 13–18: the standard library and tools

* Collections: `ArrayList`, `HashMap`, `HashSet`, `LinkedList` — when to use
  which and how fast their operations are.
* Generics, lambdas and the Stream API.
* Building a project with Maven or Gradle: dependencies and the
  `src/main/java` / `src/test/java` layout.
* Unit tests with JUnit 5.
* Git: a branch per task and meaningful commits.

## Weeks 19–26: towards backend development

Talk to a database through JDBC, then get to know Spring Boot — the framework
most server-side Java is written with. The final project is a REST service, such
as a to-do list or a book tracker: a few endpoints, storage in PostgreSQL, tests
and a README that explains how to run it.

## Projects worth showing

* a console tic-tac-toe game with win detection and tests;
* a CSV parser that computes statistics and writes a report to a file;
* bank accounts with transfers and protection against going below zero;
* a Spring Boot REST service backed by a database.

## How to check yourself

Run the tests before every commit and keep them green. Turn on static analysis
in the IDE and deal with every warning instead of switching it off. Solve
problems on sites with automatic checking, such as Exercism or LeetCode,
choosing Java as your language. Every two weeks, reread your old code and
rewrite whatever you would now do differently — it is the clearest sign of
progress. If you can, post a project for code review in a Java community, for
example the Code Review site on Stack Exchange.

## Step-by-step plan

1. **Week 1 — JDK and IDE** — Install an LTS JDK and IntelliJ IDEA, compile your first program by hand with javac.
2. **Weeks 2–6 — language basics** — Types, operators, conditions, loops, arrays, static methods.
3. **Weeks 7–12 — OOP** — Classes, inheritance, interfaces, equals and hashCode, exceptions.
4. **Weeks 13–18 — library and tools** — Collections, generics, the Stream API, Maven or Gradle, JUnit 5, Git.
5. **Weeks 19–26 — a backend project** — JDBC, Spring Boot, a REST service with a database and tests.

## Check yourself

1. What does this Java code print: System.out.println(7 / 2);
   - 3
   - 3.5
   - 4
2. What does this Java code print: System.out.println("a" + 1 + 2);
   - a3
   - a12
   - compile error
3. What is the value of the expression 10 % 3 in Java?

## Sources

- [dev.java — Learn](https://dev.java/learn/) — Official Java learning material from Oracle
- [The Java Tutorials](https://docs.oracle.com/javase/tutorial/) — Oracle's classic tutorial on the language basics
- [JUnit](https://junit.org/) — User guide for unit testing in Java
- [Exercism — Java track](https://exercism.org/tracks/java) — Free exercises with automated tests and optional mentoring

---

https://skillok.org/articles/en/learn-java-from-scratch
