Second, Java language basic knowledge

xiaoxiao2021-03-06  54

Overview:

[Before class thinking] 1. What is the identifier in Java consists of which characters? 2. What reserved words in Java? What kinds of simple data types contain? Definition of various data type variables and constant representations and range of values. 3. What is the priority and automatic conversion rules between the simple data types in Java? Under what circumstances between each data type, automatic conversion, in what circumstances use forced conversion? 4. What operators in Java? What is the priority relationship between these operators? 5. What control statements in Java? Do you understand the syntax rules for each control statement? 6. One-dimensional array and multi-dimensional array in Java do not have to initialize the array dynamic initialization and static initialization? 7. There are two representations of strings in Java. What is the difference between these two representations? 8. How to access strings? How to modify the string? How do I compare two strings?

[Difficult to focus] focus: 1. Deeply understand the concept of each knowledge point, and understand the grammar specification of Java. 2. Proficiency in the use of various data types, representations, and references. 3. Skilled in using various control statements. 4. Can skillfully handle strings. Difficulties: 1. When dynamically initializing complex type arrays, you must first open an element in an array, and then open space for each element. 2. When using the StringBuffer class represents and operators, it is important to note that it can handle a variable string, that is, when inserting characters in the string of the StringBuffer type, the system is automatically assigned an additional space for it. . The String class is not available.

2. Simple data type

2.1.1 Identifiers and reserved words 1. The identifier programmer is named the named identifier (Identifier) ​​used by each element in the program. In the Java language, the identifier is a character sequence that starts with letters, underline (_), US dollar ($), which can be followed by letters, underscores, dollar characters, numbers. For example, Identifier, UserName, User_Name, _sys_val, $ change are legitimate identifiers, while 2mail room #, class is illegal identifier. 2. The reserved word has special meaning and use, which cannot be used as a general identifier. These identifiers are called reserved words, also known as keywords, listed in the Java language all reserved words: Abstract, Break, Byte, Boolean, Catch, Case, Class, CHAR, CONTINUE, Default, Double, Do, Else, Extends, False, Final, Float, For, Finally, IF, IMPORT, IMPLEments, INT, INTERFACE, INSTANCEOF, LONG, LENGTH, NATIVE, NULL, Package, Private, Protected, Public, Return, Switch, Synchronized, Short, Static, Super, True, THIS, THROWS, Threadsafe, Transient, Void, While. The reserved words in the Java language are indicated by lowercase letters. 2.1.2 Data Type Overview

