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
- Download Java 10 JDK:
- Visit Oracle’s Java Archive.
- Download the Java 10 JDK installer suitable for your operating system.
- 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
).
- Set up the Environment Variables:
- Add the JDK bin directory to the
PATH
variable:- Go to System Properties → Advanced → Environment Variables.
- Under System Variables, find the
Path
variable and add the JDK’sbin
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.
- Open the command prompt or terminal and type:
- Add the JDK bin directory to the
- 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
- Write the Java Program:
Create a file namedHelloWorld.java
with the following content:public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World! Welcome to Java 10!"); } }
- Compile Your Program:
From the command prompt, navigate to the directory containingHelloWorld.java
, and run:javac HelloWorld.java
This will create a compiled
HelloWorld.class
file. -
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!
- How do I secure servlets with declarative security in web.xml - April 24, 2025
- How do I handle file uploads using Jakarta Servlet 6.0+? - April 23, 2025
- How do I serve static files through a Jakarta Servlet? - April 23, 2025