Java is a statically typed, object-oriented language that runs on the Java Virtual Machine (JVM). It powers banking systems, Android apps, large web back-ends and big-data tools. Install a JDK (Java Development Kit) — version 21 LTS or newer — from adoptium.net, then check with java -version.
Every Java program starts in a main method inside a class. The file name must match the public class name: class Hello lives in Hello.java.
Since Java 11 you can run a single source file directly with java Hello.java — ideal for learning. Larger projects compile with javac (or a build tool such as Maven) into .class files that the JVM runs. IntelliJ IDEA Community and VS Code with the Java extensions are excellent free editors.
import java.time.Year;
public class Hello {
public static void main(String[] args) {
String name = "Dolese";
int year = Year.now().getValue();
System.out.println("Hello, " + name + "! Welcome to Java in " + year + ".");
}
}java -version
java Hello.java # compile and run in one step
javac Hello.java # or: compile to Hello.class ...
java Hello # ... then run the compiled classKey points
- Programs start at
public static void main(String[] args). - A public class must live in a file with the same name.
java File.javaruns a single file directly;javaccompiles for larger projects.
Exercise
Write About.java that prints your name, school and favourite subject on three lines. Then pass your name as a command-line argument (java About.java Amina) and print it using args[0].
Show solution
Try the exercise yourself first — then compare your approach with this one.
Command-line arguments arrive in the args array. Checking args.length first avoids an ArrayIndexOutOfBoundsException when no name is given.
public class About {
public static void main(String[] args) {
String name = args.length > 0 ? args[0] : "Amina";
System.out.println("Name: " + name);
System.out.println("School: Azania Secondary");
System.out.println("Favourite subject: Biology");
}
}java About.java Juma
# Name: Juma
# School: Azania Secondary
# Favourite subject: Biology