Register

Java for Beginners

0 of 24 notes read 0%

Reading progress is saved to your INFIRALS account. Sign in to keep this note marked as read.

How Java runs and how to get set up

Starting out10 min readLesson 1 of 3

1.1 Java source, bytecode, and the JVM

A Java program starts as a source file with a .java extension. You write instructions in plain text, and the compiler javac translates them into bytecode, a set of instructions stored in one or more .class files. The JVM (Java Virtual Machine) loads that bytecode and runs it.

The JDK (Java Development Kit) bundles the compiler, the java launcher, and the standard library you will use every day. The JVM is the part that executes programs. The JDK also includes the runtime tools that start the JVM.

Java 21 is a long-term support release and a practical target for learning. The examples in this course use standard syntax that also works on Java 17.

1.1.1 Install a JDK

  1. Download a JDK 21 build from a trusted distribution such as Oracle JDK, Eclipse Temurin, or Microsoft Build of OpenJDK. Any current distribution that supports Java 21 works for these notes.
  2. Run the installer. Keep the option that adds the tools to your system PATH, or note the installation folder for later.
  3. Open a new terminal. Installers change the PATH, and an older terminal window may not see the update.
  4. Run java -version and javac -version. The two version numbers should match closely, and both commands should be found.

If a command is not recognized, check that a JDK is installed and that its bin folder is on the PATH. Restarting the terminal or the editor is often enough after installation. An editor with Java support adds syntax highlighting and can run the current file without switching windows.

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

Output

Hello, world!

1.1.2 Compile and run

Save the program as Main.java. The file name must match the name of the public class inside it. In the terminal, move to the folder that contains the file, then run these commands:

  1. javac Main.java compiles the source and creates Main.class.
  2. java Main starts the JVM and names the class to run. Do not add the .class extension here.
  3. java Main.java is a shortcut available in recent Java versions. It compiles and runs a single source file without leaving a class file next to it.

The shortcut is handy for small experiments. Use javac and java separately when you want to see the compiled output or compile several classes before running them. A wrong folder produces a "cannot find file" error from javac, so check the terminal prompt first.

java
public class Main {
    public static void main(String[] args) {
        // print keeps the cursor on the same line; println starts a new line
        System.out.print("Java ");
        System.out.println("is running.");
        System.out.println(2 + 3);
    }
}

Output

Java is running.
5

1.1.3 What each line does

public class Main declares a class named Main. The word public means code in other packages may use it. For now, treat the class as the container that holds your program.

public static void main(String[] args) is the entry point the launcher looks for. It is a method, and its parts are the public access modifier, the static keyword, the void return type, and a parameter list. You can read the signature as "a public, static method called main that returns nothing and receives a list of strings."

System.out.println("...") calls a printing method and moves to the next line. System is a standard class, out is a static field on it, and println is a method of that output stream.

Braces group the class body and method body. A semicolon ends a statement. Java ignores extra spaces and blank lines, but the compiler is strict about spelling and capitalisation.

java
public class Main {
    public static void main(String[] args) {
        System.out.println("Command-line arguments: " + args.length);
    }
}

Output

Command-line arguments: 0

1.1.4 Compiler errors and launch errors

javac reports problems with the source. A missing semicolon, a misspelled name, or a type mismatch stops compilation before any class file is produced. The message usually names the file, line, and symbol:

  • ';' expected points to a missing semicolon near the reported position.
  • cannot find symbol often means a name is misspelled or used outside its scope.
  • class Main is public, should be declared in a file named Main.java is a file naming problem.
  • incompatible types means the value on the right cannot be assigned to the type on the left.

The java launcher reports a different stage. ClassNotFoundException or "could not find or load main class" usually means the class name is wrong or the terminal is in the wrong folder. Fix the first compiler error and compile again, because one mistake can trigger several follow-up messages.

1.1.5 A workflow you can repeat

  1. Create a folder for small programs, then open it in an editor.
  2. Create Main.java and write a class with a main method.
  3. Open a terminal in the same folder.
  4. Compile with javac Main.java and run with java Main.
  5. After an edit, compile again. The launcher reads the compiled class, so an old .class file can hide your latest change.

You can shorten the cycle with java Main.java while learning. Keep the separate commands in mind because build tools and larger projects rely on compiled classes.

Reading the output, the exit code, and the first error message is enough for most beginner problems. Search the exact message when it names an unfamiliar symbol.

java
public class Main {
    public static void main(String[] args) {
        int lessons = 3;
        int minutes = 12;
        System.out.println("Lessons: " + lessons);
        System.out.println("Minutes: " + lessons * minutes);
    }
}

Output

Lessons: 3
Minutes: 36

1.1.6 Practice

  1. Write a Main.java that prints your name and the name of the editor you use.
  2. Compile it with javac, run it with java Main, then delete the class file and run it again with java Main.java.
  3. Remove a semicolon, compile, and read the error. Put the semicolon back.
  4. Change the public class name to Hello without renaming the file, then read the compiler message. Restore the original names.

These small failures are useful because the messages become familiar while the program is still short. Keep one folder of experiments so the file and class names stay easy to find.

Interview questions

3 questions from this note. Open one to see the answer; no account needed.

  • 01What is the difference between the JDK and the JVM?

    The JDK is the development kit that provides the compiler, launcher, and standard library. The JVM is the runtime that executes compiled bytecode.

  • 02What does `javac` produce?

    It compiles `.java` source into bytecode and writes one `.class` file for each class in the source file. The launcher runs a class by name.

  • 03Why does `java Main.class` fail?

    The `java` launcher expects a class name, and the `.class` suffix is not part of that name. Run `java Main` instead.

Quiz

Check: how Java runs

Four questions on source files, the compiler, bytecode, the JVM and the JDK.

4 questions

Question 1 of 4

0/4 answered

What does the command javac produce from a .java source file?

Answer all you can, then submit.