Java first acquaintance with Java

Java is an excellent object-oriented programming language. There are a series of technical systems formed by computer software and specifications, which are widely used in embedded systems, mobile terminals and other occasions. At the same time, Java is widely used in the IT industry, such as enterprise-level systems, web development, and android platform applications. Below, briefly introduce the father of Java, Java language features, and how to write the first program in Java. The specific content will be described in detail later.

A brief introduction to Java

1. History of Java

    Java is the English name of the Indonesian island of Java, famous for its coffee production. In 1991, Dr. James Gosling of Sun Corporation (now acquired by Oracle Corporation) started a project to develop a programming architecture that would run on a variety of consumer electronics products, in short, smart appliances. He named this program architecture Oak, the predecessor of Java. But these smart appliances didn't meet expectations, so Sun dropped the plan. With the development of the Internet in 1995, Sun Corporation saw the prospect of Oak's application on the Internet, so it transformed Oak, which was officially released under the name of Java in May 1995, and put forward the slogan of "Write once, Run anywhere". The history of the post-order here is not exhaustive, and those who are interested can Baidu by themselves.

James Gosling was born in Canada in 1955

2. The language features of Java

The Java white paper says:

1. Simplicity. The Java syntax is a "clean version" of the C++ syntax, which is equivalent to a subtraction from C++ (eg, no pointers). Not only that, but the Java development environment far exceeds that of most other programming languages.

2. Face the object. What is object orientation? In the Java world, everything is an object. For example: people, dogs, mobile phones, computers, etc. are all objects. The so-called object-oriented is to rely on the interaction between objects to complete things, such as: people use mobile phones to shop online, dogs eat bones... Java's object-oriented features are comparable to C++, and the main difference with C++ is multiple inheritance.

3. Distributed (microservices). Java has a rich library of routines for handling TCP/IP protocols like HTTP and FTP.

4. Robustness. The biggest difference between Java and C++ is that the pointer model adopted by Java eliminates the possibility of overwriting memory and corrupting data.

5. Security. Java is suitable for network/distributed environments. To achieve this, a lot of effort has been put into security. Antivirus and tamper-proof systems can be built using Java.

6. Architecture neutral. The compiler generates an architecture-neutral object file format, and the files generated according to the specification in this document, as long as there is a Java runtime system, the compiled code can be run on many processors.

7. Portability. Unlike C/C++, there is no "implementation-dependent place" in the Java specification. The size of the basic data types and related operations are clearly stated.

8. Interpretation. In order to be independent of the platform, Java maintains a set of instructions based on the stack architecture. After the Java source code is compiled, the instructions in the bytecode file are organized according to its own instruction set, but it runs in a specific hardware environment. When the Java program is executed, the Java interpreter will translate the instructions in the bytecode file into the instruction set of the CPU one by one.

9. High performance. Interpretation and execution, garbage collection, etc. lead to the low efficiency of Java code, but in recent years, the JVM has been continuously optimized, and the efficiency is no less than that of C/C++ in some cases.

10. Multithreading. It was the first mainstream language to support concurrent programming. Multithreading can bring better interactive response and real-time behavior.

11. Dynamics. Java is more dynamic than C/C++. It is able to adapt to an evolving environment.

Java is not only a programming language, but also a technical system consisting of a series of computer software and specifications.

Second, the first Java code

1,“hello world”

    As mentioned earlier, java maintains a set of instruction sets based on the stack architecture. To run Java code, it is necessary to install the Java development environment. Install the JDK (Java Development Kit, or Java Development Kit) first. The JDK contains javac and java tools, and the Java program eventually runs in the JVM (Java Virtual Machine). After the development environment is installed correctly, you can write code. You can use notepad or idea to write code. In order to illustrate the entire running process of java, use a notepad such as sublime Text to write code.

First, create a new .txt file in the folder where the code is written. Also show its suffix.

 Change .txt to .java.

 The ij icon in front is caused by using the idea to open it, don't worry about it. Then use sublime Text to open the file.

 Let's see how to enter a "hello world".

