Welcome to the professional world of Java development. In this lesson, you will master the foundational environment setup required for enterprise-grade programming and understand the mechanics behind compiling and executing your very first Java application.
To write Java, you need more than just a text editor; you need the Java Development Kit (JDK). The JDK is a comprehensive suite of tools that includes the Java Runtime Environment (JRE), which allows you to run applications, and the Java Compiler (javac), which translates your human-readable source code into bytecode.
Professional developers typically use a specific version of the JDK, such as a Long-Term Support (LTS) release (e.g., JDK 17 or 21). When installing, ensure you set your JAVA_HOME environment variable. This variable acts as a global pointer for your operating system, telling your build tools and Integrated Development Environments (IDEs) exactly where the Java installation resides. Without this, your terminal will not recognize commands like javac or java.
Every professional Java program begins with a class. In Java, everything must reside inside a class because the language enforces strict Object-Oriented Programming (OOP) principles. When you create a file, the filename must match the class name exactly, followed by the .java extension.
Inside the class, you define a main method. This is the entry point of your application. The signature public static void main(String[] args) is non-negotiable.
public: Accessible from anywhere.static: Can be called without creating an instance of the class.void: Returns no value to the operating system.String[] args: An array containing command-line arguments.Java uses a two-step process to run code. Unlike interpreted languages, Java code must be compiled into a platform-independent format called bytecode. You perform this step using the javac command.
javac HelloWorld.java generates a HelloWorld.class file. This file contains the bytecode that the Java Virtual Machine (JVM) understands.java HelloWorld triggers the JVM, which loads the bytecode and executes it.The beauty of this architecture is that the same .class file can run on Windows, macOS, or Linux, provided the JVM is installed. This is the cornerstone of Java's "Write Once, Run Anywhere" philosophy. A common pitfall for beginners is failing to recompile after changing code; if you modify HelloWorld.java but forget to run javac again, the JVM will continue executing the old version of your program.
While you can write Java in any text editor, professionals use an Integrated Development Environment (IDE) such as IntelliJ IDEA, Eclipse, or NetBeans. An IDE does much more than provide syntax highlighting; it performs static analysis to catch errors before you even run your code.
In a professional setting, you will rarely run javac manually. Instead, you will use build automation tools like Maven or Gradle. These tools handle dependency management—automatically downloading external libraries your project needs—and manage the lifecycle of your code, including testing, packaging into a JAR (Java Archive) file, and deployment.
Note: Never rely solely on an IDE's "Run" button. Understanding how the compiler and the JVM work via the command line ensures you can troubleshoot complex build failures in production environments.
public static void main method.