Setting Up Java and Writing Your First Program

Install a JDK, understand every word in the classic first program, and learn what compilation and execution actually do.

Installing a JDK

You need a JDK, not just a runtime. Any build of the same version behaves the same way, because they all implement the same specification.

  1. Install a JDK of a long term support version.
  2. Set JAVA_HOME to the installation folder.
  3. Add the bin folder inside it to the system PATH.

Verifying the installation

java -version     # the runtime
javac -version    # the compiler

If java answers but javac does not, a runtime is on the path instead of a full JDK.

The first program

public class Greeter {

    public static void main(String[] args) {
        System.out.println("Java is ready.");
    }
}

Reading that program word by word

PartMeaning
public class GreeterDeclares a class named Greeter, visible everywhere. A public class must live in a file of the same name.
publicThe launcher must be able to reach the method from outside the class.
staticThe method belongs to the class, so it can be called before any object exists.
voidThe method returns nothing. The exit status is set separately.
mainThe name the launcher looks for. Nothing else is special about it.
String[] argsThe command line arguments, already split for you. Never null, possibly empty.

Compiling and running

javac Greeter.java     # produces Greeter.class
java Greeter           # a class name, with no file extension

The compiler takes a file name. The launcher takes a class name and searches the classpath for it. Confusing the two is the most common first error.

Running a single file directly

Since Java 11 a single source file can be launched without a separate compile step, which is convenient for experiments:

java Greeter.java

The class is compiled in memory and discarded afterwards. This is for learning and small scripts, not for building applications.

Compact source files

Java 25 finalised a shorter form for small programs, where the class declaration is implicit and main need not be static:

void main() {
    IO.println("A very small program.");
}

Learn the full form first. Every real class you write, and every example in these notes, uses the explicit declaration.

Using command line arguments

public class Greeter {

    public static void main(String[] args) {
        String name = args.length > 0 ? args[0] : "stranger";
        System.out.println("Hello, " + name);
    }
}
java Greeter Anita     # Hello, Anita
java Greeter           # Hello, stranger

Common mistakes

  • Naming the file differently from the public class. The compiler rejects it.
  • Running java Greeter.class. Pass the class name only.
  • Assuming String args[] is wrong. It is legal, but String[] args is the conventional form.
  • Expecting args to contain the program name, as it does in some other languages. It does not.

Best practices

  • Keep one top level class per file.
  • Let the build tool or the editor compile for you once a project grows beyond a few files.
  • Treat main as a thin entry point that delegates to real classes.

Practice

  1. Rename the file to Hello.java without renaming the class. Predict the compiler message, then check it.
  2. Print the number of arguments received, and every argument on its own line.
  3. Why can main not be declared without static in the classic form? Answer in terms of object creation.

Conclusion

Compilation turns source into bytecode, and the launcher starts a JVM and calls main. Once those two steps are clear, the rest of the language is detail.

Useful resources

Hand picked references for this topic
Topics #Beginner #Java
Written by Lorens Mishra

Default administrator account created by the installer.

Continue reading

All Java notes →
Java

Introduction to Java

Java is a statically typed, object oriented language that compiles to bytecode and runs on a virtual machine, which is what makes it portable.

Read more

Discussion

0 comments
Sign in to join the discussion.

No comments yet. Be the first to say something.