Description: The following is just a brief description.

public: is the access modifier qualifier, and public is the public permission.

class: A keyword that defines a class. class is followed by the class name. If there is a public modification in front of this class, then the class name must be the same as the file name, otherwise an error will be reported. At the same time, the class name needs to be in the form of a big camel case, and the writing here is not recommended. Left parentheses do not wrap.

main: Like the C language, there needs to be a main method. A java program must have and have only one main method to run, otherwise it will report an error indicating that the main method cannot be found.

String[]: String represents a string type, and String[] represents an array of string type. args has no special meaning, just an array name. The parameters of the string array type in the main() method are the parameters of the java command. Running the main() method using the java command will input the parameters of the java command into the string array parameters of the Java main() method.

System.out: A simple understanding is an output stream, and the following println is the output and newline. Removing ln means no line break.

result:

 2. Run

How does the code written in Notepad work?

Be sure to save the code you write first. Press win+R again, type cmd and press Enter.

 Users is followed by the username. The default here is the C drive, find the drive where the code is written, such as the Java code I wrote on the E drive, enter e: (case-insensitive) to enter the e drive.

 Copy the file path where the Java code is written.

Enter the file path copied by cd in the black box and press Enter. If the environment etc. are correct, this is the result.

Most of the problems that arise are problems with the installation of the environment. Next, enter the javac file name.java and press Enter.

Go back to the folder where the code was written, and you will find an additional .class file. This is a bytecode file that holds binary data.

 Enter the java file name, press Enter, and you get the result.

 The above steps show the entire running process of the java code. The whole process of java code is to compile + run.

The implemented java code will run in the jvm.

The ones often encountered here are JDK, JRE, and JVM. Their relationship is:

The above javac, java instructions are provided by JDK.

javac: is a compile command that compiles java source files into .class bytecode files. java: is to run the bytecode file; the bytecode is interpreted and run by the java virtual machine. 

If you modify the java code, be sure to save it first, and then execute the above steps. If there is a problem and there is no step to the java command, most of the JDK environment is not configured properly. The error that occurs after the java command may be written in the wrong code, or it may contain a Chinese part.

public class Main {//OJ题中以Main作为主类。自己的代码需要和文件名相同

	public static void main(String[] args) {

		System.out.println("hello world");
	}
}

Save, run:

Found an error here. This is because Notepad is in UTF-8 encoding format, and the javac command is encoded in GBK format. The code tables of the two are not uniform. The solution is to add the -encoding utf-8 command after javac.

3. Notes

There are three types of annotations in java:

(1) Single-line comment: // content.

(2) Multi-line comments: /*Content*/.

(3) Documentation comments: /**Content*/. This comment is common in the description of methods and classes, and can be parsed by the javadoc tool, which can generate a set of program description documents in the form of web files.

Comments do not participate in the compilation run.

Annotation requirements:

(1) The content is accurate: the content of the comment should be consistent with the code, match, and be updated in time when the code is modified.

(2) Reasonable length: Notes should neither be too condensed nor long.

(3) Be positive: Do not include negative energy (such as leading SB, etc.) in the comments.

4. Identifiers and Keywords

Identifier: The name given by the user to a class name, method name, or variable in a program.

In java, identifiers can contain: letters, numbers, and underscores and $ symbols, and so on. However, identifiers cannot start with a number, nor can they be keywords, and are strictly case-sensitive.

In Java, it is better to use big camel case for class names (the first letter of each word is capitalized), and small camel case for method names and variable names (the first letter is lowercase, and the first letter of each word is capitalized) .

Keyword: An identifier with a special meaning. That is: keywords are defined in advance by the Java language, identifiers with special meanings, or reserved words. These are the same as the parts of the C language, such as int, void, etc. The matters needing attention are the same as the C language.

Some java keywords:

 

Guess you like

Origin blog.csdn.net/Naion/article/details/123302652