Joke Collection Website - Public benefit messages - What is the basis of java grammar?

What is the basis of java grammar?

1, key words: actually, it is a word endowed with special meaning by a certain language.

Reserved words: actually, they are words that have not been given special meaning, but are prepared for future use.

2.Marker: actually, it is a term defined in the program. Such as class name, variable name and function name. Contains 0-9, a-z, $,_;

note:

1), the number cannot start.

2), keywords can not be used.

3. Constants: immutable data in the program.

4. Variable: Actually, it is a storage space in memory for storing constant data.

Function: Easy to operate. Because some data are uncertain. So determine the noun and storage space of the data.

Features: Variable space can be reused.

When do you define variables? As long as the data is uncertain, define variables.

What elements are needed for the development of variable space?

1, what data should be stored in this space? Data type.

2. What is this space called? Variable name.

3. What is the first data in this space? The initialization value of the variable.

Range and life of variables:

Range of variables:

The range starts from the position defined by the variable and ends with a pair of braces where the variable is located;

Life cycle:

Variables exist in memory from the defined position;

When the variable reaches its scope, it disappears in memory;

Data type:

1): Basic data types: byte, short, int, long, float, double, char, boolean.

2): Reference data types: array, class and interface.

The rank from low to high is: byte, char, short (these three ranks)-> int->; Floating-> long->; double;twofold

Automatic type conversion: from low level to high level, the system automatically converts;

Forced type conversion: when to use it? Assign a high-level number to a variable with a low-level number;

Operation symbol:

1), arithmetic operator.

+-*/%:Any integer modulo 2 is either 0 or 1, so the switching operation can be realized by changing the modulo.

+:connector.

++, -

2), assignment operator.

= += -= *= /= %=

3), comparison operator.

Characteristics: The characteristic of this operator is that the operation result is either true or false.

4), logical operators.

& amp| ^ ! & amp& amp||

Except for logical operators! External is an expression used to connect two Boolean types.

& Only when both sides are true can the result be true. Otherwise it's fake.

|: As long as both sides are false, the result is false, otherwise it is true.

XOR: It's a little different from OR.

If the results of both sides are the same, it is false.

If the results on both sides are different, that's true.

& amp & & Difference:&; No matter what the result on the left is, the right side participates in the operation.

&&:Short circuit. If the left side is false, there are no parameters and operations on the right side.

| and |||| :|: Two-sided operation.

||: Short circuit or, if the left side is true, then the right side does not participate in the operation.

5) Bit Operator: An operator used to manipulate binary bits.

& amp|

& lt& lt& gt& gt& gt& gt& gt (unsigned right shift)

Exercise: Exchange data of two variables. No third-party variables are required.

int a = 3,b = 5; -& gt; b = 3,a = 5;

a = a+b; a = 8;

b = a-b; b = 3; c

a = a-b; a = 5;

a = a ^ b; //

b = a ^ b; //b= a ^ b ^ b = a

a = a ^ b; //a = a ^ b ^ a = b;

Exercise: The test of calculating 2 * 8-efficient > permutation operation shows that its exposure is not low in the basic interview of java.

5. statement.

If switch does while while for

When are these statements used?

1), you can use if or switch when judging a fixed number of values.

But it is recommended to use switch, which is relatively efficient.

Switch (variable)

Case value: the statement to be executed; Break;

Default value: statement to be executed;

}

How it works: Compare the values of the variables in brackets with the values after case in turn, and which case has the same value.

After which case the statement is executed, and if it is not the same, the statement is executed after default;

Details: 1): break can be omitted. If omitted, it will be executed until a breakpoint is encountered;

2):2) Variables in parentheses: switch should be one of four types: byte, char, short and int;

3): The default can be written anywhere in the switch structure; If the default statement is placed on the first line, regardless of whether the expression matches the value in case or not, the program will execute from the default value until the first delimiter appears.

2) if is needed when judging the data range and obtaining the Boolean type of the judgment operation result.

3) When some statements need to be executed many times, use a circular structure.

