How to Set Up Java 10 and Compile Your First Program

To set up Java 10 and compile your first program, follow these steps. Additionally, you’ll learn about Java 10’s var keyword feature once your setup is complete.


Steps to Set Up Java 10

  1. Download Java 10 JDK:
  2. Install Java 10:
    • Follow the installation wizard to install the JDK.
    • Don’t forget to note the installation path (e.g., C:\Program Files\Java\jdk-10).
  3. Set up the Environment Variables:
    • Add the JDK bin directory to the PATH variable:
      1. Go to System PropertiesAdvancedEnvironment Variables.
      2. Under System Variables, find the Path variable and add the JDK’s bin directory (e.g., C:\Program Files\Java\jdk-10\bin).
    • Verify the setup:
      • Open the command prompt or terminal and type:
        text
        java -version

        You should see the version as Java 10.
  4. Install an IDE or Use a Text Editor:
    • Download and install an IDE like IntelliJ IDEA, Eclipse, or Visual Studio Code (or use a simple text editor).

Compile and Run Your First Java Program

  1. Write the Java Program:
    Create a file named HelloWorld.java with the following content:

    public class HelloWorld {
       public static void main(String[] args) {
           System.out.println("Hello, World! Welcome to Java 10!");
       }
    }
    
  2. Compile Your Program:
    From the command prompt, navigate to the directory containing HelloWorld.java, and run:

    javac HelloWorld.java
    

    This will create a compiled HelloWorld.class file.

  3. Run Your Program:
    Execute the compiled program using:

    java HelloWorld
    

    You should see the output:

    Hello, World! Welcome to Java 10!
    

Using the var Keyword in Java 10

To explore Java 10’s var keyword for local variable type inference, you can enhance your program. Update the HelloWorld class as follows:

package org.kodejava.basic;

import java.util.List;

public class HelloWorld {
   public static void main(String[] args) {
      var message = "Hello, Java 10!";
      var numbers = List.of(1, 2, 3);

      System.out.println(message);
      for (var number : numbers) {
         System.out.println("Number: " + number); // Using 'var' in loop
      }
   }
}
  • Save the changes as HelloWorld.java.
  • Recompile and run using the steps above.

You’ll see the output:

Hello, Java 10!
Number: 1
Number: 2
Number: 3

The var keyword simplifies variable declarations without compromising type safety, ensuring better code readability and brevity.


Conclusion

You’ve successfully set up Java 10, compiled, and executed your first program. Additionally, you explored how to use Java 10’s var keyword for type inference. Keep experimenting with these features to leverage Java 10’s capabilities!

Wayan

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.