1. The data type of the Java is divided into the data type of the Java language has a simple type and composite type: Simple data types include: Integer: Byte, Short, Int, Long floating point type (floating): Float, Double character type (Textual) : CHAR Boolean type (Logical): Boolean composite data type includes: Class Interface array 2. Constant and variable constant: Final TypeSpecifier varname = value [, varname [= value] ...]; if: final int num = 100; variable: is the basic storage unit in the Java program, which includes variables Name, variable types, and several parts of the scope. Its definition format is as follows: typespecifier varname [= value [, varname [= value] ...]; if: int count; char c = 'a'; variable scope indicates a piece of code to access the variable, declare a variable while Variables are also specified. According to the role field, the variables can have the following: local variables, class variables, method parameters, and exception processing parameters. In a determined domain, the variable name should be unique. Local variables are declared in a block code of the method or method, and its scope is its code block (a block code in the method or method). Class variables are declared in the class instead of declaring in a certain method of classes, its scope is the entire class. Method parameters are passed to the method, and its scope is this method. The exception processing parameter passes to the exception processing code, and its scope is the exception processing section. 2.1.3 Simple Data Type

1. Boolean-Boolean Boolean data has only two values ​​true and false, and they do not correspond to any integer value. Boolean variables are defined as: boolean b = true; 2. Character type - Char character constant: Character constant is a character enclosed in single quotes, such as 'a', 'a'; character variable: type char, which accounts for 16 bits in machine, its range is 0 ~ 65535. Definition of character type variables such as: char c = 'a'; / * Specify variable C is a CHAR type, and the first value is 'a' * / 3. Integral data integers: ◇ Ten becomes integer: such as 123, -456, 0 ◇ Eight reformed integers: starting with 0, such as 0123 means a decimal number 83, and -011 means a decimal number of decimers - 9. ◇ Hexadecimal integer: starting with 0x or 0x, such as 0x123 means a decimal number 291, -0x12 means a decimal number of -18. Integer variable:

type of data

Placeholder

Number range

Byte

8

-27 ~ 27-1

Short

16

-215 ~ 215-1

Int

32

-231 ~ 231-1

Long

64

-263 ~ 263-14. Floating-Point (real) Data realm: ◇ Decimal number consists of numbers and decimal points, and must have decimal points, such as 0.123, 1.23, 123.0, scientific counting form, such as: 123E3 or 123E3, where e or e must There are numbers, and the index behind the E or E must be an integer. ◇ Float type value must be added f or f after numbers, such as float t = 1.23f.

Realistic variable:

type of data

Placeholder

Number range

Float

32

3.4e-038 ~ 3.4e 038

Double

64

1.7e-038 ~ 1.7e 038

2.1.4 Priority relationships and mutual conversion between various types of data in simple data types

Automatic type conversion rules integer, real, characteristic data can be mixed. Different types of data are first converted to the same type, then the operation is performed, and the conversion is from low to advanced;

Operation 1 type

Operation 2 type

Type after conversion

Byte, Short, Char

int

int

Byte, Short, Char, Int

Long

Long

Byte, Short, Char, Int, Long

Float

Float

Byte, Short, Char, Int, Long, Float

Double

Double

3. Forced Type Conversion Advanced Data To convert to low data, it is necessary to use forced type conversion, such as: int i; byte b = (byte) i;

2.2 operators and expressions

2.2.1 Operator

Basic operators are divided by function, with the following categories:

1 arithmetic operator: , -, *, /,%, , -

2 relationship operators:>, <,> =, <=, ==,! =

3 Boolean logic operators:!, &&, ||.

4 operators: >>, <<, >>>, &, |, ^, ~. >>> For the right shift, zero fill, ^ is a position different or

For example: a = 10011101; b = 00111001; there is a result: a << 3 = 11101000; A >> 3 = 11110011 ;>> 3 = 00010011; A & B = 00011001; A | B = 10111101; ~ a = 01100010 A ^ b = 10100100;

5 Assignment operator =, and its extended assignment operator such as =, - =, * =, / =, etc.

6 Catalog of operators?

OPERAND? Statement1: Statement2

Operand is a Boolean expression. If you do STATEMENT1 for true facts, for Statement27 others: including component operators, subscript operators [], instance operator instanceof, memory allocation operator NEW, forced type conversion operator (Type) , Method call operator (), etc. For example: 2.2.2 expression

The expression is a symbol sequence consisting of operands and operators in a certain grammatical form. A constant or a variable name is the simplest expression, which is the value of the constant or variable; the value of the expression can also be used as an operand of other operations to form a more complex expression. 1. The type of expression of the expression is determined by the type of operation and the type of operand, which can be a simple type, or a composite type: Boolean expression: x && y || z; integer expression: Num1 Num2; 2. The priority of the operator performs in accordance with the priority order of the operator, and the peer operator performs from left to right:

Priority

Operator

1

[] ()

2

-! ~ instanceof

3

New (Type)

4

* /%

5

-

6

>> >>> <<

Seduce

> <> = <=

8

= =! =

9

&

10

^

11

|

12

&&&&

13

||

14

?:

15

= = - = * = / =% = ^ =

16

& = | = << = >> = >>> =

First single operator, then multiply the addition and subtraction, then the operator, then compare the operation, and then assign the value. 2.3 control statement

The Java program executes the program flow by controlling the statement, completes a certain task. The program stream is composed of several statements. The statement can be a single statement, such as c = a b, or a composite statement enclosed in braces {}. The control statement in Java has the following categories: ◇ Branch statement: if-else, switch ◇ loop statement: while, do-while, for ◇ Trip statement related to program transfer: BREAK, Continue, Return ◇ Exception processing statement: TRY-CATCH-FINALLY, THROW ◇ Note Statement: // / / * * /, / ** * /

2.3.1 Branch statement branch statement provides a control mechanism such that execution of the program can skip certain statements that do not perform, and rotate the specific statement. 1. Conditional statement if- else if (boolean-expression) statement1; [else statement2;] 2. Multi-branch statement switch switch (expression) {copy value1: statement1; break; case value2: statement2; break; .......... case valuen: statemendn; break; [default: defaultstatement;]} BREAK statement is executing one case After the branch, make the program jump out of the switch statement, that is, the execution of the Switch statement (in some special cases, multiple different case values ​​should perform a set of the same operation, then there is no BREAK). If the default statement is placed in the first line, the program will execute from Default until the first Break appears regardless of whether Expression is matched to the value in CASE. 2.3.2 Cycle Statement 1. While statement [initialization] while (termination) {body; [ip. i]} 2. Do-While statement [initization] do {body; [iethion;]} while (termination); 3. FOR statement for (Initialization; Termination; i) {body;

Iteration: Repeated ◇ For statement is executed, first perform the initialization operation, then determine if the termination condition is met, if satisfied, the statement in the cyclic body is performed, and finally the iterative portion is executed. After completing a cycle, re-judgment the termination condition. ◇ Initialization, termination, and iterative parts can be empty (but the semicolon cannot be provoked), and the three are empty, it is equivalent to an infinite loop. ◇ Multiple operations can be performed using a comma statement in the initialization section and iterative portion. The comma statement is a comma-separated statement sequence.

2.3.3 Jump Statement 1. BREAK statement ◇ In Switch language, the Break statement is used to terminate the execution of the Switch statement. Enable the program to start from the first statement after the Switch statement. ◇ In Java, you can add a bracket for each code block, one code block is usually a piece of code enclosed in braces {}. The format of the marking is as follows: BlockLabel: {CodeBlock} The second usage of the BREAK statement is to jump out the block it specified and execute at the first statement that keeps up with the block.

} 2. The Continue statement Continue statement is used to end this loop, and the following has not been executed under the subsequent statement, then perform the determination of the termination condition to determine whether to continue cycling. For the for statement, before performing the determination of the termination condition, you must first execute iterative statements. Its format is: continue; you can also use the Continue to jump to the outer loop indicated by the brackets, the format is Continue Outerlable;

For example: Outer: for (INT i = 0; I <10; I ) {// outer cycle inner: for (int J = 0; j <10; j ) {// Inner layer cycle IF (i

2.3.4 Exception processing statements include TRY, CATCH, FINALLY, THROW statement; compared to C / C , is a Java unique statement. We will introduce in the fourth lecture.

2.4 array

In the Java language, arrays are the simplest composite data type. Array is a collection of order data. Each element in the array has the same data type, which can uniquely determine the elements in the array with a unified array name and subscript. An array has a one-dimensional number of groups and a multi-dimensional array. 2.4.1 One-dimensional array

◇ Static initialization INT INT INTARRAY [] = {1, 2, 3, 4}; string stringArray [] = {"ABC", "how", "you"}; ◇ Dynamic Initialization 1) Simple type array INT INTARRAY [] INTARRAY = New int [5]; 2) Array String StringArray []; string stringArray = new string [3]; / * For each element in array, the reference space (32-bit) * / StringArray [0] = New string ("how"); // open space StringArray [1] = new string ("are"); // is the second array element StringArray [2] = new string "YOU"); / / open space 3 for the third array element. The reference mode of the reference array element of one-dimensional array element is: arrayname [index] index is the array subscript, which can be integrated or expressed, starting the subscript from 0. Each array has an attribute that Length indicates its length, for example: INTARRAY.LENGTH indicates the length of the array INTARRAY.