While and for are interchangeable.

The difference is that if you need to define variables to control the number of loops. It is recommended to use. Because the for loop has been completed, the variable is released into memory.

Break: acts on switch and loop statements to jump out or end.

When the break statement exists alone, do not define other statements below, because if it cannot be executed, the compilation will fail. When loops are nested, break only jumps out of the current loop. To jump out of the nested outer loop, just give the loop a name, which is called a label.

Code snippet:

Z:// stands for loop label.

for(int x = 0; x & lt3; x++){

for(int y = 0; y & lt2; y++){

//What is not marked is the function that ends the whole loop body, and which loop ends in that loop.

If(x== 1) is broken;

//Use the label break to skip the statement, use the label position to return to the loop, and continue the conditional judgment of the loop next time.

//It has been decided whether to execute the loop body.

if(x = = 2 & amp; & ampy = = 1)break z;

}

}

Continue: only works on the loop structure and continues the loop.

Function: End this cycle and continue the next cycle. When a statement exists alone, the following statement cannot be defined and executed.

6. Function number: In order to improve the reusability of the code, it can be defined as a single function, and the embodiment of this function is the function in java. Function is one of the manifestations.

Definition format of functions in java:

Modifier return value type function name (parameter type parameter 1, parameter type parameter 1, …) {

Execute the statement;

Return returns the value;

}

When the function does not have a specific return value, the return value type is represented by the void keyword.

If the return value type of the function is void, you can omit the return statement and automatically add it for you.

The function of return is to end the function. End function.

How to define a function?

Function is actually a function, and defining a function is to realize a function, which is accomplished through two clear definitions:

1), to clarify the operation result of this function is actually to clarify the return value type of this function.

2) Whether there are any unknown contents related to the operation in the process of realizing this function is actually to clarify the parameter table (parameter type & parameter number) of this function.

Function:

1), which is used to define the function.

2) It is used to encapsulate code and improve the reusability of code.

Note: Only functions can be called, and functions cannot be defined.

Main functions:

1) to ensure the independent operation of this class.

2), because it is the entrance of the program.

3) Because it is being called by jvm.

What is the name of the function definition?

Answer: 1), in order to mark this function, it is convenient to call.

2) In order to clarify the function by name and increase the readability of the code.

The definition of overload is: if two or more functions with the same name appear in a class, as long as their parameter numbers or parameter types are different, the function can be said to be overloaded.

How to distinguish overloading: When a function is renamed, just look at the parameter list. Regardless of the type of return value.

7. Several groups: containers that store the same type of data. Benefits: You can number the data in this container, starting with 0. An array is used to encapsulate data and is a concrete entity.

How to represent an array in java? Two forms of expression.

1), element type [] variable name = new element type [number of elements];

2), variable name of element type [] = {element 1, element 2...};

Element type [] Variable name = new element type [] {element 1, element 2...};

-

//method of bisection method. There must be a premise: the elements in the array should be orderly.

public static in halfseach _ 2(int[]arr,int key){

Int min, max, mid// define the minimum, maximum and intermediate numbers.

min = 0; //The minimum value is 0

max = arr . length- 1; //The maximum length is-1.

Mid = (maximum+minimum) >> 1; //(max+min)/2; //The middle number is the maximum plus the minimum divided by 2.

while(arr[mid]! =key){// If the middle value of the array is not equal to key,

if(key & gt; arr[mid]){//If key >; average value

min = mid+ 1;

}

Elseif (key

max = mid- 1;

If (max.

return- 1;

Mid = (maximum+minimum) >> 1;

}

Return to mid

}

Knowledge expansion:

Java memory.

1: Register. 2. Local method area. 3: Method area. 4: stacking. 5: heap.

Stack: Store all local variables (variables defined in functions, parameters on functions, variables in statements);

As long as the area where the data operation is finished, the data will be released.

Heap: used to store arrays and objects, that is, entities. What is an entity? Which is used for encapsulating a plurality of data.

1: Each entity has a memory header address value.

2. Variables in heap memory