What Is Java
Think about what happens when you book a train ticket on your phone. The app sends your request to a server. The server checks which seats are free, calculates the fare, takes the payment and sends back your ticket. Thousands of people are doing the same thing at the same moment, and nobody should be charged twice.
Software like this has to be fast, reliable and easy to maintain for many years. Java has been a popular choice for exactly this kind of work since the late 1990s. That's why banks, IT services companies, product startups and government projects all hire Java developers.
Java also teaches you ideas that you'll use in every other language you pick up later: types, classes and objects, memory, and how a program actually runs.
Java Is a Programming Language
A programming language is a set of words and rules for writing instructions. A computer only understands machine code, which is long sequences of 0s and 1s. So we write in a language that people can read, and a tool translates it for the computer.
Java's rules are strict, in a helpful way. Every value has a type, such as a whole number, some text, or true/false. The compiler checks your code before it runs, so many mistakes are caught on your own computer instead of in front of a user.
Java is also object-oriented. You organize a program into classes and objects that model real things, such as a Ticket, a Passenger or a Payment. You'll learn this properly in Level 2.
Java Is Also a Platform
When people say "Java", they usually mean more than the language. They mean the language together with the tools that come with it:
| Part | What it does |
|---|---|
| The language | The rules you write your code in |
The compiler (javac) | Turns your code into bytecode |
| The JVM (Java Virtual Machine) | Runs bytecode on a particular computer |
| The standard library | Thousands of ready-made classes for text, dates, files, networking and more |
You install all of these together as the JDK (Java Development Kit). The lesson "Installing the JDK" shows you how to install it, and "Difference Between JDK, JRE and JVM" explains each part in detail.
Write Once, Run Anywhere
This is the idea that made Java famous: compile once, run on any computer that has a JVM.
- You write your program in a
.javafile. - The compiler,
javac, turns it into bytecode and saves it in a.classfile. Bytecode isn't machine code for any one processor. It's a set of simple instructions designed for the JVM. - Every operating system has its own JVM. The JVM for Windows, the JVM for macOS and the JVM for Linux all understand the same bytecode.
- So the same
.classfile runs on all of them and prints the same output.
The JVM changes from computer to computer, but your program doesn't. That's why Java's famous slogan is write once, run anywhere.
A First Look at Java Code
This program works out the total fare for a group train ticket.
public class TicketFare {
public static void main(String[] args) {
int farePerPerson = 450;
int passengers = 3;
int totalFare = farePerPerson * passengers;
System.out.println("Passengers: " + passengers);
System.out.println("Total fare: Rs. " + totalFare);
}
}Passengers: 3
Total fare: Rs. 1350How it works
- Line 1 creates a class called
TicketFare. In Java, all code lives inside a class, and the file name must match the class name. - Line 2 is the
mainmethod. This is where the program starts running. - Lines 3 and 4 store two whole numbers (
intmeans integer): the fare for one person and the number of passengers. - Line 5 multiplies them and stores the answer in
totalFare. - Lines 7 and 8 print the results. The
+joins the text and the number into one line.
You don't need to remember all of this yet. Every part has its own lesson later in the course.
Try it yourself: press Try it to open the code in the editor. Change passengers from 3 to 5. Before you run it, work out what the total should be. You should get Total fare: Rs. 2250.
Common Mistakes
Mixing up Java and JavaScript. The names are similar, but they are two different languages. JavaScript mainly runs inside web browsers. It got its name in 1995 partly for marketing, because Java was popular at the time. Knowing one doesn't mean you know the other.
Thinking the JVM is the same on every computer. Your bytecode is the same everywhere, but the JVM isn't: each operating system needs its own JVM. "Platform independent" describes your compiled program, not the JVM.
Expecting a .java file to run as it is. A computer can't run the text in a .java file directly. It has to be compiled into bytecode first. Since Java 11, the java command can compile and run a single file in one step, which you'll use while setting up Java (see "Running a Single Source File Directly").
Interview Corner
What is Java?
Java is a programming language you can use for almost any kind of software, and it is built around classes and objects. Your code is compiled into bytecode, and the Java Virtual Machine (JVM) runs that bytecode. Because every operating system has its own JVM, the same compiled program works on all of them.
Why is Java called platform independent?
Because the compiler produces bytecode instead of machine code for one particular processor. Every operating system has its own JVM that runs this bytecode, so one .class file runs on Windows, macOS and Linux unchanged. The JVM is platform dependent; the bytecode is not.
Is Java compiled or interpreted?
Both. javac compiles source code into bytecode before the program runs. When the program runs, the JVM first interprets the bytecode, and its JIT (just-in-time) compiler turns the code that runs most often into fast machine code.
Practice
- Easy: In two or three sentences, explain "write once, run anywhere" to a friend who doesn't know programming.
- Harder: Open
TicketFarein the editor and add one line so that the program also prints the fare for one person. The output should be:
Passengers: 3
Fare per person: Rs. 450
Total fare: Rs. 1350Show a hint
Add System.out.println("Fare per person: Rs. " + farePerPerson); between the two existing println lines.
Key Takeaways
Was this tutorial helpful?