Basic syntax format of Java basic syntax

Table of contents

Table of contents

Preface

mind Mapping

1. Basic syntax of Java

1.1. Class definition:

1.2. Definition of method: 

1.3, main method:

1.4, statements and expressions:

1.5, code block:

2. Comments in Java

2.1, single line comments

2.2, multi-line comments

2.3, Documentation comments

2.4, important information 

3. Keywords in java

4. Identifiers in java

5. Summary


Preface

  This chapter is a review note on the basics of Java, which facilitates my summary and study of some important knowledge points in Java learning.

  Each programming language has its own set of grammatical specifications, and the Java language is no exception. It also needs to comply with certain grammatical specifications, such as code writing, identifier definition, keyword application, etc. Therefore, to learn the Java language well, you first need to be familiar with its basic syntax. This chapter is a review and summary of the basic syntax format of Java.

mind Mapping

1. Basic syntax of Java

1.1. Class definition:

  Code in Java needs to be executed in a class . A Java file usually has only one public class . The definition of a class includes the class name and the main part of the class . The main part is enclosed in a pair of curly brackets.

public class ClassName {
    // 类的主体部分
}

1.2. Definition of method: 

  Methods in Java are used to execute specific code logic . Methods contain method signatures and method bodies . The method signature includes access modifiers, return type, method name and parameter list, and the method body contains the actual code logic.
public void methodName() {
    // 方法体
}

1.3, main method:

The entry point for Java code execution is the main method (main method) , in which the entry logic of the program can be written.

public static void main(String[] args) {
    // 主方法体
}

1.4, statements and expressions:

Statements in Java are used to perform specific operations or control the flow of a program . Statements end with a semicolon. Expressions are used to calculate values ​​or perform specific operations.

int x = 5; // 变量声明语句
System.out.println("Hello, World!"); // 输出语句
x++; // 表达式语句,自增操作

1.5, code block:

Code blocks in Java are used to group multiple statements together and can form scopes. A block of code is enclosed by a pair of curly braces.

{
    // 代码块
    int y = 10;
    System.out.println(y);
}

                             Are the last two basic syntax formats above consistent with the C language?

2. Comments in Java

  When writing a program, in order to make the code easier to read, some comments are usually added to the code while implementing the functions. A comment is an explanation of a certain function of the program or a certain line of code. It can make it easier for developers to understand the function of the code when reading and using the code later.

  Annotations are only valid in Java source files. When compiling the program, the compiler will ignore these annotation information and will not compile it into a bytecode-like file.

Comments in Java are used to describe and explain code. There are three types of comments: single-line comments, multi-line comments and documentation comments.

2.1, single line comments

           Start with a double slash (//), followed by comments. A single line comment only comments out one line of code.

// 这是一个单行注释
int x = 10; // 这是对变量的解释

2.2, multi-line comments

             Use slash asterisks (/* … */) to enclose comment content. Multi-line comments can comment multiple lines of code.

/*
这是一个多行注释
注释内容可以跨多行
*/
int y = 20; /* 这是对变量的解释 */

2.3, Documentation comments

Use slash asterisks (/** … */) to enclose comment content. Documentation comments are used to generate API documentation, usually used for descriptions of classes, interfaces, methods, etc.

/**
 * 这是一个文档注释
 * 文档注释可以跨多行
 */
public class MyClass {
    /**
     * 这是一个方法的文档注释
     * @param x 参数x的说明
     * @return 返回值的说明
     */
    public int myMethod(int x) {
        // 方法体
    }
}

2.4, important information 

Comments are for programmers to read, and the compiler will ignore the comment part , so it will not have any impact on the running of the program. The existence of comments helps to improve the readability and maintainability of the code. We should also add more comments in our daily life. It is also a kind of responsibility to others. Otherwise, how will others see the messy code.

3. Keywords in java

  Keywords are words that have been defined in advance and given special meanings in programming languages. They are also called reserved words. Like other languages, many keywords are reserved in Java.

Here are some commonly used java keywords that I have compiled:

Key words describe
abstract Declare abstract class or abstract method
boolean Declare a boolean variable or method
break End the execution of the loop or switch statement
byte Declare byte type variables or methods
case Match specific values ​​in switch statements
catch catch exception
char Declare character type variables or methods
class declare class
const Not recommended, use the final keyword instead of constants
continue Continue with the next iteration of the loop
default Specify default conditions in switch statements
do Beginning of loop statement
double Declare a variable or method of double precision floating point type
else An optional branch of the if statement
enum Declare enumeration type
extends Declare class inheritance relationships
final Declare an immutable class, method, or variable
finally Code that is always executed within a try-catch block
float Declare a variable or method of single-precision floating point type
for Beginning of loop statement
goto No longer use
if Beginning of conditional statement
implements Declare a class to implement one or more interfaces
import Import a class or package
instanceof Checks whether an object is an instance of a specific class
int Declare an integer variable or method
interface Declare interface
long Declare long integer variable or method
native Declare that a method implementation is implemented by another programming language
new Create new object
package declaration package
private Declare a private class, method or variable
protected Declare a protected class, method, or variable
public Declare a public class, method or variable
return Return the result of the method
short Declare a short variable or method
static Declare static variables, methods or code blocks
strictfp Statement that floating point calculations strictly adhere to the IEEE 754 standard
super Reference to members of parent class
switch multi-branch conditional statement
synchronized Declare synchronized methods or code blocks
this a reference to the current object
throw Throw an exception
throws Declare exceptions that may be thrown by a method
transient Declare instance variables that are not serialized
try Declare a code block for catching exceptions
void Declaring a method with no return value
volatile Used to mark a variable to indicate that it may be modified simultaneously in multiple threads
while Beginning of loop statement

4. Identifiers in java

  In Java, an identifier is a sequence of characters used to name program elements such as variables , methods, classes, packages, etc. Identifiers must follow the following rules:

  1. Identifiers consist of letters, numbers, underscores (_), and dollar signs ($).
  2. The first character of the identifier must be a letter, underscore, or dollar sign, not a number.
  3. 标识符区分大小写,例如"myVariable"和"myvariable"是不同的标识符。
  4. 标识符不能是Java的关键词(如if、for等)或保留字(如true、false等)。
  5. 标识符的长度理论上没有限制,但在实际编程中,建议保持标识符的简洁和可读性。

以下是一些示例合法的Java标识符:

  • age
  • myVariable
  • _count
  • MAX_VALUE
  • $price

5,总结

  本章主要是对Java基础语法中基本语法格式进行的总结,对于初学者,帮助初学者理解Java代码的整体结构和组织方式,编写正确的代码,理解代码的执行流程,打下坚实的基础,为进一步学习和掌握Java更高级的特性和功能奠定基础,当然对于我自己来说对于知识的理解更加透彻了。

对于我来说,每次阅读同一本书,我都能够获得不同的感受和领悟,从中汲取独特的启示和体验。

每日一言

我在自己周围筑起高墙,没有哪个人能够入内,也尽量不放自己出去。

  如果我的学习笔记对你有用,不妨点赞收藏一下,感谢你的支持,当然也欢迎大佬给我一下建议或是对笔记中的不足进行补充,对我学习大有帮助,谢谢。  

Guess you like

Origin blog.csdn.net/weixin_72543266/article/details/132528560