How to Install Java 21 and Set Up Your Development Environment

Here’s a step-by-step guide to install Java 21 and set up your development environment:

Step 1: Download and Install Java 21

  1. Download JDK 21:
    • Go to the official Oracle Java SE Downloads page or use Adoptium or OpenJDK for an open-source version.
    • Download the JDK 21 version suitable for your system (Windows, macOS, or Linux).
  2. Install Java 21:
    • Windows:
      • Run the installer file and follow the prompts.
    • macOS:
      • Use the .dmg package and follow the installation instructions.
    • Linux:
      • Extract the .tar.gz archive or use a package manager like apt or yum if supported by your Linux distribution.
      • Example for Ubuntu/Debian:
        sudo apt update
        sudo apt install openjdk-21-jdk
        

Step 2: Set JAVA_HOME and PATH

Once Java is installed, set the JAVA_HOME and add the binary folder to your PATH.

Windows:

  1. Open System Properties:
    • Press Win + S, search for “Environment Variables,” and click it.
  2. Add a JAVA_HOME variable:
    • Click New under System Variables.
    • Variable Name: JAVA_HOME
    • Variable Value: Path to the JDK installation directory (e.g., C:\Program Files\Java\jdk-21).
  3. Update the PATH variable:
    • Select the Path variable, click Edit, and add %JAVA_HOME%\bin.

macOS / Linux:

  1. Open your terminal and edit your shell configuration file (e.g., ~/.bashrc, ~/.zshrc, or ~/.bash_profile):
    export JAVA_HOME=/path/to/java/jdk-21
    export PATH=$JAVA_HOME/bin:$PATH
    
  2. Apply the changes:
    source ~/.bashrc
    # or
    source ~/.zshrc
    
  3. Verify the installation:
    java -version
    

Step 3: Set Up IntelliJ IDEA

  1. Download IntelliJ IDEA:
    • Visit the IntelliJ IDEA website and download the latest version.
    • Install the Ultimate Edition or the Community Edition, depending on your needs.
  2. Configure IntelliJ IDEA with Java 21:
    • Open IntelliJ IDEA and go to File > Project Structure > SDKs.
    • Click + to add a new JDK.
    • Navigate to the Java 21 installation folder and select it.
  3. Set the project’s JDK version:
    • Go to File > Project Structure > Modules and assign the JDK 21 to your project.

Step 4: Verify the Java Development Setup

  1. Create a sample application to test the setup:
    • Create a new Java project in IntelliJ.
    • Write a “Hello, World!” program:
      public class Main {
          public static void main(String[] args) {
              System.out.println("Hello, World!");
          }
      }
      
    • Run the program to ensure it works as expected.
  2. Confirm the Java version:
    • Run the following in the terminal:
    java -version
    
  3. IntelliJ’s terminal should point to Java 21.

Optional: Tools to Enhance Development

  1. Maven/Gradle:
    • Set up Maven or Gradle build tools for dependency management.
  2. Version Control:
    • Install Git and set it up in IntelliJ.
  3. Extensions and Plugins:
    • Install helpful IntelliJ plugins like Lombok, Checkstyle, JRebel, or a Database tool.
  4. Docker:
    • If you’re working with containers, install Docker and configure IntelliJ’s Docker plugin.

You now have Java 21 and your development environment fully set up and configured!

How to Format Strings Using String.format()

In Java, the String.format() method is a convenient way to create formatted strings using placeholders. It allows you to include values such as numbers or strings at specific positions in a string by using format specifiers. Here’s how you can use it:

Syntax

String.format(String format, Object... args)
  • format: The format string with placeholders.
  • args: The arguments to replace the placeholders.

Common Format Specifiers

  • %s: String.
  • %d: Decimal integer.
  • %f: Floating-point number.
  • %c: Character.
  • %%: Literal % character.

You can combine these with width, precision, alignment, and other formatting options.


Examples

1. String Formatting

String name = "John";
int age = 30;
String formattedString = String.format("My name is %s and I am %d years old.", name, age);
System.out.println(formattedString);
// Output: My name is John and I am 30 years old.

2. Formatting Numbers

double price = 123.456789;
String formattedPrice = String.format("The price is %.2f.", price);
System.out.println(formattedPrice);
// Output: The price is 123.46.
  • %.2f: Limits the floating-point value to 2 decimal places.

