Java Introduction
In this lesson
Java is one of the most used programming languages in the world. Banking systems, Android apps, Flipkart and Amazon back-ends, Infosys and TCS projects, Minecraft: all built with Java. If you want a job as a back-end or full-stack developer in India, Java is the language most companies hire for.
Why learn Java?#
- Write once, run anywhere. The same Java program runs on Windows, Mac, Linux and servers without changes.
- Huge job market. Service companies (TCS, Infosys, Wipro, Accenture) and product companies (Amazon, Flipkart, Paytm) use Java.
- Strong and safe. Java checks your types before the program runs, so many mistakes are caught early.
- Massive ecosystem. Spring Boot, Hibernate, Android, Kafka, Elasticsearch: all Java.
- Great for learning OOP. Java is built around classes and objects, the way large software is designed.
The magic trick: bytecode and the JVM#
Most languages turn your code into machine code for one type of computer. Java does something smarter:
Hello.java ──javac──▶ Hello.class ──java (JVM)──▶ runs on any OS
(your code) compiler (bytecode) virtual machine
- You write code in a
.javafile. - The compiler
javacturns it into bytecode (a.classfile). Bytecode is not for any specific CPU. - The JVM (Java Virtual Machine) reads the bytecode and runs it on your computer. There is a JVM for every operating system.
Think of bytecode as a movie with subtitles in a universal language, and the JVM as the local translator in every country.
JDK, JRE and JVM#
| Name | What it is | Who needs it |
|---|---|---|
| JVM | Runs bytecode, manages memory, does garbage collection | Built into the JRE |
| JRE | JVM + standard libraries: everything needed to *run* Java | Users running Java apps |
| JDK | JRE + compiler (javac) + tools: everything needed to *write* Java | You, the developer |
As a developer you install the JDK. Use an LTS (long-term support) version: Java 21 or 17.
Your first Java program#
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, VitalStack!");
}
}
Output:
Hello, VitalStack!
Line by line:
public class Hello– every Java program lives inside a class. The file must be namedHello.java(same as the public class).public static void main(String[] args)– the entry point. The JVM starts running your program here. You will understand each word soon: –public: the JVM can call it from outside. –static: no object needed to call it. –void: it returns nothing. –String[] args: command-line arguments.System.out.println(...)– prints a line of text to the console.- Every statement ends with a semicolon
;and blocks are wrapped in curly braces{ }.
Java is case-sensitive. System and system are different. Main and main are different. Most beginner errors are a capital letter or a missing semicolon.
Printing more#
public class Intro {
public static void main(String[] args) {
System.out.println("Line one");
System.out.print("Same ");
System.out.print("line");
System.out.println(); // just a new line
System.out.println(10 + 20); // 30
System.out.println("10 + 20 = " + (10 + 20));
}
}
println adds a new line at the end; print does not. The + joins text with values.
Comments#
// A single-line comment
/* A comment that
spans many lines */
/**
* A Javadoc comment: used to generate documentation.
*/
Comments are ignored by the compiler. Use them to explain why, not what.
Where Java is used#
- Back-end web development: Spring Boot REST APIs (you will learn this in this site’s Spring Boot course).
- Android apps: Android was built on Java (Kotlin now shares the stage).
- Enterprise software: banking, insurance, telecom, government systems.
- Big data: Hadoop, Kafka, Spark, Elasticsearch.
- Business apps: Jmix lets you build full apps in Java quickly.
Common mistakes#
- File name and class name don’t match (
hello.javawithclass Hello). - Missing semicolon or closing brace.
- Writing
system.out.printlninstead ofSystem.out.println. - Forgetting the exact
mainsignature, so the JVM can’t find where to start.
Exercise#
- Write a program that prints your name, your city and your favourite language on three lines.
- Print
5 * 7 = 35using a calculation, not by typing 35. - Use
printtwice andprintlnonce to make one line:Java is fun!.
Hint for 2
System.out.println("5 * 7 = " + (5 * 7)); The brackets make Java multiply before joining text.
Interview corner#
- Why is Java platform independent? Because the compiler produces bytecode, and each OS has its own JVM that runs that bytecode.
- JDK vs JRE vs JVM? JDK = tools to develop; JRE = what you need to run; JVM = the engine inside that executes bytecode.
- Is the JVM platform independent? No! Bytecode is platform independent. The JVM itself is different for each OS.
Summary#
- Java code (
.java) is compiled byjavacinto bytecode (.class), which runs on the JVM on any OS. - Install the JDK (Java 21 LTS) to write Java.
- Every program needs a class and a
mainmethod; statements end with;.
Next, you will install Java and run your first program on your own computer.
Quick quiz
3 questions. Check what stuck and earn 2 XP for each right answer.
Progress is saved in this browser. Create a free account to keep it on every device.
Get new lessons in your inbox
One email when we publish a new tutorial or guide. No spam, unsubscribe in one click.