JAVA basic grammar
- Comments, identifiers, keywords
- type of data
- Type conversion
- Variables, constants
Annotation
- We usually write code. When the amount of code is relatively small, we can still understand what we wrote, but once the project structure becomes complicated, we need to use comments.
- The comment will not be executed, it is for the people who wrote the code to see
- Writing notes is a very good habit
- Always pay attention to specifications when writing code.
There are three kinds of annotations in java:
-
Single line comment //
-
Multi-line comment/* */
-
Documentation comments/** * */
Identifier
*Keywords
*Keywords cannot be named
All the components of JAVA need names, class names, variable names, and method names are all called identifiers
1. Class name; 2. Method name; 3. Variable name;
Points to note about identifiers
- All identifiers should start with a letter (AZ or az), dollar sign ($), or underscore (_); "case sensitive"
- The first letter can be any combination of letters (AZ or az), dollar sign ($), or underscore (_) or numbers; "special symbols cannot be used"
- You cannot use keywords as variable names or method names.
- Examples of legal identifiers: age, $salary, _value, __1_value
- Examples of illegal identifiers: 123abc, -salary, #abc
- Chinese names can be used, but it is generally not recommended to use it, nor to use pinyin, which is very Low
type of data
Strongly typed language
- The use of variables is required to strictly comply with the regulations, and all variables must be determined before they can be used; the security is high, but the speed is slow.
Weakly typed language
Java data types are divided into two categories
-
Primitive type
Integer
byte 1 byte 8 bits java range 2 (7) -1 to -2 (7)
short 2 bytes are rarely used 2 (15) -1 to -2 (15)
int 4 bytes are most commonly used 2 (31) -1 to -2 (31)
long 8-byte long integer 2 (63) -1 to -2 (63)Decimal
float 4 bytes
double 8 bytesOther
char 2 bytes unsigned 2 (15) -1
boolean 1 byte represents true true or false false -
Reference type
String
String s = " abccccd ";
System.out.println(s.charAt(2));//显示第2坐标符号
System.out.println(s.indexOf("a"));//显示"a"所在坐标位置
System.out.println(s.length());//显示坐标长度
System.out.println(s.equals("ccdda"));//比对是否正确true or false
System.out.println(s.compareTo("ABCD"));//比较大小a<b,返回正值,否者相反。等于为0.
System.out.println(s.toLowerCase());//变成小写"abcd"
System.out.println(s.toUpperCase());//变成大写"ABCD"
System.out.println(s.trim());//当有空格存在时" abcd ",删除空格。
System.out.println(s.split("b"));//从指定位置切开
What is byte
-
Bit: It is the smallest unit of data storage in the computer. 11001100 is an eight-bit binary number.
-
Byte (byte): It is the basic unit of data processing in a computer. It is customarily represented by a capital B.
-
1B (byte, byte) = 8bit (bit)
-
Characters: Refers to letters, numbers, words and symbols used in computers
-
1bit means 1 bit
-
1Byte means a byte 1B=8b
-
1024B=1KB
-
1024KB=1M
-
1024M=1G
Integer expansion:
Base:
-
Binary 0b (Integer.toBinaryString) binary code
1010 Conversion 0* 2(0)+1* 2(1)+0* 2(2)+1* 2(3)=0+2+0+8===>10
-
Decimal
(1011) 2 power, convert 1* 2(3)+0* 2(2)+1* 2(1)+1* 2(0)=8+0+2+1=11(11) 10 times square
123 conversion 3* 10(0)+2* 10(1)+1* 10(2)=3+20+100
-
Octal 0 (0-7)
067 Convert 7* 8(0)+6* 8(1)=48+7=(55)10 power
-
Hexadecimal 0x 0-9 AF(10-16) (Integer.toHexString) Hexadecimal code
Convert 10* 16(0)+15* 16(1)+14* 16(2)+9* 16(3)=10+240+3584+36864
Base conversion
N-base conversion to decimal
Formula: the first digit n(0)+the second digit n(1)+…+the xth digit* n(x-1) power**
Binary to Decimal
Decimal to Binary
Decimal to Binary
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
10 1010
11 1011
12 1100
13 1101
14 1110
15 1111
Decimal to binary
-
Short division:
35: (35/2=17 (remaining 1)/2=8(1)/2=4(0)/2=2(0)/2=1(0)/2=0(1) reverse reading) =100011
100011 1* 2(0)+1* 2(1)+0* 2(2)+1* 2(5)=1+2+32=35 (forward) -
Find the number of powers:
35: 35-2(5)=3-2(1)=1-2(0)=100011 (add 1 to the power of a number, add 0 if there is no)
216:216-2(7)=88-2(6)=24-2(4)=8-2(3)=11011000
Negative conversion
Java uses two's complement method
0 0 0 0
0001 1
0010 2
0011 3
0100 4
0101 5
0110 6
0111 7
consider the beginning of 1 as a negative number
1000 -8
1001 -7
1010 -6
1011 -5
1100 -4
1101 -3
1110 -2
1111 -1
How many numbers can a 4-bit two's complement code represent? 2(4)=16
maximum number? 2(4-1)-1 ====》7 is the
smallest number? -2 (4-1) ====》-8
Assuming that a certain language expresses numbers in 8-bit two's complement, what is the range?
2 (8-1) -1 to -2 (8-1)
Two's complement indicates that the data range is related to the number of bits. If the number of bits is n,
then the range is -2(n-1) to 2(n-1)-1
Integer (int) in java is the 32-bit two’s complement representation that
java int ranges from -2 (31) to 2 (31) -1
Summary 2: 4 digits look at
7
0111
Negative = = =》1000 +1 = = =》1001 -7 1001 Negative ===》0110 +1 = = =》0111 Negative number = Positive number negative +1
positive number = Negative number is negated +1
Floating point expansion:
- float; double is not accurate enough; in the actual process, the floating-point number is infinite and discrete, but the float is finite, it will automatically round off the error, which is approximately close, but not equal;
- Banks and other accurate calculation tools are best to use BigDecimal mathematical tools
- Accurate reconciliation is best to avoid using floating point numbers for comparison altogether
- Accurate reconciliation is best to avoid using floating point numbers for comparison altogether
- Accurate reconciliation is best to avoid using floating point numbers for comparison altogether
Character expansion:
All characters are essentially numbers
Java uses Unicode character set encoding, the format is'\u0000'-'\uffff'
(Unicode is a universal fixed-length character set, all characters are 16 bits, 2 bytes)
Use escape characters for characters that are inconvenient to enter
Escape character:
Such as:'\n' "line feed",'\t' "space",'\','\b','\r','"','\u4e2d'.
Number code: '0': 48, '1': 49...
English code:'A': 65,'B': 66...
Lowercase English:'a': 97,'b': 98...
Exercise:
public void zifu(String a){
char[] zi = a.toCharArray();//(String)a转换字符型数组toCharArray,并赋值
int mu = 0; //字母
String m1 = "";
int s = 0; //数字
String s1 = "";
int k = 0; //空格
int f = 0; //其它
String f1 = "";
for (int i = 0; i <zi.length ; i++) {
int p = (int)zi[i]; //将zi的每一位数组转换为Int数值,用数值进行比对
if ((65 <= p && p<=90)||(97<= p && p <= 122)) {
//A-Z字母为65-90,a-z字母为97-122
mu++;
m1 = m1+zi[i]+" ";
}else if (48<= p && p <= 57){
//0-9为48-57
s++;
s1 = s1 + zi[i] + " ";
}else if (p==32){
//空格为32
k ++;
}else {
f++;
f1 = f1 + zi[i] +" ";
}
}
System.out.println("存在的字母个数:" + mu);
System.out.println("存在的字母为:" + m1);
System.out.println("存在的数字个数:" + s);
System.out.println("存在的数字为:" + s1);
System.out.println("存在的字符个数:" + f);
System.out.println("存在的字符为:" + f1);
System.out.println("存在的空格个数:" + k);
}
public static void main(String[] args) {
System.out.println("请输入字符串:");
Scanner scanner = new Scanner(System.in);
String a = scanner.nextLine();
ZiFuSuZhu ziFuSuZhu = new ZiFuSuZhu();
ziFuSuZhu.zifu(a);
scanner.close();
}
Type conversion:
-
Since Java is a strongly typed language, type conversion is required when some operations are required.
low------------------------------------------------- ------------->High
byte, short, char -> int -> long -> float -> double
(Decimals have higher priority than integers)
-
In operation, different types of data are converted to the same type first, and then the operation is performed.
-
Forced type conversion high--low
-
Automatic type conversion
Direct input from low to high without forced conversion
-
note
- Cannot convert boolean values
- Cannot convert object type to irrelevant type
- When converting high-capacity to low-capacity, forced conversion is required
- There may be memory overflow or accuracy problems during conversion!
char to int
Overflow problem
variable
-
What is a variable: the amount that can be changed!
-
Java is a strongly typed language, and every variable must be declared of its type.
-
Java variable is the most basic storage unit in the program, and its elements include variable name, variable type and scope.
-
Precautions:
- Every variable has a type, and the type can be a basic type or a reference type.
- The variable name must be a legal identifier.
- A variable declaration is a complete statement, so every declaration must end with a semicolon.
Variable scope
-
Class variable
In the class, the whole class can be used, static must be added in front; "static int a = 666;"
-
Instance variable
In the class, subordinate to the object; "int age;//0"
If you do not initialize yourself, the default value of this type is 0,0.0
Boolean value: the default is false; except for the basic types, the other default values are all null;
-
Local variable
In the method, the value must be declared and initialized when used; "int cba = 666;"
constant
-
Constant: The value cannot be changed after initializing! The value that will not change.
-
The so-called constant can be understood as a special variable. After its value is set, it is not allowed to be changed during the running of the program.
final 常量名 = 值; final double PI = 3.14;
-
Constant names generally use uppercase characters.
Variable naming convention
-
All variables, methods, class names: see the name know what it means
-
Class member variables: first letter lowercase and camel case principle: monthSalary
-
Capitalize the first letter of the following words except for the first word
-
Local variables: lowercase initials and camel case
-
Constants: uppercase letters and underscore MAX_VALUE
-
Class name: initial capitalization and camel case principle: Man, GoodMan
-
Method name: lowercase initials and camel case principle: run(),runRun()