What is Java and How Does it Work?
Short answer: Java is a popular, object-oriented, general-purpose programming language. Java code is compiled into bytecode that runs on the Java Virtual Machine (JVM), which is why the same program can run on Windows, macOS and Linux without changes. It is widely used for enterprise back ends, Android apps, big data tools and large-scale systems.
How Java works
- You write source code in a
.javafile. - The Java compiler (
javac) converts it into bytecode (.classfiles). - The JVM loads the bytecode and runs it, translating it into machine instructions for the current computer. A Just-In-Time (JIT) compiler optimizes frequently used code at runtime.
This is the idea behind "write once, run anywhere": the bytecode is the same on every system; only the JVM differs.
JDK, JRE and JVM
| Term | Meaning |
|---|---|
| JVM | The virtual machine that executes bytecode |
| JRE | JVM plus the standard libraries needed to run programs |
| JDK | JRE plus development tools such as the compiler; this is what you install to write Java |
Your first program
public class Hello {
public static void main(String[] args) {
String name = "Java";
System.out.println("Hello, " + name + "!");
}
}
Save as Hello.java, then:
javac Hello.java
java Hello
The file name must match the public class name. main is where the program starts.
Key features
- Object-oriented: programs are built from classes and objects; see Java OOP concepts.
- Statically typed: types are checked at compile time, catching many errors early.
- Automatic memory management: the garbage collector frees unused objects.
- Platform independent through the JVM.
- Rich standard library including collections, I/O, networking and concurrency: collections framework.
- Strong tooling and ecosystem: Maven, Gradle, Spring, Hibernate, JUnit.
A small example with classes
public class Account {
private double balance;
public void deposit(double amount) {
if (amount <= 0) throw new IllegalArgumentException("Amount must be positive");
balance += amount;
}
public double getBalance() {
return balance;
}
}
Fields are made private and exposed through methods. This is called encapsulation.
What Java is used for
| Area | Examples |
|---|---|
| Enterprise back ends | Spring Boot services, banking and insurance systems |
| Android | Apps (alongside Kotlin) |
| Big data | Hadoop, Kafka, Spark ecosystem tools |
| Web servers | Tomcat, Jetty and microservices |
| Tools and games | IDEs and desktop software |
Pros and cons
| Pros | Cons |
|---|---|
| Stable, mature and widely used | More verbose than languages such as Python |
| Strong performance for large systems | Heavier startup and memory use |
| Excellent backward compatibility | More boilerplate for small scripts |
| Large job market | Steeper start than some beginner languages |
Java vs JavaScript
Despite the name, they are different languages. Java is compiled to bytecode and used mostly on servers and Android. JavaScript runs in browsers and, with Node.js, on servers. See what Node.js is.
Getting started
- Install a recent LTS version of the JDK.
- Use an IDE such as IntelliJ IDEA, Eclipse or VS Code with Java extensions.
- Learn core syntax, then OOP, collections, exceptions and streams.
- Continue with exception handling and the Streams API.
FAQs
Is Java free?
Yes. OpenJDK is open source, and several vendors provide free builds. Check the license of the specific distribution for commercial support terms.
Is Java still worth learning?
Yes. It remains one of the most used languages in enterprises and Android development.
Is Java hard to learn?
Its strict structure takes time but teaches good programming habits, and there are plenty of learning resources.
What is the garbage collector?
A JVM component that automatically frees memory used by objects that are no longer reachable.
