Java Syntax Basics

Source: Internet
Author: User

Key words

It's a word that gives special meaning to a language.

Reserved words: In fact, there is no special meaning yet to prepare words to be used in the future

Identifiers

is actually a custom noun in the program such as the class name variable name Function name contains 0-9, a -Z, $, _

Note: numbers may not start with a keyword

Constant

Data that is not changed in the program

Variable

is actually a storage space in memory for storing constant data.

Function: Easy to operate because some data is uncertain, so determine the noun and storage space of the data

Features: Variable space can be reused

Define variables as long as the data is uncertain

1. Variable space to open up the necessary elements

Initialization values for variable name variables of data type

2. Scope and lifetime of variables

Scope of the variable: the scope starts at the position defined by the variable to the end of the curly brace where the variable is located

Life cycle: The variable starts at the defined position and disappears in memory when the variable reaches its scope.

3. Data type

Basic data type: Byte, short , int, long, float, Double, char, boolean

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-level to high-level system auto-turn

Coercion type conversion: assigns a high-level number to a variable that is lower than that number

4. Operational Symbols

A: Arithmetic operators

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

B: Assignment operator

=  += -= *= /= %=

C: Comparison operators

features : The result of the operation is either true or false

D: Logical operators

& |   ^  ! && | |

logical operators except ! All are used to connect two boolean-type expressions

&: only both sides are true the result is true otherwise it is false

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

^: XOR or and or something different. Both sides turned out the same is false True if the results are not the same on both sides

& and && difference: & no matter what the left result is, the right side participates in the Operation &&  short circuit and   if the left side is false so right no arguments and operations

| and || Difference: | Both operations on both sides | | Short Circuit or If the left side is true Then the right side does not participate in the Operation

E: Bitwise operators

Operators for manipulating bits & | ^ << >> >>> ( unsigned right shift)

Statement

IF, switch, do and while, for

when judging a fixed number of values You can use the if or switch

However, it is recommended that switch efficiency is relatively high

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 in the case and the values behind the case.   to execute case after the statement that does not have the same default after the statement

detail:   break can be omitted if omitted, execute until break switch One of four types default span class= "s2" style= "line-height:1.5;" > can be written in Switch any position in the structure if the default statement on the first line   no matter expression

when judging the data range Gets the Boolean type that is used to determine The result of the operation if

When some statements need to be executed many times just use the loop structure .

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 complete variable is released in memory

Break : action on switch and loop statement for jumping out or ending

The break statement does not define a different statement when it exists separately because execution does not fail when the loop is nested, break only jumps out of the outer loop that is currently in the loop to jump out of the nest as long as the loop is called the name of the label

continue: only for the loop structure to continue recycling

Function: End this loop to continue the next loop when the statement exists independently of the face can not be defined statement does not execute

Function

In order to improve the reusability of the code, it can be defined as a separate function the function of the function is one of the embodiment of Java

The definition format of functions in Java

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

Execute the statement;

return return value;

}

The return value type that is returned when the function does not have a specific return value is represented by the void keyword

If a function's return value type is void, The return statement can omit the non-writable system that will automatically add

return Effect: End Function End

1. Define a function

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

clear the result of the operation of the function is actually defining the return value type of the function.

is there any unknown content involved in the operation during the implementation of this function? in fact, it is clear that the parameter list of this function (parameter type & parameter number )

2. Function:

Used to define features

For encapsulating code to improve the reusability of code

Note: Functions that can only be called function cannot define functions

3. Main function:

Ensure the independent operation of this class

Because it's the entrance to the program.

Because it is called by the JVM

4. function definition Name:

In order to mark the function, it is convenient to call

in order to be able to define function by name to increase the readability of the Code

overloading is defined as if two or more than two functions of the same name appear in a class, as long as the number of arguments or the type of the parameter is different, it is called the function overload.

how to differentiate overloads : Just look at the argument list and return value type when the function has the same name okay

Array

A container for storing data of the same type benefits: data in the container can be numbered starting with 0 The array is used to encapsulate the data is a concrete entity

To represent an array in Java:

A: Element type [] variable name = new element type [number of elements ];

B: Element type [] variable name = {element 1, Element 2 ...}; element type [] variable name = new element type []{ element 1, element 2 ...};

The binary lookup method must have the premise that the elements in the array are ordered

 Public Static intHalfseach_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; }Else if(key<Arr[mid]) Max= Mid-1; if(max<min)return-1; Mid= (max+min) >>1; }    returnMid;} 

Java has 5 pieces of memory: Register Local Method area method area Stack heap

Stacks: stores are local variables ( variables in a parameter statement on a variable function defined in a function ) as long as the data operation completes the area where the data will be released

heap: used to store arrays and objects that is entities what is an entity? Is the one used to encapsulate multiple data

A: Each entity has a memory header address value

B: Variables in heap memory have default initialization values because different values for data types are not the same

C: Garbage collection mechanism

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.