---
# System prepended metadata

title: Jam 01 - Exercise 2 - Hello World & Java Fundamentals
tags: [programming, "3 \U0001F9EA in testing", java, jam01, "4 \U0001F973 done"]

---

---
title: "Jam 01 - Exercise 2 - Hello World & Java Fundamentals"
tags:
  - 3 🧪 in testing
  - 4 🥳 done
  - jam01
  - java
  - programming
created: 2025-01-20
updated: 2025-01-20
---
<!-- markdownlint-disable line-length single-h1 no-inline-html -->
<!-- markdownlint-configure-file { "ul-indent": { "indent": 4 }, "link-fragments": {"ignore_case": true} } -->

# Exercise 2 - Hello World & Java Fundamentals

{%hackmd dJZ5TulxSDKme-3fSY4Lbw %}

## Overview - Exercise 2

Now that you're familiar with IntelliJ, let's write your first Java program! This exercise will help you understand basic Java syntax and program structure while practicing IntelliJ's features.

## Required Steps - Hello World

:::info
🔑 **About Working Directories**

- The working directory `jam01` listed at the start of this document indicates where your work should be placed
- You should create this package directory under `src/main/java` if it doesn't exist
- This pattern will be used throughout the course - always check the "Working Directory" field in the jam header

:::

1. **Create a New Class**
    - In the Project view, right-click on the `jam01` package
        - Create the package if it doesn't exist yet
    - Select New > Java Class
        :::spoiler 🚨 **If "Java Class" isn't an option** (Click to open)
        You need to mark your source directories in IntelliJ:
        1. Right-click on `src/main/java`
        2. Select "Mark Directory as" > "Sources Root"
        3. Right-click on `src/test/java`
        4. Select "Mark Directory as" > "Test Sources Root"

        After marking the directories, the "New > Java Class" option should appear.
        The directories will also be color-coded (blue for sources, green for tests) to indicate their status.
        :::
    - Name it `HelloWorld`<br/>![Hello World Screenshot](https://hackmd.io/_uploads/SyTMxQFO1g.png =70%x) <!-- markdownlint-disable-line no-bare-urls -->

    - You may see a popup asking if you want to add the file to `git`. Click "Add" - this helps track your file in git, but you'll still need to stage and commit any changes you make to the file later <br/>![Add file to git prompt](https://hackmd.io/_uploads/HJsjxQYO1l.png =70%x) <!-- markdownlint-disable-line no-bare-urls -->

    - Verify your class template appears with header
        - IntelliJ may automatically collapse this header for readability - that's okay! You can click the small `>` icon in the left gutter to expand it when needed

2. **Write the Program**
    - Replace the template with the following code:
        - Notice that IntelliJ automatically added the `package jam01;` statement at the top - this matches your file location
        - Note that the file name `HelloWorld.java` matches the class name `HelloWorld` exactly - this is required in Java

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

## Compile and Run Your Program

### Using the Terminal

1. Open a terminal window
2. Navigate to your project's `src/main/java` directory
3. Compile your program:

    ```bash
    javac jam01/HelloWorld.java
    ```

    The `javac` command compiles your Java source code (`.java` file) into bytecode (`.class` file) that can be run by the Java Virtual Machine (JVM). The path `jam01/HelloWorld.java` tells the compiler where to find your source file relative to your current directory.

    Verify that a `HelloWorld.class` file was created in your jam01 folder.

4. Run your program:

    ```bash
    java jam01.HelloWorld
    ```

    Note that when running the program:
    - We use a period (`.`) instead of a slash (`/`) to separate package and class names
    - We don't include the `.class` extension - Java knows to look for the bytecode file

### Using IntelliJ

- Click the green "play" button next to main
- Or use the keyboard shortcut (Shift+F10)
- Verify output in the Run window

:::info
🔑 **Key Java Concepts**

**Comments & Syntax**

- Comments start with `//` - compiler ignores everything to the end of the line
- Multi-line comments start with `/*` and end with `*/`
- All statements must end with a semicolon (`;`)
- Java is case-sensitive! `Hello` and `hello` are different

**File & Class Structure**

- Files must have `.java` extension
- Package statement (`package jam01;`) must match directory structure
- Public class name must match file name exactly (including case)
- Every application needs a `main` method to start execution
- Braces `{ }` group statements into blocks

**Output & Program Flow**

- `System.out.println("...")` prints text with a newline at the end

:::

## Save Your Work - Exercise 2

Verify what files are uncommited:

```bash
git status
```

Stage your changes (This should be the file shown in `git status` as modified)
Feel free to use a different message as long as it's descriptive

```bash
git add src/main/java/jam01/HelloWorld.java
```

Commit your work

```bash
git commit -m "jam01: Complete Hello World program"
```

Your working directory should be clean. It might not be! We made some changes to your IDE configuration that should need to be commited. Handle any uncommited files as necessary.

> 🔍 **Checkpoint**: Before continuing, verify:
>
> - Your program compiles without errors
> - You see the expected output
> - You can run from both terminal and IDE
> - Your changes are committed
