preface

Super detailed in the last article! JDK 8 Download, installation and environment configuration (macOS and Windows versions), we have set up a Java development environment, this article is to write a Java application, the program can see Hello World output.

Let’s also analyze what’s going on from coding to running!

Encode Hello World

We don’t use any integrated development environment, we use the most primitive handwritten way to code!

When you write on a computer, you type code into a notepad.

Windows users can open Notepad directly, and macOS users can type the following command on the console to open Notepad:

open -a TextEdit
Copy the code

Here is the minimalist code that prints the Hello World application. According to knock on the line, don’t care about the meaning of each code, will talk about later.

public class HelloWorld{

	public static void main(String[] args){

		System.out.println("Hello, World!!!"); }}Copy the code

The save file is named helloworld.java, which means it is a Java source file.

Compile source file

Java files are only source files and cannot be executed. They must be converted to bytecode files, called.class files, to be executed. The process of this transformation is compilation.

The compile instruction is javac, and C is the first letter of compile.

The helloWorld.java file will be compiled by executing the following command, and the helloWorld.class file will be generated in the same directory.

javac HelloWorld.java
Copy the code

Execute the bytecode file

The.class file is generated, and the main function inside the file indicates that there is an entry point for program execution, which means that the Hello World program has been written and is ready to run.

So how do I run this Java program?

It is easy to use the Java command, the command parameter is the class name, as follows:

java HelloWorld
Copy the code

Now comes the exciting moment! We saw the console printHello, World!!!Then the program exits.

This means that we have successfully written our first Java application and it is running!

So how does this happen? Let’s analyze it.

Source code analysis

First we look at the source code, the source code is actually quite concise.

public class HelloWorld{
    public static void main(String[] args){
        System.out.println("Hello, World!!!"); }}Copy the code

There are three Java keywords: public, class, and static.

Public is the access modifier in Java syntax, which we’ll cover in detail later, but briefly mentioned here.

The siblings of public, protected, default, and private, determine how widely classes, objects, methods, and properties in the Java world can be accessed, whether it’s the current class or the current package. Or unrestricted access to everything.

To put it simply, it is a kind of permission control, similar to when you post moments, you can choose to make it completely public, you can choose to make it visible to some people, or you can choose to make it visible only to yourself.

Back in the source code, we see that public modifies the HelloWorld class and the main method. It is also public that allows us to execute programs without being blocked.

Class indicates that the current source file is a common class. This keyword is the most commonly used, and its siblings include **interface ** and enum.

The HelloWorld after class is the name given to the current class when coding. According to the naming convention, a class name is a combination of letters and numbers and must begin with a capital letter.

The curly braces ({}) after the class name are the definition of the class.

Static is a static modifier that indicates that the modified content can be referenced directly by the current class without instantiating the class.

The main method is modified here to indicate that the main method can be referenced directly by HelloWorld.

Void is the return value of the main method, where there is no return value.

**String[] ** indicates that this is an array of strings, and args represents variables.

What it does is, when you start a Java application, you can pass in an array of strings to customize the application’s initialization properties. For example, when you execute the Java HelloWorld directive, you can pass in the parameters you need.

The curly braces ({}) following the method declaration are the definition of the method.

Method with ** for each line of code; ** end, there is only one line of code, which is:

System.out.println("Hello, World!!!");
Copy the code

System.out is a static print stream object provided by Java, through which print-related operations can be carried out. On this basis, the println method can be called to output the specified information in the console.

Let’s take a look at what we did with the source code.

Execution path Analysis

First, Java source code is compiled with javac command in JDK to generate Java bytecode, that is, class file.

The javac command can be found in the bin directory of the JDK home directory.

When a class file is executed using the Java command of the JRE, the Java bytecode is transferred to the JVM(Java Virtual Machine), which executes the bytecode along with the JRE library files, and outputs the machine code, or instruction set, for the specific hardware platform.

The machine code is executed by the underlying physical hardware platform.

Through this process, we will also find that in order to achieve cross-platform features such as Write Once and Run Anywhere, class files must be executed on any platform. However, the instruction set received by different hardware platforms varies greatly. There needs to be something that ADAPTS to different operating system platforms, and the JVM is there to do that.

Therefore, it is the JVM’s non-cross-platform nature that makes the Java language cross-platform.

Like the source code of HelloWorld in this article, it is compiled to generate HelloWorld.class, and then I can execute it on macOs using Java HelloWorld.

At this point, I copy HelloWorld. Class to Linux or Windows and use Java HelloWorld to get the same results as macOs.

In fact, if I copy the helloWorld.java file into a Linux environment, the same class file generated by Javac is also the same.

Combined with the following figure, you can see why JDK downloads are operating system specific.

conclusion

This paper learned how to write Java code and how to run Java programs by using HelloWorld as a minimalist Java program. At the same time, it also analyzed the relevant syntax and norms involved in Java source code. Finally, from the perspective of how all this happened, From the analysis of the source code to the entire execution path, in the process of analysis, also help us a deeper understanding of Java cross-platform characteristics.

With a preliminary understanding and perception of Java, learning some concepts and syntax of Java will be relatively handy!