Lesson 1 of 25

Introduction to Java

What is Java?

Java is a general-purpose programming language first released in 1995 by Sun Microsystems, a company Oracle later acquired. Three decades on it is still one of the most widely used languages in the world, and it is the language that most campus placement tests, most college data-structures courses and most enterprise software teams quietly assume you already know. If you are going to learn one language properly for placements, Java is a defensible choice.

Two words describe most of what makes Java feel the way it does. It is object-oriented, which means the unit you build programs out of is the class — a description of some kind of thing that bundles the data it holds together with the operations you can perform on it. It is also statically typed, which means every variable has a type fixed while the program is being compiled, and the compiler refuses to build your program if you use a value in a way its type does not allow.

Both of those feel like extra typing when your program is ten lines long. They start paying for themselves at ten thousand lines, which is roughly where every real project lives. A misspelled method name or a string handed to something expecting a number becomes a compile error you see in your editor within a second, rather than a crash your users find in production. That trade — more ceremony up front, fewer surprises later — is the whole personality of the language.

Java also manages memory for you. In C or C++ you allocate memory and are responsible for releasing it, and forgetting to do so leaks memory while releasing it twice corrupts your program. Java runs a garbage collector: a background process that notices when an object can no longer be reached by any part of your running code and reclaims its memory automatically. You will still write bugs, but they will not usually be memory bugs.

  • Object-oriented — you model your problem as classes and objects rather than as loose functions
  • Statically and strongly typed — the compiler catches whole categories of mistakes before the program runs
  • Platform independent — the same compiled file runs on Windows, macOS, Linux and Android
  • Automatic memory management — the garbage collector frees objects you have stopped using
  • A very large standard library — collections, file handling, dates, networking and threading all ship with the language
  • Backwards compatible to a fault — code written years ago usually still compiles, which is exactly why banks and large companies keep choosing it

"Write Once, Run Anywhere" — What Actually Happens

The slogan attached to Java since the beginning is "write once, run anywhere", and it is worth understanding the machinery behind it rather than repeating it as a slogan, because interviewers ask about it constantly.

A C program is compiled straight into instructions for one specific kind of processor and one specific operating system. Compile it on Windows and the result will not run on a Mac. Java inserts an extra layer. The Java compiler, javac, does not produce machine instructions at all — it produces bytecode, a compact instruction set for an imaginary computer, and writes it into a .class file. Bytecode is identical no matter which operating system compiled it.

That imaginary computer is then made real by the Java Virtual Machine (JVM), a program written separately for each platform. The Windows JVM, the Linux JVM and the Android runtime all read the same bytecode and translate it into instructions their own hardware understands. So the portability is not magic: somebody wrote a JVM for your platform, and your bytecode is portable because it targets the JVM instead of the hardware.

You will meet three acronyms and they nest inside one another. The JVM runs bytecode. The JRE (Java Runtime Environment) is the JVM plus the standard library — enough to run a Java program but not to compile one. The JDK (Java Development Kit) is the JRE plus the compiler and developer tools. As a student you install the JDK, because you need to compile, and the JDK contains everything else.

Example
Your code            Compiler         Portable output        Runtime

HelloWorld.java  --> javac       -->  HelloWorld.class  -->  JVM on Windows
(human-readable)                      (bytecode)              JVM on macOS
                                                              JVM on Linux
                                                              Android runtime

// The .class file is the same file in every case.
// Only the JVM differs from platform to platform.
Notes
  • A common exam question: "is Java compiled or interpreted?" The honest answer is both. Your source is compiled to bytecode ahead of time, and the JVM then interprets that bytecode — while also using a JIT (just-in-time) compiler to turn the parts that run most often into real machine code as the program runs. That is why a long-running Java server often gets faster after the first few seconds.

Your First Java Program, Word by Word

Every Java program lives inside a class, and execution begins in a method called main. The five lines below look like a lot of ceremony for one line of output, and beginners often memorise them without understanding them. Do not do that — each word is doing a specific job, and knowing which job makes the next twenty lessons easier.

public class HelloWorld declares a class named HelloWorld. public means any other code is allowed to use it. The name matters: if a class is declared public, the file must be named exactly after it, so this code has to live in HelloWorld.java with the same capital letters.

public static void main(String[] args) is the entry point — the exact method signature the JVM looks for when you start a program. static means the method belongs to the class itself rather than to an object, which matters because when your program starts, no objects exist yet; there is nothing for a non-static method to belong to. void means the method returns nothing. String[] args is an array of the words typed after the program name on the command line.

System.out.println("Hello, World!") reads right to left as three nested things: System is a class in the standard library, out is a field on it representing standard output, and println is a method on that which prints its argument followed by a newline. Note the semicolon. Java ends every statement with one, and a missing semicolon is the single most common error a beginner sees.

Example
// File: HelloWorld.java  (the filename must match the public class name)
public class HelloWorld {

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

// Output:
// Hello, World!

Compiling and Running It

Two commands take you from text to a running program. javac is the compiler and it takes a file name. java is the launcher and it takes a class name. Mixing those two up produces the first genuinely confusing error message most learners meet.

Run javac HelloWorld.java and, if there are no errors, nothing is printed at all — the compiler is silent on success, which surprises people. A file called HelloWorld.class now sits next to your source file. That file holds the bytecode. Then run java HelloWorld, with no extension, and the JVM loads that class and calls its main method.

If you type java HelloWorld.class, Java assumes HelloWorld.class is a class name — a class named class inside a package named HelloWorld — and reports that it cannot find it. The fix is simply to drop the extension. Since Java 11 there is also a shortcut for single-file programs: java HelloWorld.java compiles and runs in one step without leaving a .class file behind, which is convenient while learning.

Example
# 1. Compile — takes a FILE name. Silence means success.
javac HelloWorld.java

# 2. Run — takes a CLASS name, with no extension.
java HelloWorld
# Hello, World!

# The classic beginner error:
java HelloWorld.class
# Error: Could not find or load main class HelloWorld.class

# Java 11+ shortcut for a single source file — compile and run in one go
java HelloWorld.java
# Hello, World!
Notes
  • If javac reports class HelloWorld is public, should be declared in a file named HelloWorld.java, your filename and class name do not match. Java is case-sensitive everywhere: helloworld.java is not HelloWorld.java.

Where Java Is Actually Used, and Which Version to Learn

It helps to know what you are learning Java for, because the language shows up in very different places and the answer shapes what you study after this course.

On the version question, ignore the fact that a new Java release appears every six months. Only some releases are marked LTS (long-term support), and those are the ones companies actually deploy. Java 8 dominated for years, Java 11 replaced it, and Java 17 and Java 21 are the current mainstream choices. This course targets Java 17 and later, so everything here also works on 21 and on anything newer. If your college lab still has Java 8 installed, almost everything will still work, but a handful of newer conveniences — text blocks, the arrow form of switch, var — will not compile, and the lessons flag those where they appear.

  • Android applications — the Android platform was built around Java, and even Kotlin code runs on the same runtime and calls the same libraries
  • Backend and enterprise systems — banking, insurance, telecom, logistics and government software in India runs heavily on Java, usually with the Spring framework
  • Big data tooling — Hadoop, Kafka, Elasticsearch and Spark are all JVM projects
  • Placement tests and competitive programming — Java is accepted almost everywhere alongside C++ and Python
  • Desktop and embedded software, and the tooling around it such as Jenkins and most Android build systems
Notes
  • You will see the names Oracle JDK, OpenJDK, Temurin, Corretto and Zulu. These are different builds of the same open-source Java, not different languages. Any of them will run every example in this course identically. Pick whichever installs most easily on your machine.
Ask AI