Java Official Notes: Writing and Running Java Programs

WBOY
Release: 2023-04-12 08:49:34
forward
1363 people have browsed it

You may be eager to install Java, write a Java program and run it. But before that, there are some concepts that need to be understood in advance, because Java is a little different from C, C and Python.

Compilation and Execution​

We write English codes in text files. Computers cannot understand these English codes, so we need to do some conversion to a format that computers can recognize and run. This conversion is completed by the "compiler". Some languages ​​do not have compilers, but Java does.

The file converted by the compiler is usually called a binary file, or executable file. The file content is changed from English to bytecode. Only computers can understand bytecode, not humans, so we don't need to care. We just need to ensure that the English code we write can be compiled correctly. Let the computer do the execution.

Java code files end with .java​, and Java executable files end with .class.

Create Java class​

All code in the Java file must be placed in "Java Class":

public class MyFirstClass {
}
Copy after login

The file name must be called MyFirstClass.java, the file name It must be consistent with the class name. Why is Java designed to be so strict? Convenient, it allows you to know what the class name is when you see the file name, without having to guess.

Installing JDK​

How to compile class files? Download Java.

Downloading Java means downloading JDK, Java Development Kit, which contains a Java compiler to compile MyFirstClass.java into MyFirstClass.class.

The so-called JRE, Java Runtime Environment, is part of the JDK and can only be used to run Java programs and cannot be used to compile.

Download address: https://jdk.java.net/

After installation, you need to set the environment variables JAVA_HOME and PATH according to the operating system (Windows, Linux, macOS).

Verify that the installation is successful:

java -version
Copy after login

Compile Class​

Use the javac command to compile:

javac MyFirstClass.java
Copy after login

If there is a problem with the code, an error will appear. If there is no problem, the MyFirstClass.class file will be generated.

Run HelloWorld​

Add code:

public class MyFirstClass {

public static void main(String... args) {
System.out.println("Hello, World!");
}
}
Copy after login

Recompile, ensure that the class file is generated, use the java command to run:

java MyFirstClass
Copy after login

Run successfully, control The station will print a sentence: Hello, World!

FAQ

Question 1: javac HelloWorldApp, error:

Class names, 'HelloWorldApp', are only accepted if annotation processing is explicitly requested
Copy after login

javac HelloWorldApp.java The compilation command "javac" should be followed followed by "file name".

Question 2: java HelloWorldApp.class, error:

Could not find or load main class HelloWorldApp.class
Copy after login

java HelloWorldApp The running command "java" should be followed by "class name".

You can command Help to see the difference:

javac Usage: javac <options> <source files>
Copy after login
java Usage: java [-options] class [args...]
 (to execute a class)
 orjava [-options] -jar jarfile [args...]
 (to execute a jar file)
Copy after login

When developing complex applications, javac and java commands are not used directly, but IDE, Integrated Development Environment, integrated development environment, such as Eclipse, NetBeans and IntelliJ IDEA.

Reference: Getting Started with Java https://dev.java/learn/getting-started

The above is the detailed content of Java Official Notes: Writing and Running Java Programs. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:51cto.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template