Home >Java >javaTutorial >Simple and easy-to-understand Java installation guide, suitable for novices to learn and use
Java Installation Tutorial: A concise guide for beginners who require specific code examples
Introduction:
Java is a cross-platform, high-level programming language , widely used in various software development fields. For beginners, installing Java is a big step towards learning programming. This article will provide a concise and concise Java installation tutorial for beginners, with specific code examples to help readers easily complete Java installation.
Step 1: Download Java Development Kit (JDK)
First, we need to download and install Java Development Kit (JDK). JDK is Java's core development toolkit, including Java compilers, debuggers, and other tools for developing Java applications.
1. Open the Oracle official website (https://www.oracle.com/java/technologies/javase-jdk11-downloads.html) in the browser.
2. Find the JDK version suitable for your operating system on the web page and click to download.
3. Install the downloaded JDK file. Depending on the operating system, the installation method is also different. Please follow the specific prompts to complete the installation.
Step 2: Configure environment variables
After the installation is completed, we need to configure Java environment variables. Environment variables tell the operating system where to find the Java executable file.
1. Search for "Environment Variables" in the Start menu and click "Edit System Environment Variables".
2. In the pop-up dialog box, click the "Environment Variables" button.
3. In the system variables, find the "Path" variable and click "Edit".
4. In the pop-up dialog box, click "New" and enter the installation path of Java (for example: C:Program FilesJavajdk11.0.1 in).
5. Click "OK" to save changes.
Step 3: Verify installation
After the installation is completed, we need to verify whether Java is successfully installed.
1. Open the command prompt (Windows: use the shortcut key Win R, enter "cmd" and press Enter; Mac: search for "Terminal" through Spotlight).
2. Enter "java -version" in the command prompt and press Enter.
3. If the system displays Java version information, it means that Java installation is successful.
Code Example 1: Hello World
Now, let us test the first Java program-Hello World.
Edit a Java source file, name it "HelloWorld.java", and enter the following code:
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } }
Save the file and enter the directory where the file is saved through the command prompt.
Enter "javac HelloWorld.java" in the command prompt and press Enter. This will compile the source files and generate a bytecode file named "HelloWorld.class".
Enter "java HelloWorld" in the command prompt again and press Enter. If the system successfully displays "Hello World!", it means that the program runs successfully.
Code Example 2: Calculator Program
Let us further learn Java syntax and write a simple calculator program.
Edit a Java source file, name it "Calculator.java", and enter the following code:
import java.util.Scanner; public class Calculator { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入第一个数字:"); int num1 = scanner.nextInt(); System.out.print("请输入第二个数字:"); int num2 = scanner.nextInt(); System.out.println("两个数字的和为:" + (num1 + num2)); } }
Save the file and enter the directory where the saved file is located through the command prompt.
Enter "javac Calculator.java" in the command prompt and press Enter. This will compile the source file and generate a bytecode file called "Calculator.class".
Enter "java Calculator" in the command prompt again and press Enter. The program will prompt you to enter two numbers and calculate their sum.
Summary:
Through this concise Java installation tutorial, we learned the steps to download and install the Java Development Kit (JDK), and how to configure Java environment variables. We also provide two specific code examples, namely a Hello World program and a simple calculator program. Through these examples, we can have a basic understanding of Java's syntax and operation methods, laying a solid foundation for subsequent programming learning.
Reference source:
The above is the detailed content of Simple and easy-to-understand Java installation guide, suitable for novices to learn and use. For more information, please follow other related articles on the PHP Chinese website!