Overview of Java basic grammar and jdk and path environment variables

1. Java overview

1.1 The history of Java language development (understand)

Language: the way of communication between people

Computer language: a special language for information exchange and communication between people and computers

Java language is a computer language introduced by the US Sun company (Stanford University Network) in 1995

Father of Java: James Gosling (James Gosling)

In 2009, Sun was acquired by Oracle, so we can now visit the oracle official website: https://www.oracle.com

Currently, the JDK version used by our course: 11.0

1.2 Cross-platform principle of Java language (understanding)

Java programs are not run directly. The Java compiler compiles Java source programs into platform-independent bytecode files (class files), and then the Java virtual machine (JVM) interprets and executes the bytecode files. Therefore, under different operating systems, you only need to install different Java virtual machines to achieve cross-platform java programs.

1.3 JRE and JDK (memory)

JVM (Java Virtual Machine), Java Virtual Machine

JRE (Java Runtime Environment), Java runtime environment, contains the core class libraries of JVM and Java (Java API)

JDK (Java Development Kit) is called Java development tool, including JRE and development tools

Summary: We only need to install the JDK, which contains the Java operating environment and virtual machine.

1.4 Download and installation of JDK (application)

1.4.1 Download

Obtain the JDK through the official website

http://www.oracle.com

Note : For different operating systems, you need to download the corresponding version of the JDK.

For specific download steps, please refer to "JDK Download and Installation Instructions"

1.4.2 Installation

Stupid installation, just the next step. However, the default installation path is under C:\Program Files. For the convenience of unified management, it is recommended to modify the installation path and install all development-related software into one directory, for example: E:\develop.

Note : The installation path should not contain special characters such as Chinese or spaces (use a pure English directory).

For specific installation steps, please refer to "JDK Download and Installation Instructions"

1.4.3 Introduction to the JDK installation directory

Directory name Description
bin Various tool commands of the JDK are stored in this path. javac and java are placed in this directory.
conf The JDK related configuration files are stored in this path.
include Some platform-specific header files are stored in this path.
jmods Various modules of the JDK are stored in this path.
legal The authorization documents for each module of the JDK are stored in this path.
lib Some supplementary JAR packages of JDK tools are stored in this path.

2. The first demo program

2.1 Commonly used DOS commands (applications)

Before contacting the integrated development environment, we need to use the command line window to compile and run the java program, so we need to know some common DOS commands.

1. Open the command line window: win + r to open the run window, enter cmd, and press Enter.

2. Common commands and their functions

operating Description
Drive letter name: Drive letter switching. E: Press Enter, it means switch to E disk.
to you View the content under the current path.
cd directory Enter a single-level directory. cd content
cd … Go back to the upper level directory.
cd directory1\directory2... Enter the multi-level directory. cd itheima\JavaSE
cd \ Return to the drive letter directory.
cls Clear the screen.
exit Exit the command prompt window.

2.2 Configuration of Path environment variables (application)

2.2.1 Why configure environment variables

To develop Java programs, you need to use the development tools provided by the JDK (such as javac.exe, java.exe and other commands), and these tools are in the bin directory of the JDK installation directory. If you do not configure environment variables, then these commands can only be used here Execute under the directory. It is impossible for us to put all the java files in the bin directory of the JDK, so the function of configuring environment variables is to enable the java related commands in the bin directory to be used in any directory.

2.2.2 Steps to configure environment variables

For specific configuration steps, please refer to the "Java Environment Variable Configuration Instructions" document.

2.3 HelloWorld case (application)

The HelloWorld case refers to outputting the text "HelloWorld" on the computer screen. Various computer languages ​​are used to using this case as the first demonstration case.

2.3.1 Java program development and operation process

To develop a Java program, three steps are required: writing the program, compiling the program, and running the program.

2.3.2 Preparation of HelloWorld case

1. Create a new text document file and change the name to HelloWorld.java.

2. Open the HelloWorld.java file with Notepad and enter the content of the program.

public class HelloWorld {
    
    
	public static void main(String[] args) {
    
    
		System.out.println("HelloWorld");
	}
}

2.3.3 Compile and run the HelloWorld case

Save the file, open the command line window, switch the directory to the directory where the java file is located, compile the java file to generate the class file, and run the class file.

Compile: javac file name.java

Example: javac HelloWorld.java

Execution: java class name

Example: java HelloWorld

2.4 Common problems of HelloWorld case (understanding)

