Why this topic matters
Every non-trivial Java program — whether it is a Spring Boot web service, an Android app, a Hadoop data pipeline, or a desktop utility — uses the same basic syntax you learn in this topic. Variables, types, operators, conditions, loops, methods, arrays, and strings are the atoms from which the rest of the language is built. A reader who is shaky on these atoms will struggle to read even simple production code; a reader who is fluent in them can read almost any Java file with comprehension.
The basics are also where the most common bugs live. Off-by-one errors in loops, integer overflow in arithmetic, accidental fall-through in switch statements, misusing == on String references — every one of these traps claims thousands of engineering hours per year across the industry. We spend the most time on these pitfalls in the basics tutorials because the cost of getting them wrong compounds as the codebase grows.
Finally, the basics are the most stable part of Java. The syntax you learn today will work unchanged in Java 25, Java 30, and beyond. Newer Java features — records, sealed classes, virtual threads — are additions, not replacements. Time invested in the basics is the safest investment you can make in your Java education.
What you will learn
The Basics topic covers the Java syntax, primitive and reference types, how to declare and use variables, how to express logic with operators and conditions, how to repeat work with loops, how to package reusable logic into methods, how to store fixed-size data in arrays, and how to work with the immutable String class that every Java program uses. Once these are comfortable, the rest of the language is mostly a matter of learning additional APIs and conventions — the syntax stays the same.
Each tutorial in this topic is roughly eight to twelve minutes of reading plus a five-minute exercise. You can read them in any order, but the order below is what we recommend because each tutorial builds on the previous one. By the end of the topic you should be able to read a small Java program from top to bottom and explain what every line does, and you should be able to write a small command-line utility from scratch.
If you are new to Java, work through the tutorials below in order. Each one is about eight minutes of reading plus a five-minute exercise. If you already know another programming language, you can skim the first four tutorials and slow down at Loops.
Core concepts
The basic vocabulary you will pick up in this topic. Each term is covered in detail in its own tutorial.
- Statement — a single action terminated by a semicolon, like
int x = 5;. - Block — a group of statements enclosed in curly braces
{ ... }. - Variable — a named memory location that holds a value of a specific type.
- Primitive type — one of the eight built-in non-object types:
byte,short,int,long,float,double,char,boolean. - Reference type — any class, interface, array, or record. References point to objects on the heap.
- Operator — a symbol that performs an operation on one or more values.
- Control flow — the order in which statements execute, controlled by
if,switch,while,for. - Method — a named, reusable block of code that may take parameters and return a value.
- Array — a fixed-length, indexed sequence of values of the same type.
- String — an immutable sequence of characters, used everywhere in Java.
Common pitfalls
The five bugs we see most often in beginners' code. Each is covered in detail in the relevant tutorial.
- Integer division truncation.
int avg = total / count;rounds toward zero, even if the result is mathematically non-integer. Cast one operand todoubleif you need a fractional result. - Off-by-one in loops.
for (int i = 0; i <= arr.length; i++)throwsArrayIndexOutOfBoundsExceptionon the last iteration. Use<with array lengths, not<=. - String comparison with
==.if (input == "yes")compares references, not content. Useif (input.equals("yes"))or, for null-safety,if ("yes".equals(input)). - Switch fall-through. Forgetting
breakin aswitchcase causes the next case to execute. Use the newcase L -> ...arrow form introduced in Java 14 to avoid this entirely. - Modifying while iterating. Calling
list.remove(x)inside a for-each loop throwsConcurrentModificationException. Use the iterator's ownremove()method or theremoveIfmethod.
Best practices
Adopting these from the start will save you hours of debugging later.
- Name variables in lowerCamelCase. Constants in UPPER_SNAKE_CASE. Class names in UpperCamelCase.
- Mark local variables
finalby default; removefinalonly when you actually need to reassign. Many teams adopt this as a style rule. - Use the enhanced for-each loop
for (var x : list)whenever you do not need the index. - Always specify the encoding when reading or writing text. UTF-8 is the right default.
- Prefer
StringBuilderover+for string concatenation inside loops.
Frequently asked questions
How long does it take to learn the basics?
For a complete beginner, about 10 to 20 hours of focused study. For a developer who already knows another language, 3 to 5 hours — the concepts are familiar, only the syntax is new. We recommend spreading the learning across at least a week so the material has time to consolidate overnight.
Which Java version should I install to follow these tutorials?
Install a current Long-Term-Support version: Java 17 or Java 21. Both are production-grade, both work with every tutorial on this site, and both have multiple free OpenJDK distributions (we recommend Eclipse Temurin, Amazon Corretto, or Microsoft Build of OpenJDK). Avoid old versions like Java 8 unless you must support a legacy codebase.
Do I need to memorise every keyword?
No. You will naturally memorise the keywords you use daily — class, public, static, void, int, if, return — within a few hours of writing code. The rarer keywords (strictfp, transient, native) are easy to look up in the reference page when you encounter them. Focus on understanding, not memorisation.
Should I learn generics at the same time as the basics?
No. Generics are an intermediate topic that builds on classes and interfaces. You will encounter them naturally when you start using collections — List<String> and Map<String, Integer> use generics — but you do not need to understand them deeply until the intermediate track. The basics tutorials use generics sparingly and explain them when they appear.
Tutorials in this topic
The ten tutorials below are listed in the recommended reading order. Each tutorial links to the next at the bottom of the page, so you can read them sequentially without coming back here.
- Java Introduction — install the JDK and run your first program.
- Java Syntax — tokens, identifiers, comments, statements, blocks.
- Variables — declaration, scope,
final, instance fields. - Data Types — the eight primitives, wrappers, autoboxing.
- Operators — arithmetic, relational, logical, bitwise, ternary.
- Conditions —
if,switch, pattern matching. - Loops —
while,do-while,for,for-each. - Methods — declaration, parameters, overloading, varargs, recursion.
- Arrays — fixed-length indexed collections, the
Arraysutility. - Strings — immutability,
StringBuilder, text blocks.