preface

C: in the last, we set up the Java language development environment, this article find teacher with pleasant to write our first program, but this is we use notepad (original) to write programs that do not use other advanced development tools, so may appear relatively low-end, please forgive me.

Food should be eaten bit by bit, tools should be upgraded bit by bit, and skills should be accumulated bit by bit.

A series of reading

Zha teacher said: although zha teacher heart you are the best, but if you did not read the last article, or suggested to open a quick string, in order to prevent the following content can not maintain continuity.

  • The Java syntax overview | Java”
  • The Java syntax | development environment set up”
  • The Java syntax | first Java program”

Development steps

A Java program development, need to go through: compile source code, compile source code and run, these three stages.

Experience it with Teacher Zha! Source code/source program: The original instructions of a program, written by the programmer.

1. Use Notepad to write the source code file (the extension of the source code file of the Java program is.java).

2. Open the CMD command window, go to the source code location, and run the javac command to compile the.class file.

Teacher cha said: finished writing source code, the computer is not directly executed, because it only identifies binary, so we need a translation officer to help us to translate, so that the computer can recognize. The translator is the compiler, and when compiled, a bytecode file with the.class extension (which is not a pure binary, but a pseudo-binary that runs in the JVM) is produced that can be executed by the computer.

3. Run the. Class file using the Java command.

You cannot run.class files with the.class suffix.

That’s how we implemented our first Java program, which gave the computer a console output of Hello World!! The instructions.

Why did the first program we started writing output the sentence Hello World?

In fact, The C Programming Language book famously used it for The first demonstration program, so subsequent programmers continued The habit when learning Programming or debugging devices.

A programmer only understand the cold joke: a senior programmer to the retirement age, in order to add some fun to his later life, began to learn calligraphy, on the first day of learning calligraphy, the senior programmer spread out a piece of paper, a big stroke of Hello World!

Basic program structure

Just now we wrote our first Java program, and although we memorized the relevant words, we were definitely confused. Teacher zha first to give you a reminder, now do not require you to understand their meaning too much, just need to know its effect and matters needing attention. With the deepening of learning, slowly will be in-depth understanding and understanding, do not bog to a dead end, because have not told you certainly because of the teacher think is not too suitable for you, haste makes waste.

Here are the DOS and don ‘ts you need to remember now:

  1. The class name is exactly the same as the file name, with a capital letter (PASCAL/big hump nomenclature).

  2. The main() method is the entry point to the program, and four elements are necessary.

  3. System.out.println(); system.out.println (); Output information from the console. S is uppercase. (Java is case sensitive)

  4. {and} one corresponds to each other. (Pay attention to imitate Teacher Cha’s writing habits)

  5. Note that there is a hierarchy of indentation and only one statement on a line.

Teacher Zha said: “good code should be for people, not for machines.” The above 5 matters for attention, in fact, 4 are emphasizing the specification, coding specification is very important!

Maintenance accounts for 80% of the cost of a software life cycle. Almost no piece of software is maintained by its original developers throughout its life cycle. Coding specifications improve the readability of software and allow programmers to understand new code quickly and thoroughly. In addition to imitating teacher Zha’s article sample habits, please follow up with teacher Zha to interpret the “Alibaba Java Development Manual”.

Output statements

Not only println() but also print() without ln.

System.out.print(); // Output without a line break
System.out.println(); // Output a newline
Copy the code

If you want print() to behave like println(), you can use escapes.

Escape character instructions
\n Move the cursor to the first row of the next line
\t Move the cursor to the next horizontal TAB position
// The first two lines below are equivalent to the third.
System.out.println("Hello");
System.out.println("World");
System.out.print("Hello \n World\n");
Copy the code

annotation

In the Java preparation process we need to annotate some procedures, so that in addition to their own more convenient to read, others also better understand our procedures, so we must timely add some annotations, can be programming ideas or the role of the program.

