₹20 at the chai stall
Evening, outside the college gate in Kharghar. Riya scans the QR code at the chai stall and pays ₹20. Before she can put her phone away, the shopkeeper's speaker announces: “₹20 received.”
Sir, that took two seconds! My phone, the bank and the shop's speaker are all different machines. Who writes the software that makes them talk?
Programmers do. A large share of banking and payment software is written in a language called Java. Its biggest trick is the answer to your question: one Java program can run on very different machines without being rewritten.
How? My laptop runs Windows, the server probably runs Linux. Don't they need different programs?
Normally, yes. Java gets around it with a translator called the JVM that lives on each machine. You write once, and every JVM understands it. Let's see exactly how.
What is Java?
Java is a high-level, object-oriented, general-purpose programming language. Java programs are compiled into platform-independent bytecode, which runs on any device that has a Java Virtual Machine (JVM).
Java is a language for giving instructions to a computer. You write the instructions once. Java turns them into a universal format (bytecode), and a translator called the JVM on each computer runs them. Windows, Mac, Linux, a bank server: the same program works on all of them.
Words you'll hear in every Java lesson
Java at a glance
| Created by | James Gosling and his team at Sun Microsystems |
|---|---|
| Released | Announced in 1995; JDK 1.0 released in January 1996 |
| Original name | Oak (renamed Java, after Java coffee) |
| Owned today by | Oracle (bought Sun Microsystems in 2010). The open-source version is OpenJDK. |
| Type of language | High-level, object-oriented, statically typed, compiled to bytecode and run by the JVM |
| File types | .java source code · .class bytecode · .jar a package of many class files |
| Release cycle | A new version every 6 months. Long-Term Support (LTS) versions every 2 years: Java 17, 21, 25 |
Exam tip: Java is called platform independent because its bytecode can run on any platform. The JVM itself is platform dependent: Windows, macOS and Linux each need their own JVM. Examiners love this trick question.
The Java family: 4 platforms and 4 types of applications
“Java” is not one product. It's a language plus a set of platforms, each tuned for a different kind of device.
The four Java platforms
Java SE: Standard Edition
The core language and libraries: strings, collections, files, threads. Everything in this course is Java SE.
Start hereJakarta EE (formerly Java EE)
Java SE plus tools for large web and enterprise systems: web servers, databases, transactions.
Banks, e-commerceJava ME: Micro Edition
A smaller Java for devices with little memory: old feature phones, set-top boxes, sensors.
Embedded devicesJava Card
A tiny Java that runs inside chips on SIM cards and bank cards to keep secrets safe.
Smart cardsThe four types of Java applications
| Type | What it is | Example |
|---|---|---|
| Standalone (desktop) | Installed and run on one computer | IntelliJ IDEA, Eclipse IDE |
| Web application | Runs on a server; you use it through a browser | Online booking and shopping sites |
| Enterprise application | Large, distributed, secure systems used by organisations | Core banking, insurance, HR systems |
| Mobile application | Runs on phones | Android apps (Java or Kotlin) |
From zero to your first running Java program
Here's what happens on your side and what happens inside the computer at each step.
Install a JDK
Download a free JDK such as Eclipse Temurin or Oracle JDK. Choose an LTS version (21 or 25). In a hurry? Skip this and use the live editor in Part 7.
The installer copiesjavac(the compiler),java(the launcher that starts the JVM) and thousands of ready-made library classes onto your disk.Check it worked
Your operating system searches the folders listed in thejava -version javac -versionPATHsetting for programs namedjavaandjavac, runs them, and they print their version.Write the program
Open any editor (VS Code, IntelliJ IDEA, even Notepad) and save this as
The editor saves plain text characters to your disk. To the computer it's just letters, not yet a program.Main.java. The file name must match the class name.Compile it
javac Main.javajavacreads the whole file, checks the grammar and the types, and writes a new fileMain.classcontaining bytecode. No message means success.Run it
The JVM starts, loadsjava MainNamaste from Java!Main.classinto memory, checks the bytecode is safe, finds themainmethod and runs it line by line.
Shortcut: since Java 11 you can run a single file directly with java Main.java. Java compiles it in memory and runs it straight away. It's handy for practice, but real projects still compile first.
Write once, run anywhere: both views
Press Play or step through with Next. The left side shows what you see on screen. The right side shows what's happening behind it at the same moment.
- Riya types her program in an editor and saves it as
Main.java. - She runs
javac Main.java. Nothing is printed, which means success, and a new file Main.class appears in the folder. - She copies the same Main.class to a Windows PC, a MacBook and a Linux server. She doesn't recompile anything.
- On each machine she types
java Main. All three print Namaste from Java! - Compare C: a program compiled into
hello.exeon Windows refuses to run on Linux. - That's Java's promise: Write Once, Run Anywhere.
- The file is just text stored as numbers: each letter has a code (p = 70, u = 75…). A CPU can't run text.
- javac checks grammar and types, then writes bytecode. Every .class file starts with the bytes
CA FE BA BE. The rest are JVM instructions likegetstaticandinvokevirtual. - The .class file contains no Windows-, Mac- or Linux-specific code, so it's identical on every machine. Each machine has its own JVM.
- Each JVM translates bytecode into its own CPU's machine code as the program runs. Its JIT compiler converts frequently used code ahead of time for speed.
- A C compiler produces machine code for one OS and CPU. Linux doesn't understand a Windows
.exefile, so the program must be rebuilt for Linux. - The full picture: .java → javac → .class → JVM → machine code. You write and compile once, and the JVM takes care of each platform.
Java vs C vs Python: how each one runs
| C | Java | Python | |
|---|---|---|---|
| You write | hello.c | Main.java | hello.py |
| Converted to | Machine code for one OS/CPU | Bytecode for the JVM | Bytecode for the Python interpreter (done automatically) |
| Runs on another OS? | Only after recompiling | Yes, the same .class file | Yes, if Python is installed |
| Speed | Very fast | Fast (JIT compiler) | Slower for heavy computation |
| Types checked | At compile time | At compile time | At run time |
Where Java runs in your daily life
You use Java-powered systems many times a day without knowing it. Here are seven places, and what Java does in each.
Watch it with an example: one UPI payment
You pay Rs 60 at the college canteen. Follow the payment and see where Java code is doing the work.
- Tap Pay: the UPI app on Riya's phone sends a payment request (who pays, who gets it, how much) over the internet to the bank.
- Check: on the bank's server a Java program checks the UPI PIN and the daily limit. If anything is wrong, it stops here and sends back an error.
- Debit: Java code reduces Riya's balance from 500 to 440. Banks write this kind of code very carefully, because money must never be lost or created.
- Credit: the same program adds 60 to the canteen's account.
- Confirm: the canteen's phone shows “Paid”. The JVM ran this Java program on the bank's server, not on either phone.
- Done: Riya's app shows “Sent”. Many banking and payment back-ends are written in Java because it is fast, safe and runs on any server. (Simplified: real UPI also passes through NPCI and both banks.)
1. Banking and UPI payments
When you pay by UPI or net banking, your request goes to bank servers. Many core banking and payment platforms are built in Java because it's stable, secure and handles thousands of transactions a second.
2. Android apps
Java was Android's original app language, and millions of apps are written in it. Today Android apps are written in Java or Kotlin, and both share the same Java ecosystem and libraries.
3. Ticket booking and online shopping
When lakhs of people book train tickets at the same minute, or a big sale opens, the servers must not crash or sell one seat twice. Java frameworks such as Spring Boot are widely used for these high-traffic backends.
4. Big data tools
The tools companies use to handle huge amounts of data, such as Apache Hadoop, Apache Kafka and Elasticsearch, are written mainly in Java, and Apache Spark runs on the JVM. Learning Java helps you understand these tools from the inside (they're in our Big Data track).
5. Games
Minecraft: Java Edition, one of the best-selling games ever, is written in Java. Its huge modding community exists partly because Java code is easy to extend.
6. Developer tools
Popular code editors such as IntelliJ IDEA and Eclipse are themselves Java applications. Programmers use Java to build tools for other programmers.
7. SIM cards and bank cards
The chip in many SIM cards and debit/credit cards runs Java Card, a tiny version of Java. It stores secret keys and proves the card is genuine when you tap or insert it.
Examples are simplified to show where Java fits. Each company's exact technology choices vary.
Your first Java program
Read the program and its line-by-line meaning first, then open the live editor and try each challenge.
public class Main {
public static void main(String[] args) {
System.out.println("Namaste from Java!");
}
}public class MainEvery Java program lives inside a class. Here the class is namedMain, so the file must beMain.java.public static void main(String[] args)The entry point: the JVM always starts running from this exact method. Lesson 1.6 explains each word.System.out.println(...)Prints text to the console and then moves to a new line (ln = line)."Namaste from Java!"A string: text inside double quotes.;and{ }Every statement ends with a semicolon. Curly braces group code into blocks.
What the JVM does at every line
Step through this slightly bigger program. Watch the highlighted line on the left and the memory on the right. The stack holds methods and their small variables, and the heap holds objects such as strings. Both are covered in depth in Module 8.
Worked examples: real problems, solved and traced in memory
Three tiny programs of the kind Java runs every day in shops, banks and phones. Each one uses only what you have seen so far: println, a few variables and arithmetic. Run them, then step through to see each value appear on main's stack.
Print a canteen bill
Problem statement
A college canteen wants its counter computer to print a simple bill: two items with prices, and the total. Write a Java program that stores the prices in variables, adds them, and prints the bill.
Masala dosa Rs 60, Filter coffee Rs 25
--- Canteen Bill --- Masala dosa Rs 60 Filter coffee Rs 25 Total Rs 85
Approach
- Store each price in an
intvariable. - Add them into a
totalvariable. - Print each line with
System.out.println, joining text and numbers with+.
Solution: run it live
What happens in memory, step by step
Recorded from the real JVM: press Next to execute one line at a time. Watch method calls push and pop frames on the stack, and objects (numbered #1, #2 …) appear on the heap.
How many steps are left today?
Problem statement
A fitness app sets a daily goal of 8,000 steps. The phone has counted 5,340 so far. Print the steps walked, the steps remaining and the percentage completed.
goal 8000, walked 5340
Walked: 5340 steps Remaining: 2660 steps Done: 66%
Approach
- Store the goal and the steps walked.
- Remaining = goal − walked.
- Percentage = walked × 100 / goal (whole-number division).
Solution: run it live
What happens in memory, step by step
Recorded from the real JVM: press Next to execute one line at a time. Watch method calls push and pop frames on the stack, and objects (numbered #1, #2 …) appear on the heap.
How many SMS parts will this message need?
Problem statement
An SMS can carry 160 characters. A bank wants to know how many characters its OTP message has, and how many 160-character parts it needs.
message: "Your OTP for login is 482913. Do not share it with anyone. Valid for 5 minutes."
Characters: 79 SMS parts needed: 1
Approach
- Store the message in a
String. message.length()gives the number of characters.- Parts = (length + 159) / 160 rounds up using whole-number division.
Solution: run it live
What happens in memory, step by step
Recorded from the real JVM: press Next to execute one line at a time. Watch method calls push and pop frames on the stack, and objects (numbered #1, #2 …) appear on the heap.
Check yourself
Quick revision
Lesson 1.1 in one screen
- Java: a high-level, object-oriented language by James Gosling, Sun Microsystems, 1995. Owned by Oracle; OpenJDK is the open-source version.
- Flow:
.java→javac→.class(bytecode) →JVM→ machine code. - WORA: bytecode is platform independent. The JVM is platform dependent.
- 4 platforms: SE (core), Jakarta EE (enterprise), ME (small devices), Java Card (chips).
- 4 application types: standalone, web, enterprise, mobile.
- Used in: banking and payments, Android, booking and e-commerce backends, big data (Hadoop, Kafka), games, IDEs, smart cards.
- Commands:
javac Main.javacompiles,java Mainruns.
Interview and viva questions
Is Java a compiled or an interpreted language?
Both. Java source code is compiled by javac into bytecode. At run time, the JVM interprets the bytecode and its JIT compiler compiles frequently used parts into native machine code for speed. So Java is best described as “compiled to bytecode, then interpreted and JIT-compiled by the JVM”.
Why is Java called platform independent?
Because the compiled bytecode (.class) does not depend on any operating system or CPU. Any machine with a JVM can run it. The JVM itself is platform dependent: there's a different JVM for each OS.
What is the difference between Java and JavaScript?
They are completely different languages that share part of a name for historical marketing reasons. Java is statically typed and runs on the JVM, mainly for backends, Android and big data. JavaScript is dynamically typed and runs mainly in web browsers and on Node.js.
Is Java free to use?
Yes. OpenJDK is free and open source, and free builds such as Eclipse Temurin are widely used. Oracle's own JDK has its own licence terms, so companies check those before using it in production.
Is Java still worth learning in 2026?
Yes. Java is consistently among the most-used languages in industry, especially for banking, enterprise backends (Spring Boot), Android and big data. It also teaches object-oriented thinking that carries over to C#, Kotlin and other languages.
Practice problems
Short programs that use println, variables and arithmetic. Every output is fixed, so you can check yours exactly. The last three are small business tasks.
Print your name and city
Print two lines: your name, then your city.
(none)
Name: Riya Sharma City: Pune
Show solution and explanation
public class Main {
public static void main(String[] args) {
System.out.println("Name: Riya Sharma");
System.out.println("City: Pune");
}
}How it works: each println prints its text and moves to a new line.
Add two numbers
Store 45 and 30 in two variables and print their sum.
45, 30
Sum = 75
Show solution and explanation
public class Main {
public static void main(String[] args) {
int a = 45;
int b = 30;
System.out.println("Sum = " + (a + b));
}
}How it works: the brackets make Java add the numbers first. Without them, "Sum = " + a + b would print Sum = 4530.
Hours in a week
Print how many hours there are in a week.
7 days × 24 hours
Hours in a week: 168
Show solution and explanation
public class Main {
public static void main(String[] args) {
int days = 7;
int hoursPerDay = 24;
System.out.println("Hours in a week: " + days * hoursPerDay);
}
}How it works: * runs before +, so the multiplication happens first.
Minutes to hours and minutes
A movie is 155 minutes long. Print it as hours and minutes.
155
2 h 35 min
Show solution and explanation
public class Main {
public static void main(String[] args) {
int total = 155;
System.out.println(total / 60 + " h " + total % 60 + " min");
}
}How it works: / between ints gives the whole part (2); % gives the remainder (35).
Average of three marks
Print the average of 78, 85 and 92 with decimals.
78, 85, 92
Average: 85.0
Show solution and explanation
public class Main {
public static void main(String[] args) {
int sum = 78 + 85 + 92;
double avg = sum / 3.0;
System.out.println("Average: " + avg);
}
}How it works: dividing by 3.0 (a decimal) keeps the fraction; dividing by 3 would give 85.
Auto-rickshaw fare
Fare is Rs 26 for the first 1.5 km and Rs 17 per km after that. Print the fare for a 6 km ride (4.5 extra km).
6 km
Fare: Rs 102.5
Show solution and explanation
public class Main {
public static void main(String[] args) {
double extraKm = 6 - 1.5;
double fare = 26 + extraKm * 17;
System.out.println("Fare: Rs " + fare);
}
}How it works: real fare meters do exactly this: a base fare plus a per-km rate. (Illustrative rates.)
Electricity bill
A home used 240 units at Rs 7 per unit plus a fixed charge of Rs 100. Print the bill.
240 units
Units: 240 Bill: Rs 1780
Show solution and explanation
public class Main {
public static void main(String[] args) {
int units = 240;
int bill = units * 7 + 100;
System.out.println("Units: " + units);
System.out.println("Bill: Rs " + bill);
}
}How it works: variables make the rule easy to change later when the rate changes. (Illustrative tariff.)
Split a dinner bill
Four friends share a bill of Rs 1,450. Print each person's share and what's left over if each pays a whole number of rupees.
1450, 4 people
Each pays: Rs 362 Left over: Rs 2
Show solution and explanation
public class Main {
public static void main(String[] args) {
int bill = 1450;
int people = 4;
System.out.println("Each pays: Rs " + bill / people);
System.out.println("Left over: Rs " + bill % people);
}
}How it works: 1450 / 4 = 362 (whole part) and 1450 % 4 = 2 rupees remaining, which someone has to cover.
Real-world scenario questions (asked in company interviews)
Companies rarely ask “define Java”. They describe a situation from the job and watch how you think. Read each scenario, decide your answer first, then open the model answer.
“Do we need to rebuild this for Linux?”
You built a small payment-report service on your Windows laptop with JDK 21. The operations team will run it on a Linux server. Your team lead asks: “Do we need to rebuild it for Linux? Anything you'd check before handing over the jar?”
The interviewer is checking: do you understand Write Once, Run Anywhere, and do you also know its practical limits?
Show a strong answer
No rebuild is needed. The .jar contains bytecode, and the JVM on Linux runs it as it is. Before handing it over, I would check four things:
- Java version on the server. It must be the same as, or newer than, the version I compiled for. If it's older, I compile with
javac --release 17(or whatever the server has), or ask ops to upgrade. - OS-specific assumptions in my code: hard-coded Windows paths like
C:\reports, and file-name letter case (Linux treatsReport.csvandreport.csvas different files). - Native libraries: if any library uses native code, it needs its Linux version.
- One trial run on a Linux test machine or container before production.
Avoid saying: “Java runs anywhere, so nothing can go wrong.” Bytecode is portable, but the assumptions in your code may not be.
The app won't start on the server
Your build works on your laptop, but on the server the app fails at start-up with: UnsupportedClassVersionError: Main has been compiled by a more recent version of the Java Runtime (class file version 65.0), this version of the Java Runtime only recognizes class file versions up to 61.0. What happened, and how do you fix it?
The interviewer is checking: can you read an error message calmly and connect it to how bytecode works?
Show a strong answer
Every .class file records which Java version produced it, called the major version. 65 means Java 21 and 61 means Java 17. So I compiled with JDK 21 but the server runs Java 17, and an older JVM refuses newer bytecode.
- Fix 1: upgrade the server's Java runtime to 21.
- Fix 2: if I don't need Java 21 features, compile for the older version with
javac --release 17(or set the release version in Maven or Gradle). - I can confirm the version with
javap -v Main.classand look formajor version. - Long term, the team should keep laptops, the CI build and the servers on the same Java version.
Avoid: reinstalling Java at random or trying different jar files until something works. Explain the cause first.
“Java is slow. Let's rewrite it in C++.”
A client's order-tracking system feels slow. In a meeting, someone says the fix is to rewrite it in C++. Your manager turns to you: “What do you think?”
The interviewer is checking: do you make decisions with measurements instead of myths, and can you say so politely to a client?
Show a strong answer
I'd suggest we measure where the time goes before changing the language:
- Most business applications spend most of their time waiting for databases, network calls and disk, not computing. A rewrite won't fix a slow SQL query or a missing index.
- Java's JIT compiler turns frequently used code into native machine code, so after warm-up Java is usually fast enough for this kind of work.
- Tools such as Java Flight Recorder, a profiler, or timing logs around each step will show the real bottleneck.
- A rewrite costs months of work and testing, and C++ brings back memory-safety risks. I'd only recommend it if the data shows the language itself is the problem.
Avoid: defending Java emotionally (“Java is the best!”) without offering a way to measure.
Which JDK should the new team install?
You're setting up laptops for a new three-year project. The options someone found are Oracle JDK 25, the newest non-LTS OpenJDK release, and Eclipse Temurin 21. Your lead asks you to recommend one.
The interviewer is checking: do you know about LTS versions, licences, and keeping the whole team consistent?
Show a strong answer
- Pick an LTS version (21 or 25). Non-LTS releases get updates for only about six months, which is too short for a three-year project.
- Match what production and the CI build will run, so everyone uses the same major version.
- Free OpenJDK builds such as Eclipse Temurin are widely used. For Oracle JDK, check Oracle's current licence terms with the company before using it in production.
- Write the version in the build file and README so new joiners install the right one.
My recommendation: Temurin 21 or 25, whichever production supports.
Avoid saying: “Always install the newest version.” The newest version isn't always the best supported one.