What Java is
Java is a compiled, statically typed, object-oriented language: every variable has a declared type, every piece of code lives inside a class, and the compiler refuses to build a program it cannot make sense of. The compiled program runs on the Java Virtual Machine, which is why the same file runs unchanged on Windows, macOS and Linux. Writing it feels deliberate and explicit — there is more to type than in Python, but the structure the language insists on is the structure large programs need anyway.
Where Java is used
- Enterprise and financial back ends
- Banks, insurers, retailers and public bodies run a great deal of Java on the server, much of it built with the Spring framework, because the language stays stable across decades and the tooling for large teams is mature.
- Android apps
- Android's application framework was built around Java. New Android development is now Kotlin-first, but Java remains fully supported and a large share of existing Android code and documentation is Java.
- Data infrastructure
- Many of the systems that move and store data at scale — search engines, message queues, distributed databases — are written in Java or run on the JVM.
- Developer tooling and desktop software
- Build tools, IDEs and a good deal of internal business software are Java, and the same runtime underlies other JVM languages such as Kotlin and Scala.
Your first Java program
Saved as Main.java. You can paste it straight into the playground to see it run.
public class Main {
public static void main(String[] args) {
String name = "Asha";
int[] scores = {72, 88, 95};
int total = scores[0] + scores[1] + scores[2];
System.out.println(name + " took " + scores.length + " tests.");
System.out.println("Average: " + total / scores.length);
}
}What it prints
Asha took 3 tests.
Average: 85- Line 1 declares a public class called
Main. In Java every line of code lives inside a class, and a public class must sit in a file with the same name, so this file is Main.java. - Line 2 declares the
mainmethod, where the program starts.public static void main(String[] args)has to be written exactly like this; each word will make sense later, and for now you type it as given. - Line 3 declares a variable of type
Stringand stores the text "Asha" in it. The type comes first, then the name, and the statement ends with a semicolon. - Line 4 creates an array of three
intvalues. The square brackets afterintsay it is an array, and the braces list the contents. Line 5 adds them up — positions start at 0, so the last isscores[2]— into anintcalledtotal. - Line 6 prints a line.
System.out.printlnis the standard way to write to the terminal,+joins text with values, andscores.lengthis 3. - Line 7 prints the average. Both
totalandscores.lengthare integers, so/does whole-number division: 255 / 3 is 85, and 256 / 3 would also print 85 rather than 85.33. Lines 8 and 9 close the method and the class.
Run Java on your own computer
Java is a two-step language: javac compiles your source into bytecode, and java runs it. You need a JDK (Java Development Kit), which contains both; a JRE on its own can only run programs, not build them.
Install a JDK
Download a current long-term-support release — Java 21 or Java 25 — either as Oracle's build from oracle.com/java or as an OpenJDK build from jdk.java.net or your operating system's package manager. Choose the JDK, not the JRE, and let the installer add it to your PATH.
Check both tools
Then run java -version as well. Both should report the same version. If javac is missing, you installed a runtime rather than the development kit.
Shelljavac -versionCompile the file
Save the program as Main.java — the name must match the class — and compile it. Success is silent and produces Main.class in the same folder; errors are printed with the file name and line number.
Shelljavac Main.javaRun it
Give java the class name, not the file name. Typing java Main.class fails with Error: Could not find or load main class Main.class.
Shelljava MainOr run the source directly
Since Java 11 the java launcher can compile and run a single source file in one step, which is convenient for small experiments. For anything with more than one file, go back to javac.
Shelljava Main.javaUse an editor with Java support
Java is far easier with an editor or IDE that understands it: it will fill in the class and method boilerplate, flag errors as you type and manage the compile step for you. The major ones all have free editions.
A learning order for Java
Stages, not a timetable. Each one exists because the next would not make sense without it, and how long each takes depends on how much you write.
Stage 1. Structure, types and output
- the Main class and main method
- System.out.println
- int, double, boolean and char
- String
- arithmetic and integer division
- compiling and running
Java asks for more ceremony than most languages before it prints anything. Getting the compile-and-run loop working, and understanding why types are declared, removes the first and largest hurdle.
Stage 2. Control flow
- if / else and switch
- comparison and logical operators
- for and while loops
- the enhanced for loop
- break and continue
- Scanner for keyboard input
Once a program can decide and repeat, you can write real exercises. Reading input with Scanner belongs here because it is what makes those exercises interactive.
Stage 3. Methods and arrays
- static methods and return types
- parameters and overloading
- arrays and indexing
- the String and Math classes
- reading compiler errors
Methods are how Java code is split into named, testable pieces, and arrays are its most basic collection. Learning to read a javac error message properly here saves hours later.
Stage 4. Classes and objects
- fields and constructors
- instance methods and this
- static versus instance
- encapsulation and access modifiers
- toString, equals and hashCode
- records
This is the heart of Java. Modelling an account or a player as a class, with its data and behaviour together, is what the language was designed for, and records make the simple cases short.
Stage 5. Inheritance, interfaces and collections
- extends and super
- abstract classes and interfaces
- polymorphism
- ArrayList and HashMap
- generics
- enums
Interfaces and the collections framework are what let Java programs grow without becoming tangled. Generics arrive naturally here, because every collection uses them.
Stage 6. Exceptions, files and beyond
- checked and unchecked exceptions
- try / catch / finally
- reading and writing files
- packages and imports
- a build tool (Maven or Gradle) and JUnit tests
- a project of your own
Real programs fail and need to be built by a tool rather than by hand. With exceptions, a build tool and tests, you have the shape of a professional Java project.
Mistakes beginners make in Java
- The file name does not match the class name
- error: class Main is public, should be declared in a file named Main.java is the compiler insisting that a public class and its file share a name, capital letters included. Rename one to match the other.
- Comparing strings with ==
- "hello" == input compiles and sometimes even works, which makes it worse: == asks whether two variables point at the same object, not whether the text is the same. Use a.equals(b) for strings (or a.equalsIgnoreCase(b)), and keep == for numbers and booleans.
- Expecting a decimal from integer division
- 7 / 2 is 3 in Java, because both sides are int and the result is truncated. Make one side a double (7 / 2.0) to get 3.5. The related message error: incompatible types: possible lossy conversion from double to int appears when you try to store a double in an int variable without an explicit cast.
- Using a variable that was never given an object
- Exception in thread "main" java.lang.NullPointerException means a method was called on a reference that holds null — often a field that was declared but never assigned in the constructor. Recent versions describe the problem in the message, for example Cannot invoke "String.length()" because "name" is null, which usually points straight at the fix.
- Wrong capitalisation
- Java is case-sensitive. string name and system.out.println both produce error: cannot find symbol, because the compiler is looking for something with exactly that spelling. Class names start with a capital letter; keywords and primitive types such as int and boolean do not.
Strengths and trade-offs
Where it is strong
- A strict compiler that catches type errors, missing returns and unhandled checked exceptions before anything runs, which helps while learning and while maintaining.
- Write once, run anywhere still holds in practice: the same compiled program runs on any machine with a JVM, and the JVM itself is fast and thoroughly tested.
- Backwards compatibility and long-term-support releases mean code and knowledge age slowly; a Java program written years ago usually still builds.
- Deep tooling: the debuggers, profilers, build tools and IDEs available for Java are among the most capable for any language, and most are free.
Where it is not
- Verbose. The first program needs a class and a fully spelt-out method signature, and everyday code involves more declarations than Python, JavaScript or Kotlin need.
- The compile step and JVM start-up make small scripts and quick experiments slower to iterate on than an interpreted language.
- Memory use and start-up time are higher than for natively compiled languages such as C++, Go or Rust, which matters for small command-line tools and some cloud workloads.
- It does not run in the browser, and on Android the recommended language for new apps is now Kotlin, so Java's territory is mainly the server and existing codebases.
Who Java is for
Java is the right first language if you want a strict, explicit language that teaches types and structure from day one, if your course or workplace uses it, or if your aim is large server-side systems. It is also a sound next language for anyone coming from Python or JavaScript who wants to see what a compiler and static types buy you. It is a poor fit for quick scripts, data analysis, browser front ends or low-level systems work; Python, JavaScript and C++ each serve one of those better. For Android, Java still gives you a working base, but expect to write Kotlin.
Questions about learning Java
- Is Java the same as JavaScript?
- No. They share four letters and little else of importance. Java is compiled, statically typed and runs on the JVM; JavaScript is run by browsers, dynamically typed, and was named to ride on Java's reputation in the 1990s. Learning one does not teach you the other.
- Is Java free to use?
- Yes. The JDK is open source under the OpenJDK project, and free builds are available from Oracle at jdk.java.net and from several other vendors. Oracle also sells paid support subscriptions for businesses, but nothing about learning or shipping Java requires paying.
- Which version of Java should I learn?
- A current long-term-support release, which in 2026 means Java 21 or Java 25. Everything in the roadmap above works the same on both. Be wary of tutorials written for Java 8 or earlier: the language has gained records, switch expressions, text blocks and var since then, and modern code uses them.
- Do I really have to write public static void main every time?
- For the classic form, yes, and it is what almost all existing code and documentation shows. Java 25 finalised a shorter form in which a small program can be written as void main() without a class declaration, printing with IO.println. It is fine to use on Java 25 or newer, but learn the full form too, because you will read far more of it than you write.
The primary source
When this guide and the official documentation disagree, the documentation is right and we would like to hear about it.