day02 - Java Basic Grammar
1. Notes
Comments are explanatory and explanatory text for code.
There are three types of annotations in Java:
- Single line comment:
// 这是单行注释文字
- Multi-line comments:
/*
这是多行注释文字
这是多行注释文字
这是多行注释文字
*/
注意:多行注释不能嵌套使用。
- Documentation comments (temporarily unused):
/**
这是多行注释文字
这是多行注释文字
这是多行注释文字
*/
techniques used
If we want to explain the code, then we can use comments.
When the content of the comment is relatively small, it can be written in one line, and a single-line comment can be used.
If the comment has a lot of content and needs to be written in multiple lines, then you can use multi-line comments.
important point
The content of the comment will not participate in compilation and operation, it is only an explanation of the code.
Therefore, no matter what is written in the comment, it will not affect the result of the code running.
2. Keywords
2.1 Concept
An English word given a specific meaning by Java.
When we write keywords in the code, when the program is running, we know what to do.
Note: There are many keywords, so don't deliberately remember them.
abstract | assert | boolean | break | byte |
---|---|---|---|---|
case | catch | char | class | const |
continue | default | do | double | else |
enum | extends | final | finally | float |
for | goto | if | implements | import |
instanceof | int | interface | long | native |
new | package | private | protected | public |
return | strictfp | short | static | super |
switch | synchronized | this | throw | throws |
transient | try | void | volatile | while |
2.2 The first keyword class
Indicates the definition of a class. Create a class.
Class: The most basic unit of a Java project. A complete Java project may consist of thousands of classes.
Class is followed by the name of the class, abbreviated as: class name.
There will be a pair of curly braces after the class name, indicating the content of this class.
Example:
public class HelloWorld{
}
Explanation: class means to define a class.
Class name: HelloWorld
The braces after HelloWorld indicate the scope of this class.
3. Literal
Function: Tell the programmer the writing format of the data in the program.
literal type | illustrate | How to write in the program |
---|---|---|
integer | numbers without decimals | 666,-88 |
decimal | numbers with decimals | 13.14,-5.21 |
character | Single quotes must be used, with only one character | 'A', '0', 'i' |
string | Double quotes must be used, the content is optional | "HelloWorld", "Dark Horse Programmer" |
Boolean value | Boolean value, indicating true and false, only two values: true, false | true 、false |
empty value | a special value, null | Value is: null |
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("欢迎来到黑马程序员"); // 输出字符串
}
}
distinguishing skills
- Numbers without a decimal point are integer literals.
- As long as a decimal point is included, it is a literal value of the decimal type.
- As long as it is enclosed in double quotation marks, no matter what the content is, whether there is content or not, it is a literal value of string type.
- Literals of character type must be enclosed in single quotes, no matter what the content is, but there can only be one.
- The literal value of character type has only two values, true and false.
- Literals of empty type have only one value, null.
4. Variables
4.1 What is a variable?
A variable is a container for temporarily storing data in a program. But only one value can be stored in this container.
4.2 Variable definition format
data type variable name = data value;
4.2.1 Format Details
Data type: defines what type of data can be stored in a variable.
If you want to store 10, then the data type needs to be an integer type.
If you want to store 10.0, then the data type needs to be written as a decimal type.
Variable name: In fact, it is the name of the container.
When you want to use the data in the variable in the future, just use the variable name directly.
Data value: the data actually stored in the container.
Semicolon: Indicates the end of a sentence, just like the period when writing a composition before.
4.2.2 Common data types
Integer: int
decimal: (floating point) double
Other data types will be explained later
Example:
public class VariableDemo{
public static void main(String[] args){
//定义一个整数类型的变量
//数据类型 变量名 = 数据值;
int a = 16;
System.out.println(a);//16
//定义一个小数类型的变量
double b = 10.1;
System.out.println(b);//10.1
}
}
4.2.3 Notes on Variables
- Variable names cannot be repeated
- In one statement, multiple variables can be defined. But this method affects the reading of the code, so just understand it.
- Variables must be assigned a value before they can be used.
case:
public class VariableDemo2{
public static void main(String[] args){
//1.变量名不允许重复
//int a = 10;
//int a = 20;
//System.out.println(a);
//2.一条语句可以定义多个变量
//了解。
//int a = 10, b = 20, c = 20,d = 20;
//System.out.println(a);//?
//System.out.println(b);//?
//3.变量在使用之前必须要赋值
int a = 30;
System.out.println(a);
}
}
4.3 Exercises with variables
Requirement: After telling the bus to the terminal, how many passengers are there in the bus?
At first there were no passengers.
First stop: One passenger went up, but no one got off.
Second stop: Two passengers go up and one passenger goes down.
Third stop: Two passengers go up and one passenger goes down.
The fourth stop: no passengers going up, one passenger coming down.
Fifth stop: One passenger went up, but no one got off.
Question: After arriving at the terminal, how many passengers are there in total?
Code analysis:
public class VariableTest1{
//主入口
public static void main(String[] args){
//一开始没有乘客。
int count = 0;
//第一站:上去一位乘客
//在原有的基础上 + 1
count = count + 1;
//System.out.println(count);
//第二站:上去两位乘客,下来一位乘客
count = count + 2 - 1;
//第三站:上去两位乘客,下来一位乘客
count = count + 2 - 1;
//第四站:下来一位乘客
count = count - 1;
//第五站:上去一位乘客
count = count + 1;
//请问:到了终点站,车上一共几位乘客。
System.out.println(count);//3
}
}
5. Data types
5.1 Classification of Java language data types
- basic data type
- Reference data type (learn in depth when object-oriented)
5.2 Four categories and eight types of basic data types
type of data | keywords | memory usage | Ranges |
---|---|---|---|
integer | byte | 1 | Negative 2 to the 7th power ~ 2 to the 7th power -1 (-128~127) |
short | 2 | Negative 2 to the 15th power ~ 2 to the 15th power -1 (-32768~32767) | |
int | 4 | Negative 2 to the 31st power ~ 2 to the 31st power -1 | |
long | 8 | Negative 2 to the 63rd power ~ 2 to the 63rd power -1 | |
floating point number | float | 4 | 1.401298e-45 ~ 3.402823e+38 |
double | 8 | 4.9000000e-324 ~ 1.797693e+308 | |
character | char | 2 | 0-65535 |
Boolean | boolean | 1 | true,false |
illustrate
e+38 means multiplied by 10 to the 38th power, and e-45 means multiplied by 10 to the negative 45th power.
Integers in java are of type int by default, and floating point numbers are of type double by default.
Need to remember the following points
The value range of byte type:
-128 ~ 127
Approximate value range of int type:
-More than 2.1 billion ~ More than 2.1 billion
The relationship between the value range of integer type and decimal type:
double > float > long > int > short > byte
The most commonly used data type selection:
- When defining variables, different types of variables should be selected according to the actual situation.
For example: the age of a person, you can choose the byte type.
For example: the age of the earth, you can choose the long type.
- If the range of the integer type is not sure, then the int type is used by default.
- If the range of the decimal type is not sure, then the double type is used by default.
- If you want to define a variable of character type, then use char
- If you want to define a variable of Boolean type, then use boolean
5.3 Define 8 basic data type variables
public class VariableDemo3{
public static void main(String[] args){
//1.定义byte类型的变量
//数据类型 变量名 = 数据值;
byte a = 10;
System.out.println(a);
//2.定义short类型的变量
short b = 20;
System.out.println(b);
//3.定义int类型的变量
int c = 30;
System.out.println(c);
//4.定义long类型的变量
long d = 123456789123456789L;
System.out.println(d);
//5.定义float类型的变量
float e = 10.1F;
System.out.println(e);
//6.定义double类型的变量
double f = 20.3;
System.out.println(f);
//7.定义char类型的变量
char g = 'a';
System.out.println(g);
//8.定义boolean类型的变量
boolean h = true;
System.out.println(h);
}
}
important point
- If you want to define a variable of integer type, you don't know which data type to choose, and int is used by default.
- If you want to define a variable of decimal type, you don’t know which data type to choose, and double is used by default.
- If you want to define a variable of type long, you need to add the L suffix after the data value. (Upper and lower case are acceptable, but upper case is recommended.)
- If you want to define a variable of type float, you need to add the F suffix after the data value. (both uppercase and lowercase)
5.4 Exercise 1
Requirement: Define 5 variables to record teacher's information and print
Code example:
public class VariableTest1{
public static void main(String[] args){
//1.定义字符串类型的变量记录老师的姓名
String name = "黑马谢广坤";
//2.定义整数类型的变量记录老师的年龄
int age = 18;
//3.定义字符类型的变量记录老师的性别
char gender = '男';
//4.定义小数类型的变量记录老师的身高
double height = 180.1;
//5.定义布尔类型的变量记录老师的婚姻状况
boolean flag = true;
//输出5个变量的值
System.out.println(name);
System.out.println(age);
System.out.println(gender);
System.out.println(height);
System.out.println(flag);
}
}
5.5 Exercise 2
Requirement: Select different types of variables for the four information (movie name, starring role, year, rating), and then print them out.
Code example:
public class VariableTest2{
public static void main(String[] args){
//1.定义字符串变量记录电影的名称
String movie = "送初恋回家";
//2.定义三个变量记录主演的名字
String name1 = "刘鑫";
String name2 = "张雨提";
String name3 = "高媛";
//3. 定义整数类型的变量记录年龄的年份
int year = 2020;
//4.定义小数类型的变量记录电影的评分
double score = 9.0;
//打印变量的信息
System.out.println(movie);
System.out.println(name1);
System.out.println(name2);
System.out.println(name3);
System.out.println(year);
System.out.println(score);
}
}
5.6 Exercise 3
Requirement: Select one of the mobile phones, select different types of variables for the two information (mobile phone price, mobile phone brand), and then print them out.
Code example:
public class VariableTest3{
public static void main(String[] args){
//1.定义小数类型的变量记录手机的价格
double price = 5299.0;
//2.定义字符串类型的变量记录手机的品牌
String brand = "华为";
//输出变量记录的值
System.out.println(price);
System.out.println(brand);
}
}
6. Identifier
Most programmers in the industry are following Alibaba's naming rules.
It is in the data folder of day02.
6.1 Mandatory requirements:
This must be done, otherwise the code will report an error.
- Must consist of numbers, letters, underscores _, and dollar signs $.
- number cannot start with
- cannot be a keyword
- Case sensitive.
6.2 Software Recommendations:
If you don't do this, the code will not report an error, but it will make the code appear to be relatively low.
6.2.1 CamelCase
Applies to variable names and method names
- If it is a word, then all lowercase, such as: name
- If it is multiple words, start with the second word and capitalize the first letter, for example: firstName, maxAge
6.2.2 Big hump nomenclature
applies to class names
- If it is a word, then the first letter is capitalized. For example: Demo, Test.
- If it is more than one word, the first letter of each word needs to be capitalized. For example: Hello World
No matter what name you choose, you must be familiar with the name.
Alibaba naming convention details:
- Try not to use pinyin. But some international pinyin can be regarded as English words.
Correct: alibaba, hangzhou, nanjing
Incorrect: jiaage, dazhe
- Usually, when naming variable names, method names, and class names, do not use underscores or dollar signs.
Error: _name
Correct: name
7. Keyboard entry
The actual function of keyboard input has been written in Java for us, and we don’t need to implement it ourselves, and the functions written in Java are all placed in the Scanner class, so we only need to use the Scanner class directly.
Steps for usage:
first step:
Guide package: In fact, it means to find out where the Scanner class is first.
Step two:
Create an object: In fact, it means to declare that I am going to start using the Scanner class.
third step:
Receiving data: It is also the code that actually does the work.
Code example:
//导包,其实就是先找到Scanner这个类在哪
import java.util.Scanner;
public class ScannerDemo1{
public static void main(String[] args){
//2.创建对象,其实就是申明一下,我准备开始用Scanner这个类了。
Scanner sc = new Scanner(System.in);
//3.接收数据
//当程序运行之后,我们在键盘输入的数据就会被变量i给接收了
System.out.println("请输入一个数字");
int i = sc.nextInt();
System.out.println(i);
}
}
8. IDEA
8.1 Overview of IDEA
The full name of IDEA is IntelliJ IDEA, which is an integrated environment for Java language development. It is recognized by the industry as the best tool for Java program development.
Integrated environment:
A development tool that integrates multiple functions such as code writing, compiling, executing, and debugging.
8.2 Download and installation of IDEA
8.2.1 Download
You can download it from the official website at: https://www.jetbrains.com/idea
In today's information, the corresponding installation package has also been improved for everyone.
8.2.2 Installation
- Go to the data folder and double-click the installation package.
- Click next, ready to install
- Click Browse to modify the installation path.
After modification, click next
- Check the 64-bit launcher. Indicates creating a 64-bit shortcut on the desktop.
Do not check the others.
Click next.
- Click Install, ready to install.
- After the progress bar is finished reading, there will be a final interface prompt.
Click finish.
- The first startup will ask whether to import some settings.
Choose the second one not to import, keep the default settings, and click OK.
- Choose a background theme.
On the left is a black background. On the right is a white background.
This can be chosen according to your own preferences.
After selecting, click next in the lower right corner
- Let us buy ideas in this interface.
Because we are in the learning stage, we can use it for free for 30 days.
Click on the second in the first row. Evaluate for free
- Click the blue Evaluate to start a free 30-day trial.
- When you see this interface, it means that the idea has been successfully installed.
You can click the upper right corner to close it.
8.3 Introduction to Hierarchical Structure in IDEA
8.3.1 Structural classification
- project (project, project)
- module
- package (package)
- class (class)
8.3.2 Structure Introduction
In order to allow everyone to better absorb, we will learn the package level later, and learn the most basic project, module, and class first.
project (project, project)
Taobao, Jingdong, and Dark Horse programmer websites all belong to each project, and IDEA is each Project.
module
In a project, multiple modules can be stored, and different modules can store different business function codes in the project. In the official website of Dark Horse Programmer, at least the following modules are included:
- forum module
- Registration, consultation module
In order to better manage the code, we will store the code in two modules.
package (package)
There are many services in one module. Taking the forum module of the official website of Dark Horse Programmer as an example, it contains at least the following different services.
- post
- Comment
In order to distinguish these businesses more clearly, packages are used to manage these different businesses.
class (class)
This is where the actual code is written.
8.3.3 Summary
- Hierarchical relationship
project - module - package - class
- Multiple
modules can be created in a project,
multiple packages can be created
in a module, and multiple classes can be created in a package
. The division of these structures is for the convenience of managing class files.
8.4 The first code in IDEA
8.4.1 Operation steps
- Create a Project project
- Create a Module module
- create class class
- write the code in the class
- Compile and run
8.4.2 Step-by-step illustration
- Double-click the launcher icon
- First, create a new project
and click create new project
- We want to start writing code from 0, so create an empty project with nothing.
Click Empty Project in the lower left corner
and then click next in the lower right corner
- Enter the name of the project
Enter the storage path of the project
- Click ok. idea will help us create a project folder locally
- Click Module to create a new module
- Click +
and then click New Module
- We want to write Java code, so we need to create a new Java module.
Click Java
and then click next in the lower right corner
- Enter the name of the module
and click Next in the lower right corner
- After successfully creating a new module, the newly created module will appear in the middle,
click OK in the lower right corner
- Go back to the main interface,
expand the newly created module,
right-click on src, select New, and select Java Class
- Enter the class name
and press Enter
- Since the font is relatively small
, we need to set the font.
Click on File and select Settings.
- Search for font and
enter the value of Size on the right to adjust the size of the code font.
After setting, click OK in the lower right corner
- Write code
- Run the code
Right click on the blank space and click Run
- The console will pop up at the bottom.
The contents of all output statements will be displayed on the console.
8.5 Related operations of classes in IDEA
8.5.1 Class related operations
- new class file
- delete class file
- modify class file
8.5.2 Create a new class file
- All Java code will be written in the src folder.
So, right click on src, select new, click Java Class
- Enter the class name and press Enter
- newly built
8.5.3 Modifying the class name
- Right-click the file you want to modify,
click Refactor
and click Rename
- Enter the name you want to modify
and click Refactor below after typing
- Both the file name and class name have been modified successfully
8.5.4 Delete class files
- To delete a file, right-click on the file
and select Delete
- Click OK in the pop-up interface to confirm the deletion
Tips:
At this time, the deletion does not go to the recycle bin, but is directly deleted from the hard disk.
8.6 Related operations of modules in IDEA
8.6.1 Related operations of the module
- new module
- delete module
- modify module
- import module
8.6.2 New module
- Click on File and select Project Structure
- Select Module
- Click +
to select New Module
- To create a Java module, so select the first Java
and click Next in the lower right corner
- Enter the name of the module
and click Finish in the lower right corner
- After successful creation, the newly created module appears in the middle blank area,
click OK in the lower right corner
- In the main interface, the newly created module will also appear
8.6.3 Delete a module
- Right click on the module
and select Remove Module
- Select Remove to confirm the deletion
- At this time, it is found that on the IDEA list page, the deleted module is no longer there.
Tips:
At this time, the deletion is only a deletion from the IDEA list, and it still exists in the local hard disk.
8.6.4 Modifying modules
- Right-click on the module name,
select Refactor
and select Rename
- Select the third one to modify the module name and local folder name
and click OK
- Enter the new module name to be modified
and click Refactor after entering
- Back to the main interface, you will find that the module name and folder name have been modified
8.6.5 Importing modules
- Click on File and select Project Structure
- Select Module
and click +
to select Import Module
- Select the module to import from the local hard disk
and click OK
- Keep clicking Next
- If a prompt box appears in the middle, click Overwrite
and then continue to click Next in the lower right corner
- Keep clicking until finish
- After successful import, the imported module information will appear in the middle position
- The imported module information will also appear in the main interface
- Expand the module and click the Java file in the module, you will find that the code reports an error.
It is because the imported module is not associated with the JDK.
- You can click Setup SDK in the upper right corner
and select the installed JDK version
- After the import is complete, the code will return to normal and no error will be reported.
8.7 Related operations of projects in IDEA
8.7.1 Project-related operations
- close project
- open project
- modify item
- New Project
8.7.2 Closing a project
- Click File and select Close Project
- The project you just operated has been closed.
On the left is the project list. If you want to open the project again, just click it.
There is create new project on the right, you can build a new project
- When the mouse is placed on the item, a cross will appear behind it.
If you click the fork here, it will be deleted from the IDEA list. Items on the local hard drive are not deleted.
8.7.3 Opening a project
- In this interface, you can also open a project that already exists locally.
Click Open or Import
- Select the project to open
and click OK
- The project is opened.
8.7.4 Modify project
- Click on File and select Project Structure
- In this interface, the default is Module.
Therefore, you must first click Project
on the right page, enter a new project name,
modify the JDK version and compiled version to JDK14,
and then click OK
- At this point, it is found that the project name has been modified
- But the name of the local folder has not been modified
- You need to close the current project first
- Click the cross next to an item to remove it from the list
- Go to the local hard disk to manually modify the name of the folder
- Click Open or Import to reopen the project
- Select the modified item
and click OK
- At this point, you will find that the project name and the name of the local hard disk folder have been modified.
8.7.5 Create a new project
- Click File
to select New
and click Project
- Also create an empty project with nothing
- Enter a name for the project
and click finish in the lower right corner
- Does the IDEA cycle need to help us create a new folder locally
? Click OK
- Ask whether to open in this window or in a new window.
You can click New Window to open in a new window.
- At this point, two windows appear, and a new project is opened in a new window