Java Learning (ii) grammar knowledge

Source: Internet
Author: User
Tags array length

Second: Java Syntax basics:

1 , the keyword: in fact, a language has given special meaning to the word.

Reserved words: In fact, there is no special meaning, but prepare the words to be used later.

2 , the identifier: is actually a custom noun in the program. such as the class name, variable name, function name. Contains 0-9, A-Z, $, _;

Attention:

1 ), the number may not begin.

2 ), you cannot use keywords.

3 , constant: is the data that will not change in the program.

4 , a variable: in fact, a storage space in memory that is used to store constant data.

Function: Convenient for operation. Because some data is uncertain. So determine the noun and storage space for the data.

Features: Variable space can be reused.

When do I define a variable? as long as the data is uncertain, the variable is defined.

What are the necessary elements for the opening of variable spaces?

1, what data does this space store? The data type.

2, what's the name of this space? The variable name.

3, what is the first data for this space? The initialization value of the variable.

Scope and lifetime of variables:

Scope of the variable:

The scope starts at the position defined by the variable and ends with the curly brace where the variable is located;

Life cycle:

The variable is alive in memory starting at the defined position;

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

Data type:

1 ): Basic data type : Byte, short, int, long, float, double, char, Boolean

2 ): Reference data type : Array, class, interface.

level from low to High:Byte,char,short (these three levels)-->int-->float-->long-->double

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

coercion Type conversions: When to use? Assign a high-level number to a variable that has a low rank for that number;

Operation Symbols:

1), arithmetic operator.

+-*/% : any integer modulus 2 is not 0 is 1, so as long as the change is the module can be implemented switch operation.

+: Connector.

++,--

2), assignment operator.

=  += -= *= /= %=

3), comparison operator.

Features: This operator is characterized by the result of the operation, either True or false.

4), logical operator.

& |   ^  ! && | |

Logical operator except! are used to concatenate two Boolean expressions.

&: Only both sides are true and the result is true. Otherwise, it is false.

|: If both sides are false the result is false, otherwise it is true

^: xor: And or a little different.

On both sides, the result is false.

It is true that the results are different on both sides.

& and && difference: &: No matter what the left result is, the right side is involved in the operation.

&&: Short circuit with , if the left is false, then the right side is not a parameter and operation.

| and | | difference:|: Both sides of the operation.

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

5), bitwise operator: operator used to manipulate bits.

& | ^

<< >> >>> (unsigned Right shift)

Exercise: Swap data for two variables. No third-party variables are required.

int a = 3,b = 5;-->b = 3,a = 5;

A = a + B; A = 8;

b = a A; 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: efficiently Calculate 2*8 = 2<<3;

5 , statement.

If switch do and while and for

When do these statements work?

1), when judging the fixed number of values, you can use if, you can also use switch.

However, it is recommended to use switch for relatively high efficiency.

Switch (variable) {

Case value: The statement to be executed;

...

Default: the statement to execute;

}

How it works: compare the values of the variables in parentheses with the values after the case, and the values after the case are the same

Executes the statement following the case, and if there is no same, execute the statement after the default;

Details: 1): Break can be omitted, if omitted, has been executed until a break is encountered;

2): The variable in parentheses after switch should be one of the byte,char,short,int four types;

3): Default can be written anywhere in the switch structure, if the default statement is placed in the first row, regardless of whether expression matches value in case, the program starts execution from default until the first break occurs.

2), when judging the range of data, get the Boolean type of judgment operation, you need to use if.

3), the loop structure is used when certain statements need to be executed many times.

While and for can be interchanged.

The difference is that if you need to define a variable control loop number. It is recommended to use for. Because the For loop is complete, the variable is freed in memory.

Break : acts on a switch, and a loop statement, used to jump out, or to be called an end.

When a break statement exists separately, the following do not define other statements, because the compilation fails because it is not executed. When a loop is nested, break only jumps out of the current loop. To jump out of a nested outer loop, just give the loop a name, which is called a label .

continue: only for loop structure, continue to recycle.

Effect: End this cycle and continue the next cycle. When the statement is present alone, the statement below cannot be defined and cannot be executed.

6 , Functions: in order to improve the reusability of code, it can be defined as a separate function, the function is the embodiment of the function in Java. function is one of the embodiment.

Java The definition of the function in the format:

modifier Returns a value type function name (parameter type form parameter 1, parameter type parameter 1, ...) {

Execute the statement;

return return value;

}

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

If a function's return value type is void, the return statement can be omitted, and the system will automatically add it to you.

return effect: End Function. End Function.

How do I define a function?

function is actually a function, the definition function is implementation function, through two clear to complete:

1), clear the function of the results of the operation, in fact, is to clarify the function of the return value type.

2), in the process of implementing this function, whether there is unknown content involved in the operation, in fact, is to clarify the function of the parameter list (parameter type & number of parameters).

Role of the function:

1), for defining functions.

2), used to encapsulate code to improve the reusability of code.

Note: Functions can only be called, and functions cannot be defined.

Main function:

1), to ensure the independent operation of the class.

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

3), because it is called by the JVM.

What is the function definition name?

A: 1), in order to mark the function, it is convenient to call.

2), in order to use the name can be clear function functions, in order to increase the readability of the code.

overloading is defined as: In a class, if there are two or more than two functions with the same name, as long as they have the number of arguments, or the type of the parameter is different, it can be called the function overload.

How to differentiate overloads: when the function has the same name, only the argument list is viewed. And the return value type is okay.

7 , an array: a container for storing data of the same type. Benefit: You can number the data in the container, starting with 0. Arrays are used to encapsulate data, which is a specific entity.

How do you represent an array in Java? Two forms of expression.

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

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

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

---------------------------------------------------------

// Two-point lookup method. There must be a precondition: the elements in the array should be ordered.

public static int halfseach_2 (int[] arr,int key) {

int min,max,mid;//defines the minimum, maximum, middle number

Min = 0;//Minimum is 0

max = arr.length-1;//Maximum array length-1

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

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

if (Key>arr[mid]) {//if key> medium value

Min = mid + 1;

}

else if (Key<arr[mid])

max = mid-1;

if (max<min)

return-1;

Mid = (max+min) >>1;

}

return mid;

}

---------------------------------------------------------

Java 5 pieces of memory.

1: register. 2: Local method area. 3: method area. 4: stack. 5: heap.

Stacks: stores are local variables (variables defined in functions, arguments on functions, variables in statements);

The data is freed as long as the area where the data operation is completed ends.

heap: used to store arrays and objects, that is, entities . What is an entity? is used to encapsulate multiple data.

1: Each entity has a memory header address value.

2: variables in heap memory have default initialization values. Because the data types are different, the values are not the same.

3: garbage collection mechanism.

Java Learning (ii) grammar knowledge

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.