Java Programming: Complete Beginner to OOP
Chapter 1 / 8· 18 min read· 0 cards

Why Java, and Your First Program

Why Java runs everywhere, how the JVM works, and your first program explained line by line.

Why learn Java?

Java is one of the most popular and enduring programming languages on earth. It runs the back ends of banks, e-commerce giants, and enterprise software; it's the native language of Android app development; and it's a staple of university courses and campus placements across India. Companies value Java developers because Java powers the large, reliable systems that businesses depend on — and there are millions of jobs that ask for it.

Java's famous promise is "write once, run anywhere". A Java program you write on Windows runs unchanged on Mac, Linux, or a server, thanks to a clever piece of technology called the JVM. Combined with strong object-oriented design and a vast ecosystem of libraries and frameworks (like Spring and Android), Java is a skill that opens a huge number of career doors.


How Java runs: the JVM

Java works differently from both Python and C, and understanding it clears up a lot. Your Java code is first compiled — but not into machine code for a specific computer. Instead it's compiled into an intermediate form called bytecode. That bytecode then runs on the Java Virtual Machine (JVM), a program that exists for every operating system and translates bytecode into the right machine instructions on the fly.

javac Main.java   # compile: Main.java -> Main.class (bytecode)
java Main         # run: the JVM executes the bytecode

This two-step model — compile to bytecode, run on the JVM — is exactly what makes Java portable. The same .class file runs anywhere a JVM exists. The playground in this lesson handles compiling and running for you, so you can follow along immediately; install the JDK (Java Development Kit) later for local work.


Your first program, dissected

Java's first program looks more ceremonial than Python's, and every part has a reason. Let's go through it:

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, world!");
    }
}

public class Main — in Java, all code lives inside a class, and the file must be named after the public class (Main.java). public static void main(String[] args) is the main method — the exact entry point the JVM looks for to start your program. System.out.println(...) prints a line to the screen. This structure is mandatory in Java; it feels heavy at first, but you'll soon read past it without thinking.

Why so much structure compared to Python's one-line print? Because Java is built for large, organised programs where this explicitness pays off. Don't let it intimidate you — for now, just know that your code goes inside main, and System.out.println is how you print.


Printing and reading input

You print with System.out.println (prints and adds a new line) or System.out.print (no new line). To read input from the user, Java uses a Scanner:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter your age: ");
        int age = sc.nextInt();
        System.out.println("Next year you'll be " + (age + 1));
    }
}

The import java.util.Scanner; line brings in the Scanner class. We create one connected to keyboard input (System.in), then sc.nextInt() reads a whole number. Notice the + in the print — it joins text and the number together into one message. Java automatically converts the number to text when you join it with a string this way.


The shape of every Java program

Almost every simple Java program follows the same skeleton: a public class, a main method inside it, your code inside that, and any import lines at the top. Once this structure is familiar, you stop seeing it as overhead and start seeing it as the organised home for your logic. With the basics of compiling, printing, and reading input in hand, the next chapter covers how Java stores data in its strongly-typed variables.

Reading mode · scroll to read at your own pace

Finished "Why Java, and Your First Program"?

Mark this chapter complete so you can pick up exactly where you left off. Your progress saves locally — sign in to sync across devices.

Was this chapter clear?

Try it yourself — open the Code Playground15+ languages — Python, JavaScript, Java, C++, SQL & more — full IDE-style editor, instant run. Your code is auto-saved per language.