3. Padding and Alignment

  • Right-aligned text:
String formattedString = String.format("%10s", "Java");
System.out.println(formattedString);
// Output: "      Java" (padded with spaces to the left, 10 characters in total)
  • Left-aligned text:
String formattedString = String.format("%-10s", "Java");
System.out.println(formattedString);
// Output: "Java      " (padded with spaces to the right, 10 characters in total)

4. Adding Leading Zeros

int number = 42;
String formattedNumber = String.format("%05d", number);
System.out.println(formattedNumber);
// Output: 00042

5. Formatting Multiple Values

String result = String.format("%s scored %d out of %d in the exam.", "Alice", 90, 100);
System.out.println(result);
// Output: Alice scored 90 out of 100 in the exam.

6. Escaping %

To include a literal % in the string, use %%.

String formattedString = String.format("Progress: %.2f%%", 85.123);
System.out.println(formattedString);
// Output: Progress: 85.12%

Notes

  1. Null Values: If a value in args is null, %s outputs the string "null".
  2. Exceptions: Make sure the placeholders match the number and type of arguments; otherwise, it may throw an exception (e.g., IllegalFormatException).

Formatted strings are especially useful when generating user-friendly messages or handling precise output formatting, such as in reporting systems or logs.

How to compile and run Java 17 code using command line

To compile and run Java 17 code using the command line, follow these steps:


1. Install Java 17

  • Ensure that Java 17 is installed on your system.
  • Run the following command to check the installed Java version:
java -version

If Java 17 is not installed, download and install it from the official Oracle website or use OpenJDK.


2. Write Your Java Code

  • Create a Java file with the .java extension. For example, create a file named HelloWorld.java with the following content:
public class HelloWorld {
   public static void main(String[] args) {
       System.out.println("Hello, World!");
   }
}

3. Open Command Line

  • Open a terminal (on Linux/Mac) or Command Prompt/PowerShell (on Windows).

4. Navigate to the Directory

  • Go to the directory where the .java file is located using the cd command. For example:
cd /path/to/your/code

5. Compile the Java File

  • Use the javac command to compile the .java file into bytecode. The javac compiler will create a .class file.
javac HelloWorld.java
  • If there are no errors, you’ll see a file named HelloWorld.class in your directory.

6. Run the Compiled Java File

  • Execute the compiled .class file using the java command (without the .class extension):
java HelloWorld
  • You should see the following output:
Hello, World!

7. (Optional) Use Java 17 Specific Features

  • Java 17 brought several new features such as sealed classes, pattern matching for switch, and more. Make sure your code uses features specific to Java 17 to fully utilize it.

Common Troubleshooting

  1. 'javac' is not recognized as an internal or external command:
    • Ensure Java is added to your system’s PATH environment variable. Refer to your operating system’s documentation to add the Java bin directory to the PATH.
  2. Specify Java Version (if multiple versions are installed):
    • Use the full path to the desired Java version for compilation and execution:
/path/to/java17/bin/javac HelloWorld.java
/path/to/java17/bin/java HelloWorld

With these steps, your Java 17 code should successfully compile and run from the command line.

How to use helpful NullPointerExceptions in Java 17

In Java 14, along with the -XX:+ShowCodeDetailsInExceptionMessages feature, Helpful NullPointerExceptions were introduced. This feature provides detailed and precise messages when a NullPointerException (NPE) occurs. It is available starting from Java 14 as a preview feature and was enabled by default (no longer requiring the JVM flag) starting with Java 16. This behavior continues in Java 17.

These enhancements tell you exactly which object reference was null, making debugging easier compared to the default NPE messages.


Steps to Use Helpful NullPointerExceptions in Java 17

  1. Ensure Java 17 is Installed
    • Verify that the installed JDK version is Java 17 or newer. Use:
    java -version
    
  2. By Default, It’s Enabled
    • Starting from Java 16, Helpful NullPointerExceptions are enabled by default, so no additional JVM flag or setup is required.
  3. Run Your Application
    • If your code throws a NullPointerException, the detailed message will be generated.
  4. How It Works
    • When a NullPointerException is thrown, the JVM will now include details in the exception’s message about the null reference that caused the problem.

