Java Get Started

Java Get Started: Your First Program

Welcome to the world of Java! Java is one of the most popular and widely-used programming languages, powering everything from mobile apps to large-scale enterprise systems.

On IntricateDevo, you don't need to install anything to run the code examples. You can use the "Intricate yourself >>" button to run Java code directly in your browser!


What is Java?

Java is a high-level, object-oriented, and platform-independent programming language created by Sun Microsystems in 1995. Its core philosophy is "Write Once, Run Anywhere" (WORA). This means that Java code compiled on one platform (like Windows) can run on any other platform that has a Java Virtual Machine (JVM) without needing to be recompiled.

Key Features of Java:


Setting Up Your Environment (Optional)

If you want to write and run Java code on your own computer, you need to install the Java Development Kit (JDK).

JDK vs. JRE vs. JVM

To start programming, you need the JDK. You can download it for free from the official Oracle website or use an open-source alternative like OpenJDK.


Your First Java Program: "Hello, World!"

Let's write the traditional first program that every programmer creates. It's a simple program that prints the text "Hello, World!" to the console.

Hello World in Java

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

Code Breakdown:


Compiling and Running from the Command Line

If you have the JDK installed, you can compile and run your program using a terminal or command prompt.

  1. Save the code: Save the code above in a file named Main.java.

  2. Open the terminal: Navigate to the directory where you saved the file.

  3. Compile the code: Run the Java compiler, javac.

    javac Main.java
    

    This command will create a new file called Main.class, which contains the Java bytecode.

  4. Run the program: Run the Java application launcher, java.

    java Main
    

    Notice that you don't include the .class extension. The output will be:

    Hello, World!
    

Detailed Installation Guide

While the interactive editor is great for learning, eventually you will want to run Java on your own machine. Here is a step-by-step guide to installing the JDK.

Step 1: Download the JDK

Oracle provides a commercial JDK, but the open-source OpenJDK is entirely free and functionally identical for most purposes. Sites like Adoptium (Eclipse Temurin) or Amazon Corretto provide excellent, free OpenJDK binaries.

  1. Go to the Adoptium website.
  2. Download the latest LTS (Long Term Support) version for your operating system (e.g., Java 17 or Java 21).

Step 2: Install on Windows

  1. Run the downloaded .msi installer.
  2. Follow the setup wizard. Crucial Step: When prompted for features to install, ensure that the option to "Set JAVA_HOME variable" and "Add to PATH" are selected. This saves you from having to do it manually.
  3. Finish the installation.

Step 3: Install on macOS

  1. Run the downloaded .pkg installer and follow the wizard.
  2. Alternatively, if you use Homebrew, you can open your terminal and run: brew install --cask temurin

Step 4: Verify the Installation

Open a new terminal (Command Prompt on Windows, Terminal on macOS/Linux) and type: java -version

You should see output similar to:

openjdk version "21.0.1" 2023-10-17 LTS
OpenJDK Runtime Environment Temurin-21.0.1+12 (build 21.0.1+12-LTS)
OpenJDK 64-Bit Server VM Temurin-21.0.1+12 (build 21.0.1+12-LTS, mixed mode)
Next, check the compiler: ```bash javac -version ``` If both commands return a version number, your installation is successful!

Setting Environment Variables (Windows Manual Method)

If your terminal says 'javac' is not recognized as an internal or external command, you need to set your environment variables manually.

  1. Find where Java is installed (usually C:\Program Files\Eclipse Adoptium\jdk-21...\bin). Copy this path.
  2. Press the Windows key, type "Environment Variables", and select Edit the system environment variables.
  3. Click the Environment Variables... button at the bottom.
  4. Under System variables, find the variable named Path and select it, then click Edit.
  5. Click New and paste the path you copied in step 1.
  6. Click OK on all windows to save the changes.
  7. Restart your Command Prompt and try javac -version again.

Choosing an Integrated Development Environment (IDE)

While you can write Java in standard text editors like Notepad, using an IDE makes coding significantly easier by providing code completion, syntax highlighting, and powerful debugging tools.

Here are the top choices for Java developers:

1. IntelliJ IDEA (Recommended)

Developed by JetBrains, IntelliJ IDEA is widely considered the best IDE for Java. The "Community Edition" is completely free and more than sufficient for learning and developing standard applications. It offers the smartest code completion and refactoring tools available.

2. Eclipse

Eclipse is a classic, open-source IDE that has been around for decades. It has a massive ecosystem of plugins and is still heavily used in many enterprise environments.

3. Visual Studio Code (VS Code)

VS Code is a lightweight, incredibly popular code editor developed by Microsoft. While not a full-fledged Java IDE out of the box, installing the "Extension Pack for Java" transforms it into a highly capable environment. It's a great choice if you already use VS Code for other languages like JavaScript or Python.


Understanding the main Method Signature

Let's take a closer look at the signature of the main method, as it can look intimidating to beginners:

public static void main(String[] args)


Common Beginner Mistakes

As you start writing your own Java code, you might run into a few common pitfalls. Keep an eye out for these:

  1. File Name Mismatch: In Java, if a class is declared as public, the file name must exactly match the class name. If your class is public class MyProgram, the file must be saved as MyProgram.java.
  2. Case Sensitivity: Java is strictly case-sensitive. System is not the same as system. String is not the same as string.
  3. Missing Semicolons: Almost every statement in Java must end with a semicolon (;). Forgetting one is the most common cause of compilation errors.
  4. Unmatched Braces: Every opening curly brace { must have a corresponding closing brace }. This is how Java defines blocks of code. IDEs help with this by highlighting matching braces.

Exercise

?

What is the main entry point for any Java application?