Java 基础语法
1. Notes
Comments are explanations and explanatory text for the code.
There are three types of annotations in Java:
- Single-line comments:
// single line comment text
- Multi-line comments:
/*
This is a multiline comment text
This is a multiline comment text
This is a multiline comment text
*/
- Documentation notes:
/**
This is the doc comment text
*/
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.
be careful
The content of the comment will not participate in compilation and operation, but 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.
After 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 Keyword class
means to define 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. Literals
Function: Tell the programmer the writing format of the data in the program.
字面量类型 | 说明 | 程序中的写法 |
---|---|---|
整数 | 不带小数的数字 | 666,-88 |
小数 | 带小数的数字 | 13.14,-5.21 |
字符 | 必须使用单引号,有且仅能一个字符 | ‘A’,‘0’, ‘m’ |
字符串 | 必须使用双引号,内容可有可无 | “HelloWorld” |
布尔值 | 布尔值,表示真假,只有两个值:true,false | true 、false |
空值 | 一个特殊的值,空值 | 值是:null |
public class Demo {
public static void main(String[] args) {
System.out.println(10); // output an integer
System.out.println(5.5); // output a decimal
System.out.println('a'); // output a character
System.out.println(true); // output boolean value true
System.out.println("helloworld"); // output string
}
}
Differentiation 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 inside is, no matter whether there is content inside, it is a literal value of string type.
- The literal value of the character type must be enclosed in single quotation marks, no matter what the content is, but there is one and only one.
- The literal value of character type has only two values, true and false.
- Empty type literals have only one value, null.