Basic knowledge of Java programming language explained

Source: Internet
Author: User
Tags function definition logical operators

Whether the study of Java programming or other programming language, are from the Zero Foundation, then the need to integrate all the scattered knowledge points, so that the systematic learning of a language, to better get started and mastered, through the basic knowledge of learning and understanding, to learn more in-depth programming code, The path to being a Java development engineer can be even smoother.

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. Identifier: In fact, it is a custom noun in the program. such as the class name, variable name, function name. Contains 0-9, A-Z, $, _;

Attention:

(1) Numbers may not be prefaced.

(2) keywords cannot be used.

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

4, variable: In fact, is a memory storage space, 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 will be stored in this space? The data type.

2. What is 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 operators are used to concatenate two Boolean expressions except!

&: 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: &: Regardless of the left result, the right side participates 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 are calculated.

|| : 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.

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

a=a+b;a=8;

b=a-b;b=3;

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 figure out 2*8=2<<3;

5, statement.

Ifswitchdowhilewhilefor

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, Function: In order to improve the reusability of code, it can be defined as a separate function, which is the embodiment of the function in Java. function is one of the embodiment.

The definition format of a function in Java:

Modifier returns a value type function name (parameter type form parameter 1, parameter type parameter 1, ...) {

Execute the statement;

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: the 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, 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.

Publicstaticinthalfseach_2 (Int[]arr,intkey) {

Intmin,max,mid;

min=0;

Max=arr.length-1;

Mid= (max+min) >>1;//(max+min)/2;

while (Arr[mid]!=key) {

if (Key>arr[mid]) {

min=mid+1;

}

ElseIf (Key<arr[mid])

Max=mid-1;

if (max<min)

return-1;

Mid= (max+min) >>1;

}

Returnmid;

}

Java has 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

Basic knowledge of Java programming language explained

Related Article

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.