Basics of Java program syntax (1)

JAVA development kit JDK

Java Development Kit , Java Development Kit, referred to as JDK
javac : Java compiler
java : Java virtual machine
javadoc : Java document generator
jar : Java archive packager
appletviewer : Java applet viewer

When a source program file contains multiple classes, pay attention to the following issues:

  1. Only one class can be declared public.
  2. The file name must match the public class name exactly, including letter case.
  3. public static void main(String[] args) can only be defined in public classes.

identifier - indentifer

An identifier is a name assigned by a programmer to a variable, constant, method, enumeration, class, interface, etc. The characters that make up an identifier have certain specifications. The naming rules of identifiers in the Java language:

  1. Case sensitivity : My and my are two different identifiers;
  2. The first character , which can be an underscore or a dollar sign or a letter, but not a number;
  3. Characters other than the first character , which can be underscores, dollar signs, letters and numbers;
  4. keywords cannot be used as identifiers

The letters in the Java language use double-byte Unicode encoding. Unicode is a unified encoding system, which includes Asian character encodings, such as Chinese, Japanese, Korean and other characters.

keywords

There are almost fifty keywords in Java, as shown in the figure: (all keywords are lowercase letters)
insert image description here

delimiter

The main delimiters are: semicolon (;), left and right braces ({}) and blank.
Semicolon : indicates the end of a statement
Braces : used for nesting of statements, etc.
Blank : Blanks are allowed between elements in Java source code, and the number of blanks is not limited. Blanks include spaces, tabs (Tab key input) and line breaks (Enter key input), and appropriate blanks can improve the readability of the source code.

variable

Variables and constants are an important part of expressions, and the internals of variables can be modified. Variables include variable names and variable values, variable declaration format:

数据类型		变量名		[=初始值]

The variable name must comply with the identifier naming convention, and there cannot be repeated variable names in the relevant scope.
The variable scope is the scope of use of the variable . The variable can be used within this scope. When the scope is exceeded, the variable content will be released. According to different scopes, variables are divided into member variables and local variables .

constant

Constants are variables that cannot be modified. Constants also need to be declared, and an initial value needs to be assigned when declaring. Constants cannot be modified once initialized . The format is as follows:

final 数据类型 变量名 = 初始值;

Constants are divided into three categories: static constants , member constants , and local constants .

public class Hello {
    
    
    public static final double PI = 3.1415;
    // 静态常量,可当作const使用
    final int y = 10;
    //声明成员变量
    public static void main(String[] args) {
    
    
        // 声明局部变量
        final int k = 4;
    }
}

The constant modified by public static is a static constant. Its scope is global and can be accessed without creating an object. The access form outside the class: Hello

type of data

There are 8 basic data types in the Java language.
insert image description here
Different data types have different ways of writing:
integer constants:
decimal : 20, -20;
octal : 020, -020;
hexadecimal : 0x20, -0x20;
binary : 0b10100, -0B10100

Real number constant:
with decimal point : 20.5, -20.0
scientific notation : 2.05E1, etc.

You can add the suffix "L" (both uppercase and lowercase) to convert it into a long type, for example: 20L, -20l;

There are a few points I haven't seen in C++, as follows:

  1. Java language integer types are all signed forms (signed)
  2. The single-byte integer in the Java language is byte, and in C++ it is char
  3. The Java long integer long occupies 8 bytes, which is twice that of int
  4. The character type char occupies 2 bytes in Java and 1 byte in C++
  5. There are no pointers in Java

Regarding the encoding method, I don’t know much about it. Let’s post a screenshot:
insert image description here

escape character

insert image description here

read-only variable

If a certain data processed by the program is constant and does not need to be changed during the running of the program, you can define a read-only variable (read-only) to save the data.

  1. A read-only variable is essentially a variable, and from a functional point of view, it uses variables to realize the function of constants
  2. Read-only variables are sometimes simply called constants
  3. Compared with literal constants, read-only variables can improve program readability and facilitate adjustment of constant values. Definition
    method:
final 数据类型 常变量名=初始值;

Grammar description :

  1. Use the keyword final to define read-only variables;
  2. Read-only variables can only be assigned a value once . After the read-only variable obtains the initial value, it can only be read, and cannot be written (such as reassignment).
  3. Read-only variables are usually initialized when they are defined.
    insert image description here

operator

insert image description here

data coercion

insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here

Algorithm Basic Structure

  1. sequential structure
  2. select structure
  3. loop structure

insert image description here
If else, switch, while, for, continue, break, and other statements are the same as in C++.

Guess you like

Origin blog.csdn.net/qq_52109814/article/details/122478715