25:00
Focus
Sign in to save your learning paths. Guest paths may be lost if you clear your browser data.Sign in
Lesson 1

Setting Up and First Java Program

~5 min50 XP

Introduction

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.

The Java Development Kit (JDK)

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.

Exercise 1Multiple Choice
Why is the 'JAVA_HOME' environment variable critical for a Java developer?

Anatomy of a Java Program

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.

Compilation and Execution

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.

  1. Compilation: Running javac HelloWorld.java generates a HelloWorld.class file. This file contains the bytecode that the Java Virtual Machine (JVM) understands.
  2. Execution: Running 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.

Exercise 2True or False
After modifying your Java source code, you do not need to recompile the file to see the changes.

Professional IDE Workflow

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.

Exercise 3Fill in the Blank
The file produced by the Java compiler that contains bytecode is known as a ___ file.

Key Takeaways

  • The JDK is essential for development and includes the compiler and runtime tools.
  • All Java code must reside within a class, and the entry point is the public static void main method.
  • Java code undergoes a two-stage process: compilation into bytecode, followed by execution by the JVM.
  • Professional developers leverage IDEs and build tools like Maven or Gradle to manage complex projects and dependencies efficiently.
Finding tutorial videos...
Go deeper
  • What is the difference between JDK, JRE, and JVM?🔒
  • Why is Java strict about placing everything inside a class?🔒
  • How do I check if my JAVA_HOME is set correctly?🔒
  • What makes an LTS version better for enterprise development?🔒
  • Can I use a different name for my Java source file?🔒