Setup & Hello World
Dashboard
Topic 2/30
Home › Setup & Hello World

⚙️ Setup & Hello World

Get your environment ready for Java development and dive into writing your first interactive programs.

Installing the JDK

Before you can write and run Java applications, you need to install the Java Development Kit (JDK). Here are the standard steps:

  1. Download the latest JDK from the official Oracle website or adopt an OpenJDK distribution like Eclipse Temurin.
  2. Run the installer and follow the on-screen instructions.
  3. Note the installation path (e.g., C:\Program Files\Java\jdk-17).

Setting the PATH Variable

To compile and run Java programs from any command line window, you must add the JDK's bin folder to your system's PATH variable:

  • Windows: Open System Properties > Advanced > Environment Variables. Edit the Path variable and add the path to your JDK bin directory.
  • macOS/Linux: Add export PATH=$PATH:/path/to/jdk/bin to your ~/.bashrc or ~/.zshrc file.
Tip: Verify your installation by opening a terminal and running java -version and javac -version. You should see the installed JDK version.

Anatomy of a Java Program

Every Java application requires at least one class and a main method. The main method acts as the entry point for the JVM.

HelloWorld.java Java
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
            
Output
Hello, World!

Compilation and Execution Commands

To run the program above manually from a command line:

  1. Compile the code: javac HelloWorld.java. This creates a file named HelloWorld.class containing the bytecode.
  2. Run the code: java HelloWorld. This commands the JVM to execute the main method inside the compiled class.

Naming Conventions

  • Classes: PascalCase (e.g., HelloWorld)
  • Methods & Variables: camelCase (e.g., main, myVariable)
  • Constants: UPPER_SNAKE_CASE (e.g., MAX_VALUE)
  • Packages: lowercase (e.g., java.util)

Interactive Programs using Scanner

Let's make our programs interactive by reading user input using the Scanner class.

UserInput.java Java
import java.util.Scanner;

public class UserInput {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        System.out.print("Enter your name: ");
        String name = scanner.nextLine();
        
        System.out.println("Welcome to Java, " + name + "!");
        scanner.close();
    }
}
            

Building a Simple Calculator

SimpleCalculator.java Java
import java.util.Scanner;

public class SimpleCalculator {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        
        System.out.print("Enter first number: ");
        int num1 = scan.nextInt();
        
        System.out.print("Enter second number: ");
        int num2 = scan.nextInt();
        
        int sum = num1 + num2;
        System.out.println("The sum is: " + sum);
        
        scan.close();
    }
}
            

🎯 Frequently Asked Interview Questions

Q1: Explain every keyword in public static void main(String[] args).
Answer:
  • public: Access modifier allowing JVM to invoke main from outside the class package.
  • static: Allows JVM to call main without creating an instance of the class first.
  • void: Indicates main method does not return any value.
  • main: Designated entry point name searched by the JVM.
  • String[] args: Array of command-line arguments passed into the program.
Q2: What happens if you remove static from the main method?
Answer: The program compiles successfully, but at execution time, the JVM throws a NoSuchMethodError: main method is not static error because JVM cannot instantiate the host class automatically.
Q3: What is the difference between PATH and CLASSPATH environment variables?
Answer: PATH tells the OS shell where to find binary executables like javac and java. CLASSPATH tells the Java compiler/JVM where to look for user-defined .class files and JAR libraries.