2.4.2 multi-dimensional array

1. Definition of two - dimensional array [] []; type [] [] arrayname; 2. Initialization of two - dimensional array ◇ Static initialization INT INTIARRAY [] [] = {{1, 2}, {2, 3}, {3, 4, 5}}; Java language, due to the two-dimensional array as an array Array, array spaces are not continuously assigned, so they do not require the same size of each dimension of the two-dimensional array. ◇ Dynamic Initialization 1) Directly allocate space for each dimension, the format is as follows: arrayname = new type [arraylength1] [ArrayLength2]; int A [] [] = new int [2] [3]; 2) From the highest dimension, Each dimensional allocation space: arrayname = new type [ArrayLength1] []; arrayname [0] = new type [arraylength20]; arrayname [1] = new type [arrayngth21]; ... arrayname [arraylength1-1] = new Type [ArrayLength2N]; 3) Example: Dynamic initialization of the two-dimensional simple data type array, int A [] [] = new int [2] []; a [0] = new int [3]; a [1] = New Int [5]; an array of two-dimensional composite data types must first assign a reference space for the highest dimension, and then then the low-dimensional allocation space. Moreover, you must separate allocation for each array element. For example: string s [] [] = new string [2] []; s [0] = new string [2]; / / for the highest dimensional assignment reference space S [1] = new string [2]; // The highest dimensional assignment reference space S [0] [0] = new string ("good"); / / Separate the space S [0] [1] = new string ("luck"); / / for each array element Each array element separately allocates a space S [1] [0] = new string ("to"); / / Separate space S [1] [1] [1] = New String ("you"); / " / Separate the space 3 separately for each array element. The reference to the two-dimensional array element is: arrayname [index1] [index2], for example: Num [1] [0]; 2.5 string processing

2.5.1 representation of strings

In the Java language, the strings are processed as objects, and class string and stringbuffer can be used to represent a string. (Class name is uppercase letters)

1. Comparison of strings String provides methods provided in String: equals () and equalsignorecase () They are different from operators '= =' implementation. The operator '= =' compares whether the two objects reference the same instance, and equals () and equalsignoreCase () compare whether each of the corresponding one of the two strings is the same.

转载请注明原文地址:https://www.9cbs.com/read-118249.html

New Post(0)