JAVA basics (two) variables and constants

table of Contents

1. Identifier keywords

Second, the constant

Three, variables

Four, direct quantity


1. Identifier keywords

Identifiers are composed of numbers (0~9) and letters (A~Z and a~z), dollar sign ($), underscore (_), and all symbols in the Unicode character set whose symbols are greater than 0xC0 (no spaces between the symbols) ).

The first symbol of the identifier is a letter, underscore, and dollar sign, and any letter, number, dollar sign, or underscore can follow.

Legal identifiers: date, $2011, _date, D_$date, etc.

Illegal identifiers: 123.com, 2com, for, if, etc.

The Java language currently defines 51 keywords. These keywords cannot be used as variable names, class names, and method names. These keywords are classified below.

Data types: boolean, int, long, short, byte, float, double, char, class, interface.

流程控制:if、else、do、while、for、switch、case、default、break、continue、return、try、catch、finally。

修饰符:public、protected、private、final、void、static、strict、abstract、transient、synchronized、volatile、native。

动作:package、import、throw、throws、extends、implements、this、supper、instanceof、new。

Reserved words: true, false, null, goto, const.

Second, the constant

Constant names are usually named in uppercase letters

After the constant final modification, the value cannot be modified

Constants can modify not only constants of data types, but also methods and classes

Modified final defined method can be inherited but cannot be overridden

Modified classes cannot be inherited (extends)

Constants are divided into 3 types 

1. Static constant

需要 public static 来修饰,外部类可正常访问
public static final double PI = 3.14;

2. Member constants

成员常量,类内部使用
final double PI = 3.14;

 3. Local constant

局部常量 ,方法内部使用
public static void main(String[] args) {
        // 声明局部常量
        final double x = 3.3;
}

 

Three, variables

Variables have member variables and local variables

1. Member variables

A. Global variables (instance variables)

No static modification

Life cycle: As long as the object is treated as a reference, the instance variable will exist

B. Static variables (class variables)

Decorate with static

Life cycle: depends on the life cycle of the class. The class will be destroyed only when it is completely recycled by the garbage collection mechanism

2. Local variables

A. Method local variables

 从定义这个变量开始到方法结束这一段时间内有效。
public static void main(String[] args) {
        int a = 7;
        if (5 > 3) {
            int s = 3; // 声明一个 int 类型的局部变量
            System.out.println("s=" + s);
            System.out.println("a=" + a);
        }
        System.out.println("a=" + a);
    }

B. Method parameter variables

public static void testFun(int n) {
        System.out.println("n=" + n);// n 方法局部变量
    }

C, code block local variables


public static void test() {
        try {
            System.out.println("Hello!Exception!");
        } catch (Exception e) { // 异常处理块,参数为 Exception 类型
            e.printStackTrace();
        }
    }

Four, direct quantity

 There are 3 types of direct quantity, basic data type, string type, null type

1) Direct quantity of int type

The integer value directly given in the program can be divided into four types: binary, decimal, octal and hexadecimal. Binary needs to start with 0B or 0b, octal needs to start with 0, and hexadecimal needs to start with 0x or Start with 0X. For example, 123, 012 (corresponding to decimal 10), 0x12 (corresponding to decimal 18) and so on.

2) Direct quantity of long type

After adding l or L after the integer value, it becomes the direct quantity of the long type. For example, 3L, 0x12L (corresponding to 18L in decimal).

3) Direct quantity of float type

Adding f or F after a floating-point number becomes a direct quantity of type float. This floating-point number can be in standard decimal form or in scientific notation form. For example, 5.34F, 3.14E5f.

4) Direct quantity of double type

Directly giving a floating-point number in standard decimal form or scientific notation form is the direct quantity of the double type. For example, 5.34, 3.14E5.

5) Direct quantity of boolean type

The only direct variables of this type are true and false.

6) Direct amount of char type

There are three forms of the direct quantity of the char type, which are characters enclosed in single quotes, escape characters, and characters represented by Unicode values. For example,'a','\n' and'\u0061'.

7) Direct quantity of String type

A sequence of characters enclosed in double quotes is a direct quantity of the String type. In most other languages, including C/C++, strings are implemented as arrays of characters. However, this is not the case in Java. In Java, strings are actually object types. As you will see later in the tutorial, because Java implements strings as objects, it has a wide range of string processing capabilities and is both powerful and easy to use.

8) Direct quantity of null type

The direct quantity of this type has only one value, which is null.

int a = 5; 
char c = 'a';
 boolean b = true; 
float f = 5.12f;
 double d = 4.12;
 String name = "CJ点";
 String url = "https://mp.csdn.net/postedit/102516903";

 

Reference article:

http://c.biancheng.net/java/20/

Guess you like

Origin blog.csdn.net/qq_37203082/article/details/102516903