How do I set up JAVA_HOME and Path variables in Windows?

Setting up a JAVA_HOME and Path variables is the second thing you’ll need to do after installing a JDK (Java Development Kit). Although this is not required by Java itself, it is commonly use by other application. For instance then Apache Tomcat web application server and other application server will need it. Or we might need it if we want to compile or running our Java classes from the command prompt. It helps us to organize the default JDK and the execution path.

So here are the steps that we’ll need to do to configure the JAVA_HOME and Path variable on a Windows operating system.

Step 1. Finding the location of our JDK installation directory. If we already know where we have installed the JDK continue to the Step 2.

  1. The JDK usually installed in the C:\Program Files\Java directory by default.
  2. Under this directory we can find one or more versions of installed JDK, for examples I have jdk-14 and jdk-17. Just choose the default one we’re going to use.

Step 2. Setting JAVA_HOME variable

After we know the location of your JDK installation, we can copy the directory location from the Windows Explorer address bar.

  1. Open Windows Explorer
  2. Right-Click the Computer and select the Properties menu.
  3. Click Advanced system settings and the System Properties windows will be shown.
  4. Select the Advance tab.
  5. Click the Environment Variables button.
  6. A new Environment Variables window will be shown.
  7. Under the System Variables, click the New button to create a new environment variable.
  8. Enter the variable name as JAVA_HOME, all letters are in uppercase.
  9. In the variable value enter the JDK installation path you’ve copy above.
  10. Click OK.

Step 3. Setting the Path variable

After we’ve set the JAVA_HOME variable, now we can update the Path variable.

  1. In the Environment Variables window, under the System Variables section find a variable named Path.
  2. If we don’t have the Path variable we need to add one using the New button.
  3. If we already have the Path variable we’ll need to update its value, click Edit button to update.
  4. Add %JAVA_HOME%\bin; to the beginning of the Path variable value.
  5. Press OK to when we are done.
  6. Press another OK to close the Environment Variables window.

Step 4. Check to see if the settings work

  1. Open your Windows Command Prompt.
  2. Type java -version in the command line.
  3. If everything was set correctly we’ll see the running version of your installed Java JDK.

As an example on my Windows Command Prompt I have something like:

D:\>java -version
java version "17" 2021-09-14 LTS
Java(TM) SE Runtime Environment (build 17+35-LTS-2724)
Java HotSpot(TM) 64-Bit Server VM (build 17+35-LTS-2724, mixed mode, sharing)

If you don’t see the correct output, for instance you get an error like “‘java’ is not recognized as an internal or external command, operable program or batch file.”, please retry the steps described above. Enjoy your new adventure with Java programming. Happy coding!

How to use underscore in numeric literals?

Writing a long sequence of numbers in a code is a hard stuff to read. In the new feature introduced by JDK 7 we are now allowed to write numeric literals using the underscore character to break the numbers to make it easier to read.

You can see how to use underscore in numeric literals in the following examples. And you’ll see it for yourself that it really makes numbers easier to read.

package org.kodejava.basic;

public class UnderscoreNumericExample {
    public static void main(String[] args) {
        // Write numeric literals using underscore as an easier way
        // to read long numbers.
        int maxInt = 2_147_483_647;
        int minInt = -2_147_483_648;

        if (maxInt == Integer.MAX_VALUE) {
            System.out.println("maxInt = " + maxInt);
        }

        if (minInt == Integer.MIN_VALUE) {
            System.out.println("minInt = " + minInt);
        }

        // Write numbers in binary or hex literals using the
        // underscores.
        int maxIntBinary = 0B111_1111_1111_1111_1111_1111_1111_1111;
        int maxIntHex = 0X7____F____F____F____F____F____F____F;

        System.out.println("maxIntBinary = " + maxIntBinary);
        System.out.println("maxIntHex    = " + maxIntHex);
    }
}

The results of the code snippet:

maxInt = 2147483647
minInt = -2147483648
maxIntBinary = 2147483647
maxIntHex    = 2147483647

How do I define an integer constant in binary format?

The JDK 7 add a small feature to work with a binary number. In the previous JDK we have to use the Integer.parseInt() method if we need to work with other base number. But with this new feature introduced in the Project Coin we can simplify the code when we work with the binary number.

To specify a binary literal in the code, add the prefix 0b or 0B to the number. The following code snippet show you how to write the binary literals:

package org.kodejava.basic;

public class BinaryLiteralExample {
    public static void main(String[] args) {
        // In JDK 6 and the previous version you must use the
        // Integer.parseInt() method to define a number using
        // a binary literal.
        int x = Integer.parseInt("00101010", 2);
        System.out.println("x = " + x);

        // In the new JDK 7 you can simply use the following
        // binary literal to define a number using a binary
        // literal.
        int y = 0b00101010;
        System.out.println("y = " + y);
    }
}

The result of our code snippet:

x = 42
y = 42

How do I use the diamond syntax?

In Java 7 a new feature called diamond syntax or diamond operator was introduced. This diamond syntax <> simplify how we instantiate generic type variables. In the previous version of Java when declaring and instantiating generic types we’ll do it like the snippet below:

List<String> names = new ArrayList<String>();
Map<String, List<Integer>> map = new HashMap<String, List<Integer>>();

As you can see in the snippet, we were repeating our self by defining the generic type two times. We define the object type we’ll be stored in the List and the Map on both left and the right side. By using the diamond syntax the compiler will infer the type of the right side expression argument automatically. So in Java 7 we can write the above code snippet like this:

List<String> names = new ArrayList<>();
Map<String, List<Integer>> map = new HashMap<>();

This make our code simpler and more readable, and by using the diamond syntax the compiler will ensure that we have the generic type-safe checking available in our code. This will make any error due to type incompatibility captured at compile time.

How do I pick a random value from an enum?

The following code snippet will show you how to pick a random value from an enum. First we’ll create an enum called BaseColor which will have three valid value. These values are Red, Green and Blue.

To allow us to get random value of this BaseColor enum we define a getRandomColor() method in the enum. This method use the java.util.Random to create a random value. This random value then will be used to pick a random value from the enum.

Let’s see the code snippet below:

package org.kodejava.basic;

import java.util.Random;

public class EnumGetRandomValueExample {
    public static void main(String[] args) {
        // Pick a random BaseColor for 10 times.
        for (int i = 0; i < 10; i++) {
            System.out.printf("color[%d] = %s%n", i,
                    BaseColor.getRandomColor());
        }
    }

    /**
     * BaseColor enum.
     */
    private enum BaseColor {
        Red,
        Green,
        Blue;

        /**
         * Pick a random value of the BaseColor enum.
         *
         * @return a random BaseColor.
         */
        public static BaseColor getRandomColor() {
            Random random = new Random();
            return values()[random.nextInt(values().length)];
        }
    }
}

The output of the code snippet:

color[0] = Green
color[1] = Green
color[2] = Blue
color[3] = Red
color[4] = Blue
color[5] = Blue
color[6] = Blue
color[7] = Blue
color[8] = Green
color[9] = Blue