2. Variables
2.1. Keywords and reserved words
keywords
Definition : a character string given special meaning and used for special purposes
Features : All letters of the keyword are
小写
reserved word
Not used by existing Java versions, but may be used as a keyword in future versions. Avoid using these reserved words when naming identifiers yourself. Such as: goto, const
2.2. Identifier
Use of identifiers
1. Identifier: Any place where you can name it is called an identifier. For example: class name, variable name, method name, interface name, package name...
2. Naming rules for identifiers:
Consists of 26 English letters, uppercase and lowercase, 0-9, _ or $
Numbers cannot begin with.
Keywords and reserved words cannot be used, but keywords and reserved words can be included.
Java is strictly case-sensitive, and the length is unlimited.
Identifiers cannot contain spaces.
3. Naming conventions in Java :
包名
: When composed of multiple words所有字母都小写
: xxxyyyzzz
类名、接口名
: When composed of multiple words, all words首字母大写
: XxxYyyZzz
变量名、方法名
: When multiple words are formed, the first letter of the first word is lowercase, and the first letter of each word is capitalized at the beginning of the second word:xxxYyyZzz
常量名
:所有字母都大写
. When there are multiple words, connect each word with an underscore: XXX_YYY_ZZZ
2.3. Variables
Inclusion of variables 变量类型
, 变量名
and 存储的值
: the format of defining variables: data type variable name = variable value
String myName = "Arbicoral";
System.out.println(myName);
The role of variables: to store data in memory
Notice:
Every variable in Java must
先声明,后使用
Use variable names to access data in this area
The scope of a variable: within a pair of {} where it is defined
2.3.1, data type
7 basic data types: byte, short, int, long, float, double, char, boolean
3 reference data types: class, interface, array
Integer constant, the default type is: int
Floating-point constant, the default type is: double
2.3.1.1, member variables and local variables
Outside the method body, the variables declared in the class body are called member variables ;
Variables declared inside the method body become local variables .
2.3.1.2. Integer type
bit: the smallest storage unit in the computer; Byte: the basic storage unit in the computer. 1 Byte = 8 bits
Integer constant, the default type is: int
2.3.1.3, floating point type
Single-precision float (4 bytes), double-precision double (8 bytes); to declare a float variable, it must end with "f" or "F"
Float means that the range of values is larger than long, because some of them are used to store integers, and some are used to store decimal points
Floating-point constant, the default type is: double
E:10
2.3.1.4, character type
1 character = 2 bytes, represented by a pair
''
, only one character can be written inside. a=97;A=65
Representation:
1. Declare a character;
2. Escape characters;
3. Use Unicede values directly to represent character constants.
char c1 = 'S';
//转义字符:换行符
char c2 = '\n';
//Unicode,十六进制
char c3 = '\u0056'; // 表示字母V
2.3.1.5, History of Coding Development
-
ASCⅡ
: The scope of application is too small. -
Unicode
Encoding: Unicode and ASCII cannot be distinguished, and the computer cannot distinguish whether three bytes represent one symbol or three symbols respectively. -
UTF-8
: It is a variable-length encoding method, using 1-6 bytes to represent a symbol, and the byte length can be changed according to different symbols.
2.3.1.6, Boolean
Can only take one of 2 values: true, false;
It is often used in conditional judgment and loop structure.
2.3.1.7、String
String is a reference data type
String can be empty, but the char character type cannot be empty, and a space can be added.
String can be operated with 8 other data types, and the operation symbol is +, and the budget results are all String
String s1 = "";
char c1 = ' ';
char c2 = ''; // 编译不通过
//*****************************
int number = 1002;
String numberStr = "学号";
String stu = numberStr + number; //+:连接运算
2.4. Data type operation
-
Automatic type promotion: byte, short, char--> int--> long--> float--> double
-
When a variable of a data type with a small capacity is operated on with a variable of a data type with a large capacity, the result is automatically promoted to a data type with a large capacity.
-
In particular:
当 byte、char、short三种类型的变量做运算时,结果为 **int型**
.
-
-
Coercion: The inverse of the automatic type promotion operation.
-
Need to use strong escape character ();
-
Note: Mandatory type conversion may result in loss of precision (high --> low).
-
illustrate:
It can be executed without adding l or L after the long type, and no error is reported, indicating that an int type is converted to a long type, and the data type is improved
float 后面一定要加上F or f
, if not added, an error will be reported, because float-->double will have precision lossInteger constant, the default type is int ;
Floating-point constant, the default type is double .
2.5, hexadecimal
All numbers exist in binary form at the bottom of the computer .
For integers, there are 4 representations:
Binary : 0, 1 , full 2 into 1, start with 0b or 0B ;
Decimal (decimal): 0~9 , when 10 is filled, enter 1;
Octal (octal): 0~7 , full 8 into 1, starting with the number 0;
Hexadecimal (hex): 0~9 and A~F , full 16 into 1, starting with 0x or 0X . Here A~F are not case sensitive. For example: 0x1AF + 1 = 0x21B0
The bottom layer of the computer stores data in complement code !
Positive number three yards in one!
Sign bit: 0-->positive number; 1-->negative number
2.5.1, Binary --> Decimal
2.5.2, decimal --> binary
The inverse of the remainder of division by 2.
2.5.3, hexadecimal <--> octal
Octal/Hexadecimal --> Binary
Binary --> Octal/Hexadecimal