There are three types of annotations in Java:

  1. A single line comment//Start;
  2. Multi-line comments to/ *At the beginning,* /At the end.
  3. JavaDoc (document comments) comments to/ * *At the beginning,* /End (Document annotations, understand in the early stage, to master in the later stage)
/** * helloworld.java * the first Java program */
public class HelloWorld{
    public static void main(String[ ] args){
        // Output the message to the console (note the addition of a space after a single line comment, this is a small specification)
        System.out.println("Hello World!!!"); }}Copy the code

Cross-platform principle

In the Java syntax | Java overview “, check the ability of teacher speak Java was buried a foreshadowing, Java program is a cross-platform, what is a cross-platform? Search teacher give you an example of a non-cross-platform program, the following is baidu network disk client program download page, it in order to be able to run on different platforms (systems), developed 6 sets of systems (in fact, 7 sets, but also a web version). You can see why cross-platform is such a great capability.

After writing the first Java program, let’s think about why Java can be cross-platform. The reason can be found in the development steps. Compile! Compilation turns source code into bytecode files (pseudo-binaries) that will be run on the JVM (Java Virtual Machine) in the future. In other words, Java can be cross-platform because the JVM can be installed cross-platform.

As a result, Java programmers don’t have to worry about where they’re going to run their programs, they’re going to run them on the JVM, and the JVM is going to take care of translating them into the machine language of the platform. Many of the best programs that have followed have adopted this idea.

The Java virtual machine has its own complete hardware architecture, such as processor, stack, and corresponding instruction system.

A Java virtual machine is essentially a program that, when launched on the command line, begins executing instructions stored in a bytecode file. The portability of the Java language is based on the Java virtual machine. Bytecode files (.class) can run on any platform that has a Java virtual machine specific to that platform. This is “compile once, run many times”.

Java virtual machine is not only a cross-platform software, but also a new network computing platform. The platform includes many related technologies, such as various apis that conform to open interface standards, optimization techniques, and so on. Java technology enables the same application to run on different platforms. The Java platform can be divided into two parts, namely the Java Virtual Machine (JVM) and the Java API class library. [1]

For now, this level of understanding is enough. In-depth JVM learning is an important step in the future to improve “Java internal power “, but it is not suitable for you as a beginner.

Compile and decompile

From what we’ve just learned, we know that compilation converts source code files into bytecode files, which we’ll need to run later. If one day, your boss give you a heap of bytecode file, let you to reference the content (source code), of course, you know the bytecode is a pile of garbage characters in the file, so as to, we need it to convert a source code file, this is decompiled, we can use tools to faster, better batch processing.

Compilation: The process of converting a source file (.java) into a bytecode file (.class) is called compilation.

Decompilation: The process of converting a bytecode file (.class) back to the source file (.java) is called decompilation. (Commonly used Jad, FrontEnd, JD-GUI)

This is just a demonstration of decomcompiling HelloWorld.class using Jad. Similar tools include JD-GUI, etc.

reference

[1] gu wei. Analysis and research of Java virtual machine [J]. Office automation,2017,22(9):35-36,11

Afterword.

Well, the introduction of the first Java program is over here, this homework please search wechat attention: check the teacher’s handout, and then reply to the first Java program homework.

How do you feel about this one today? Is the concept clear? Did you remember the grammar? All things are difficult before they are easy. Come on, classmates!

Teacher Zha said: For the learning of technology, teacher Zha has always followed the following steps: With a simple demo to let it run first, and then learn it the most commonly used API and configuration can let yourself up, finally, on the basis of proficiency in the spare time reading the source to try to make myself to be able to see clearly its running mechanism, part of the cause of the problem, at the same time, draw lessons from these technology to enhance your own code level.

So in the teacher’s article, the early basic are small white, only interspersed with a very small amount of source research. Of course, such as the small white update, you still like, the late will not regularly dedicated to part of the technology of the source code analysis.