Java syntax BASICS (Summary), java syntax basics Summary

Source: Internet
Author: User
Tags bitwise operators variable scope

Java syntax BASICS (Summary), java syntax basics Summary

1. Keywords: words with special meanings given by a language.

Reserved Words: Actually, words that have not been given a special meaning but will be used in the future.

2. identifier: it is actually a custom term in the program. For example, class name, variable name, and function name. Contains 0-9, a-z, $, and ,_;

Note:

1), cannot begin with a number.

2). You cannot use keywords.

3. Constant: data that will not change in the program.

4. Variable: it is actually a storage space in the memory used to store constant data.

Function: facilitates operations. Because some data is uncertain. Therefore, determine the terms and storage space of the data.

Feature: variable space can be reused.

When to define variables? Variables are defined as long as the data is uncertain.

What are the elements required for variable space development?

1. What data is stored in this space? Data type.

2. What is the name of this space? Variable name.

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

Scope and lifetime of variables:

Variable scope:

The scope starts from the position where the variable is defined and ends with the braces where the variable is located;

Lifecycle:

The variable is active in the memory from the defined position;

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

 

Data Type:

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

2): Reference Data Types: arrays, classes, and interfaces.

Level from low to high: byte, char, short (these three levels) --> int --> float --> long --> double

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

Forced type conversion: under what circumstances? Assign a high-level number to a variable with a lower level of this number;

Operator number:

1) Arithmetic Operators.

+-*/%: Any integer modulo 2 is either 0 or 1, so you only need to change the modulus to implement the switch operation.

+: Connector.

++ ,--

2) value assignment operator.

= + =-= * =/= % =

3) Comparison operators.

Feature: This operator is characterized by either true or false.

4). logical operators.

& | ^! & |

Except for the logical operators! Both are used to connect two boolean expressions.

&: The result is true only when both sides are true. Otherwise, it is false.

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

^: Exclusive or: it is a little different from or.

If the results on both sides are the same, the value is false.

The results on both sides are different, true.

& Difference: &: no matter what the result on the left is, the right is involved in the operation.

&: Short Circuit and operation. If the value is false on the left, no parameters and operations are performed on the right.

| And | difference: |: both sides of the operation.

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

5) bitwise operators: operators used to operate binary digits.

& | ^

<>>>> (Shifts right unsigned)

Exercise: swap the data of two variables. Third-party variables are not required.

Int a = 3, B = 5; --> 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 =

A = a ^ B; // a = a ^ B ^ a = B;

Exercise: efficiently calculate 2*8 ------------------> the test of the displacement operation. Its exposure is not low in the java basic interview.

5. Statements.

If switch do while

When are these statements used?

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

However, it is recommended to use a switch, which is more efficient.

Switch (variable ){

Case value: the statement to be executed; break;

...

Default: The statement to be executed;

}

Working principle: Compare the values of variables in parentheses with the values after case in sequence, which is the same as the values after case

Execute the statement following the case. If the statement does not have the same case, execute the statement following the default statement;

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

2): the variables in parentheses after the switch should be byte, char, short, or int;

3): The default statement can be written at any position in the switch structure. If the default statement is placed in the first row, whether the expression matches the value in the case or not, the program runs from default until the first break appears.

2) When determining the data range and obtaining the boolean Type of the judgment result, use if.

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

While and for can be exchanged.

The difference is: If you need to define a variable to control the number of cycles. We recommend that you use. Because the for loop is complete, the variable is released in the memory.

Break: Act on the switch and loop statements, used for jumping out, or called the end.

When the break statement exists independently, do not define other statements below, because the execution fails and the compilation will fail. When loops are nested, break only jumps out of the current loop. To jump out of the nested External Loop, you just need to name the loop. This name is called a label.

Code snippet:
Z: // for cycle label
For (int x = 0; x <3; x ++ ){
For (int y = 0; y <2; y ++ ){
// The function of ending the entire loop body without a label is to end the loop inside the loop.
If (x = 1) break;
// Skip the statement after break with a label, return to the cycle at the label position, and continue the next condition judgment of the cycle,
// You have decided whether to execute the loop body.
If (x = 2 & y = 1) break z;
}
}

Continue: applies only to the loop structure and continues to be used.

Purpose: end this cycle and continue the next cycle. If this statement exists separately, the following statements cannot be defined and cannot be executed.

6. function: to improve code reusability, it can be defined as a separate function, which is embodied in java functions. Function is one of the manifestations.

Definition Format of functions in java:

Modifier Return Value Type Function Name (parameter type form parameter 1, parameter type form parameter 1 ,...) {

Execute the statement;

Return value;

}

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

If the return value type of a function is void, the return statement can be omitted without being written. The system will automatically add the return Statement to the function.

Return: end function. End function.

How to define a function?

A function is actually a function. Defining a function is to implement a function. It is accomplished through two definitions:

1) Clarifying the result of the operation of this function is actually clarifying the return value type of this function.

2) Is there any unknown content involved in the operation during the implementation of this function? In fact, it is necessary to clarify the parameter list of this function (parameter type & number of parameters ).

Functions:

1) define functions.

2) It is used to encapsulate code to improve code reusability.

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

Main function:

1) Ensure that the class runs independently.

2) because it is the entry of the program.

3) because it is called by jvm.

Why is the function definition name?

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

2) in order to clarify the function functions by name, in order to increase the readability of the Code.

The definition of overload is: in a class, if two or more functions with the same name appear, as long as the number of their parameters or the parameter type is different, this function is called overload.

How to differentiate overload: When a function has the same name, it only looks at the parameter list. It has nothing to do with the return value type.

7. Number Groups: a container used to store the same type of data. Benefit: the data in the container can be numbered, starting from 0. An array is used to encapsulate data. It is a specific object.

How to present an array in java? Two forms.

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 ...};

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

// Binary search method. Prerequisites: Elements in the array must be ordered.

Public static inthalfSeach_2 (int [] arr, int key ){

Int min, max, mid; // defines the minimum, maximum, and intermediate number.

Min = 0; // the minimum value is 0.

Max = arr. length-1; // the maximum length of the array-1

Mid = (max + min)> 1; // (max + min)/2; // The median is the maximum and minimum values divided by 2.

While (arr [mid]! = Key) {// if the value in the array is not equal to the key

If (key> arr [mid]) {// if key> Value

Min = mid + 1;

}

Elseif (key <arr [mid])

Max = mid-1;

If (max <min)

Return-1;

Mid = (max + min)> 1;

}

Return mid;

}

Knowledge Development:

Java memory.

1: Register. 2: local method area. 3: Method area. 4: Stack. 5: heap.

STACK: stores all local variables (variables defined in the function, parameters in the function, and variables in the statement );

As long as the region where the data operation is completed ends, the data will be released.

Heap: used to store arrays and objects, that is, objects. What is entity? Is used to encapsulate multiple data.

1: each entity has a memory first address value.

2: all variables in heap memory have default initialization values. Different data types have different values.

3: Garbage collection mechanism.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.