2.4.1 BUG

In a computer system or program, some undiscovered defects or problems that are hidden are collectively called bugs (vulnerabilities).

2.4.2 Solution of BUG

1. Ability to identify bugs: look more

2. Have the ability to analyze BUG: think more, check more information

3. Have the ability to solve bugs: try more, sum up more

2.4.3 Common Problems of HelloWorld Case

1. The problem of illegal characters. The symbols in Java are all in English format.

2. Capitalization issues. The Java language is case sensitive (case sensitive).

3. Display the file extension in the system to avoid the HelloWorld.java.txt file.

4. The java file name after the compilation command needs to have the file suffix .java

5. The class file name (class name) after running the command does not have the file suffix .class

2.5 Installation and use of Notepad++ software (application)

2.5.1 What to use Notepad++ software

Notepad++ is more powerful than the built-in notepad in windows. In addition to being used to make general plain text description files, it is also very suitable for writing computer program codes. Notepad++ has line numbers, which can quickly locate the problem location, as well as syntax highlighting, code folding and other functions. And it is free.

2.5.2 Notepad++ software installation

Installation: fool-style installation, just continue to the next step. It is recommended to install it in a unified development software directory, such as E:\develop.

For specific installation steps, please refer to the "Nodepad++ Software Installation and Configuration Instructions" document.

2.5.3 Notepad++ software configuration

After the installation is complete, for ease of use, make a simple configuration: modify the default language and encoding.

For specific configuration instructions, please refer to the "Nodepad++ Software Installation and Configuration Instructions" document.

3. Java basic grammar

3.1 Notes (understanding)

Comments are the explanation and explanatory text of the code, which can improve the readability of the program, so it is very important to add the necessary comment text in the program. There are three types of comments in Java:

Single line comment. The format of single-line comments is //, and the text starting from // to the end of this line will be used as the comment text.

// 这是单行注释文字

Multi-line comments. The format of multi-line comments is to use /* and */ to enclose a longer comment.

/*
这是多行注释文字
这是多行注释文字
这是多行注释文字
*/
注意:多行注释不能嵌套使用。

Document comments. Document comments /**start and */end. (I will talk about it later)

3.2 Keywords (understanding)

Keywords are words that are given special meanings by the Java language.

Features of keywords:

​ The letters of the keyword are all lowercase.

​ Commonly used code editors have highlighted keywords, such as public, class, static, etc. that we can see now.

3.3 Constants (application)

Constant: The amount whose value cannot be changed during the running of the program.

Constant classification in Java:

​ String constants with multiple characters enclosed in double quotes (can contain 0, one or more), such as "a", "abc", "China", etc.

​ Integer constant integer, for example: -10, 0, 88, etc.

​ Decimal constant decimal, for example: -5.5, 1.0, 88.88, etc.

​ Character constant is a character enclosed in single quotes, for example:'a', '5','B','中', etc.

​ Boolean constant Boolean value, which means true or false, there are only two values ​​true and false

​ The empty constant is a special value, empty value, the value is null

Except for empty constants, other constants can be output directly using output statements.

public class Demo {
    
    
    public static void main(String[] args) {
    
    
        System.out.println(10); // 输出一个整数
        System.out.println(5.5); // 输出一个小数
        System.out.println('a'); // 输出一个字符
        System.out.println(true); // 输出boolean值true
        System.out.println("欢迎来到哈哈哈"); // 输出字符串
    }
}

3.4 Data type (memory, application)

3.4.1 Computer storage unit

We know that computers can be used to store data, but whether it is a memory or a hard disk, the smallest unit of information in a computer storage device is called a "bit", which is also called a "bit", usually in lowercase letters "b" "Means. The most basic storage unit in a computer is called "byte",

It is usually represented by a capital letter "B", and the byte is composed of 8 consecutive bits.

In addition to bytes, there are some commonly used storage units, the conversion units are as follows:

1B (byte) = 8bit

1KB = 1024B

1MB = 1024KB

1GB = 1024MB

1TB = 1024GB

3.4.2 Data types in Java

Java is a strongly typed language, and data in Java must be clearly typed. Data types in Java include two basic data types and reference data types.

Basic data types in Java:

