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.
-
Java Basics
- Introduction to Java
- Setting Up Java and Writing Your First Program
- Variables, Data Types and Literals in Java
- Type Casting and Type Conversion in Java
- Operators and Expressions in Java
- Input and Output in Java
- Comments, Keywords and Naming Conventions in Java
- Control Flow in Java: if, else and switch
- Loops in Java: for, while and do-while
- Methods
- Arrays and Strings
-
OOP
- Classes and Objects in Java
- Constructors in Java
- The this Keyword in Java
- The static Keyword in Java
- Encapsulation in Java
- Access Modifiers in Java
- Inheritance in Java
- Method Overriding and super in Java
- Polymorphism in Java
- Abstraction, Abstract Classes and Interfaces in Java
- Composition, Aggregation and Association in Java
- The Object Lifecycle in Java
- Core Java
- Exception Handling
-
Collections
- The Java Collections Framework
- List in Java: ArrayList, LinkedList, Vector and Stack
- Set in Java: HashSet, LinkedHashSet and TreeSet
- Map in Java: HashMap, LinkedHashMap and TreeMap
- How HashMap Works Internally in Java
- Queue and Deque in Java: ArrayDeque and PriorityQueue
- Iterators in Java
- Comparable and Comparator in Java
- Collections Utilities and Choosing the Right Collection
- Generics
- Java 8+
- Stream API
- Date and Time
- File and I/O
-
Multithreading
- Threads in Java: Processes, Runnable and Thread
- Thread Lifecycle in Java
- Synchronization in Java: synchronized and volatile
- Locks and Atomic Classes in Java
- Race Conditions and Deadlocks in Java
- The Executor Framework and Thread Pools in Java
- Future and CompletableFuture in Java
- Concurrent Collections in Java
- The Java Memory Model
- JVM and Memory
- Advanced Java
- Networking
- JDBC
- Testing
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.
- Install a JDK of a long term support version.
- Set
JAVA_HOMEto the installation folder. - Add the
binfolder inside it to the systemPATH.
Verifying the installation
java -version # the runtime
javac -version # the compilerIf 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
| Part | Meaning |
|---|---|
public class Greeter | Declares a class named Greeter, visible everywhere. A public class must live in a file of the same name. |
public | The launcher must be able to reach the method from outside the class. |
static | The method belongs to the class, so it can be called before any object exists. |
void | The method returns nothing. The exit status is set separately. |
main | The name the launcher looks for. Nothing else is special about it. |
String[] args | The 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 extensionThe 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.javaThe 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, strangerCommon 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, butString[] argsis the conventional form. - Expecting
argsto 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
mainas a thin entry point that delegates to real classes.
Practice
- Rename the file to
Hello.javawithout renaming the class. Predict the compiler message, then check it. - Print the number of arguments received, and every argument on its own line.
- Why can
mainnot be declared withoutstaticin 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.