second: Java Syntax basics:

Source: Internet
Author: User

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 , identifiers : The names of packages, classes, methods, parameters, and variables in Java can be made up of any order of uppercase and lowercase letters, numbers, underscores (_), and dollar signs ($), but identifiers cannot start with numbers and cannot be reserved keywords in Java.

• The following is a valid identifier:

Yourname

Your_Name

_yourname

$yourname

• The following are illegal identifiers:

Class

67.9

Hello Careers

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 the && difference: &: Regardless of the left result, 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 the || 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.

&

Example: 129&128, The value of "a" is 129, converted to binary is 10000001, and "b" value is 128, converted to binary

Is 10000000. According to the Operator's operation law, only two bits are 1, the result is 1, you can know the result is

10000000, or 128.

|

Example: 129|128, The value of a is 129, converted to binary is 10000001, and the value of B is 128, converted to binary is 10000000, according to or operator of the operation law, only two bits have a 1, the result is 1, you can know the result is 10000001, That is, 129.

^

Example: 15^2 xor operator XOR operator is denoted by the symbol "^", its operation law is: two operands of the bit, the same result is 0, different results are 1.

Analysis of the above program segment: the value of a is 15, converted to binary 1111, and the value of B is 2, converted to binary 0010, according to the different or the operation law, can be derived from the result of 1101 is 13.

<< >> >>> (unsigned right Shift)

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. It is recommended to use switchfor a 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, or if the default statement is placed in the first row, regardless of expression and case in the value is matched, The program will be default start execution until the first one break appear.

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 return value type function name ( parameter type form parameter 1, parameter type form 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 , one-dimensional 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 ...} ;

3), system.arraycopy (source,0,dest,0,x): The meaning of the statement is: copy the source array from subscript 0 to the beginning of the X element to the target array, from the target array of subscript 0 corresponding to the location to start access.

4), Arrays.sort (array Name) is a sort of operation, but this method is java.util in this package, so you need to import it First.

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

second: Java Syntax basics:

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.