Example

Code Example

package org.kodejava.basic;

public class NullPointerDemo {
   public static void main(String[] args) {
      String str = null;
      System.out.println(str.toLowerCase()); // Will throw a NullPointerException
   }
}

Output

Exception in thread "main" java.lang.NullPointerException:
Cannot invoke "String.toLowerCase()" because "str" is null

If you use field/method chaining, the message will identify exactly which part caused the NPE.

Example with Field Access

class Person { 
    Address address; 
}

class Address { 
    String city; 
}

public class HelpfulNPEExample { 
    public static void main(String[] args) { 
        Person person = new Person(); 
        System.out.println(person.address.city); // Accessing null property
    }
}

Detailed Output

Exception in thread "main" java.lang.NullPointerException: 
Cannot read field "city" because "person.address" is null

Enabling or Disabling (Optional)

Helpful NullPointerExceptions can be disabled using the following JVM argument:

-XX:-ShowCodeDetailsInExceptionMessages

To enable explicitly (though it’s enabled by default in Java 17+):

-XX:+ShowCodeDetailsInExceptionMessages

Add this argument when running your application:

java -XX:+ShowCodeDetailsInExceptionMessages YourMainClass

Benefits of Helpful NullPointerExceptions

  1. Faster Debugging: You no longer need to search manually for which variable or reference is null.
  2. Enhanced Error Information: Pinpoints the exact null reference, which is especially useful in complex codebases.
  3. Productivity Increase: Saves time during troubleshooting and debugging.

Java 17 users benefit from this feature out-of-the-box, making it a significant enhancement for clean and error-free development.

How to create records in Java 17 for immutable data models

In Java 17, you can use the record feature to create immutable data models. Records are a new type of class in Java designed specifically to hold immutable data. Using records simplifies creating classes that are essentially data carriers. Here’s a step-by-step guide on how to create and use records in Java 17:

What is a Record?

A record is a special kind of class in Java introduced in Java 14 (as a preview) and became stable in Java 16+. It:

  • Is designed for immutability
  • Automatically generates boilerplate code like getters, equals(), hashCode(), and toString()

Syntax of a Record

Declaring a record is simple. Here’s the syntax:

public record RecordName(datatype field1, datatype field2, ...) {}

Key Features of Records

  1. Records automatically:
    • Generate getter methods for fields (no need for get prefix – field name itself is used).
    • Override toString(), hashCode(), and equals().
  2. Records are immutable (fields cannot be changed after initialization).

  3. Records can include custom methods.
  4. Records cannot extend other classes (inheritance is not allowed) but can implement interfaces.

An Example: Immutable Data Model with Records

package org.kodejava.basic;

public record Person(String name, int age) {
    // Custom constructor (optional)
    public Person {
        if (age < 0) {
            throw new IllegalArgumentException("Age cannot be negative");
        }
    }

    // Example of adding a custom method
    public String greet() {
        return "Hello, my name is " + name + " and I am " + age + " years old.";
    }
}

How to Use Records

You use a record just like any other class:

package org.kodejava.basic;

public class Main {
    public static void main(String[] args) {
        // Create a record instance
        Person person = new Person("John Doe", 30);

        // Access fields using getters
        System.out.println("Name: " + person.name());
        System.out.println("Age: " + person.age());

        // Use a custom method
        System.out.println(person.greet());

        // Immutability tested
        // person.name = "New Name"; // Compilation error because fields are final
    }
}

Output:

Name: John Doe
Age: 30
Hello, my name is John Doe and I am 30 years old.

Advantages of Using Records

  1. Less Boilerplate Code: You don’t need to write getters, setters, constructors, or methods like toString() and hashCode().
  2. Thread-Safety: Records are immutable, making them easy to use in concurrent environments.
  3. Better Readability: The succinct syntax improves code readability.

Restrictions of Records

  1. Records are final — you cannot extend them.
  2. Fields in a record are also final and cannot be changed.
  3. Records themselves cannot be mutable.

When Should You Use Records?

You should use records when:

  • You need a simple data model to hold immutable data.
  • You want to avoid the verbosity of writing boilerplate code for fields and methods (getters, toString(), etc.).

For mutable data, traditional classes or other patterns should be used instead of records.