type of data Keyword Memory footprint Ranges
Integer type byte 1 -128~127
short 2 -32768~32767
int (default) 4 -2 to the 31st power to 2 to the 31st power -1
long 8 -2 to the 63 power to 2 to the 63 power -1
Floating point type float 4 Negative numbers: -3.402823E+38 to -1.401298E-45 Positive numbers: 1.401298E-45 to 3.402823E+38
double (default) 8 Negative numbers: -1.797693E+308 to -4.9000000E-324 Positive numbers: 4.9000000E-324 to 1.797693E+308
Character type char 2 0-65535
Boolean type boolean 1 true,false

Description:

​ e+38 means multiplying by 10 to the 38th power. Similarly, e-45 means multiplying by 10 to the minus 45th power.

​ In Java, integers are of type int by default, and floating-point numbers are of type double by default.

3.5 Variables (application)

3.5.1 Definition of variables

Variable: The amount whose value can be changed during the running of the program.

Essentially, a variable is a small area in memory whose value can vary within a certain range.

Variable definition format:

数据类型 变量名 = 初始化值; // 声明变量并赋值
int age = 18;
System.out.println(age);

or

// 先声明,后赋值(使用前赋值即可)
数据类型 变量名;
变量名 = 初始化值;
double money;
money = 55.5;
System.out.println(money);

You can also define multiple variables of the same data type on the same line, separated by commas. But it is not recommended to use this method to reduce the readability of the program.

int a = 10, b = 20; // 定义int类型的变量a和b,中间使用逗号隔开
System.out.println(a);
System.out.println(b);

int c,d; // 声明int类型的变量c和d,中间使用逗号隔开
c = 30;
d = 40;
System.out.println(c);
System.out.println(d);

The use of variables: access through the variable name.

3.5.2 Precautions when using variables
  1. In the same pair of curly braces, variable names cannot be repeated.
  2. Before variables can be used, they must be initialized (assigned).
  3. When defining a variable of type long, you need to add L after the integer (uppercase and lowercase are acceptable, uppercase is recommended). Because integers are of type int by default, an integer that is too large may exceed the range of int.
  4. When defining a variable of float type, you need to add F after the decimal (uppercase or lowercase is acceptable, uppercase is recommended). Because the default type of floating-point numbers is double, the value range of double is larger than float, and the types are not compatible.

3.6 Identifier (memory, comprehension)

Identifiers are names used by users in programming and are used to name classes, methods, variables, constants, etc.

Rules for the composition of identifiers in Java:

​ Consists of letters, numbers, underscore "_", dollar sign "$", the first character cannot be a number.

​ Cannot use keywords in java as identifiers.

​ Identifiers are case sensitive (case sensitive).

Naming convention of identifiers in Java:

​ Little camel case naming: variable name, method name

​ Lowercase the first letter, capitalize the first letter of each word from the second word.

​ Big hump naming: class name

​ The first letter of each word is capitalized.

​ In addition, the naming of the identifier is best to be able to know the meaning of the name

​ For example: username, studentNumber, etc.

3.7 Type conversion (understanding)

In Java, some data types can be converted to each other. Divided into two situations: automatic type conversion and forced type conversion.

Automatic type conversion:

​ Assign a value or variable that represents a small data range to another variable that represents a large data range. This conversion method is automatic, just write directly. E.g:

double num = 10; // 将int类型的10直接赋值给double类型
System.out.println(num); // 输出10.0

Forced type conversion:

​ Assign a value or variable that represents a large data range to another variable that represents a small data range.

​ Forced type conversion format: target data type variable name = (target data type) value or variable;

​ For example:

double num1 = 5.5;
int num2 = (int) num1; // 将double类型的num1强制转换为int类型
System.out.println(num2); // 输出5(小数位直接舍弃)

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-JgmHGkkI-1603713697783)(img\图片1.png)]

Description:

  1. The conversion of char type data to int type is calculated according to the corresponding int value in the code table. For example, in the ASCII code table,'a' corresponds to 97.
int a = 'a';
System.out.println(a); // 将输出97
  1. Integers are of type int by default, and byte, short, and char type data are automatically converted to int type when participating in operations.
byte b1 = 10;
byte b2 = 20;
byte b3 = b1 + b2; 
// 第三行代码会报错,b1和b2会自动转换为int类型,计算结果为int,int赋值给byte需要强制类型转换。
// 修改为:
int num = b1 + b2;
// 或者:
byte b3 = (byte) (b1 + b2);
  1. The boolean type cannot be converted to and from other basic data types.

Guess you like

Origin blog.csdn.net/weixin_42143994/article/details/109297484