Java-basic statements, java-statements

Source: Internet
Author: User

Java-basic statements, java-statements

1. switch: The accept type is byte short int char (suitable for specific values, but not many values .)
When the condition is true, the statement after case is executed. If break is not met after the execution, or '}', the statement that can be executed will continue and the condition of case will not be judged, until the break is encountered again; or '}'.
2. Loop statement:

① While (conditional expression)
{
Loop body (execution statement );
} ② Do

{
Loop body (execution statement );
} While (conditional expression );
③ For (initialization expression, cyclic condition expression, and cyclic Operation expression)
{
Execute the statement;
}

2.1.while and:

Variables have their own scopes. For: If the incremental definition used to control the loop is defined in the for statement, the variable is only valid in the for statement. After the for statement is executed, the variable is released in the memory.

2.2 for and while can be exchanged. If you need to define cyclic increments, it is more appropriate to use.


3. function: defines an independent applet with specific functions in the class, also known as a method.
3.1 format of the function:
Modifier Return Value Type Function Name (parameter type form parameter 1, parameter type form parameter 2 ,...)
{
Execute the statement;
Return value;
}
Return Value Type: the data type of the result after the function is run.
Parameter type: it is the data type of a formal parameter.
Form parameter: a variable used to store the actual parameters passed to the function when a function is called.
Actual parameter: the specific value passed to the form parameter.
Return: Used to end a function.
Return Value: this value is returned to the caller. (Use the keyword void to identify a function when no specific value is returned .)
3.2. Function overloading: in the same class, more than one function with the same name is allowed, as long as they have different parameter numbers or parameter types.
Specific reload: it is irrelevant to the return value type. It only looks at the parameter list (also related to the parameter order ).
Benefits of heavy load: easy to read, optimized program design.
Overload example:
// Returns two integers and
Int add (int x, int y) {return x + y ;}
// Returns the three integers and
Int add (int x, int y, int z) {return x + y + z ;}
// Returns two decimal places and
Double add (double x, double y) {return x + y ;}

4. Memory Structure:
When a Java program is running, it needs to allocate space in the memory. To improve the computing efficiency, we divide the space into different regions, because each region has a specific data processing and memory management mode.
Stack memory: used to store local variables. when data is used up, the occupied space is automatically released.
Heap memory:
1>. arrays and objects are stored in heap memory in instances created through new.
2>. Each object has a memory address value.
3>. All variables in the object have default initial values.
4>. if the object is not used, it will be recycled by the Garbage Collector within an uncertain period.


5. Array (reference data type): a collection of the same type of data. In fact, arrays are a container.
Array advantages: You can automatically number the elements in the array from 0 to facilitate operations on these elements.
Format 1:
Element type [] array name = new element type [number of elements or array length];
Eg: int [] arr = new int [5];
Format 2:
Element type [] array name = new element type [] {element, element ,....};
Eg: int [] arr = new int [] {1, 2, 5, 3}; // create an object in the heap memory and assign a specific value to each element.
Int [] arr = {1, 2, 5, 3}; // can be abbreviated if the data is clear
New: used to generate a container entity in the heap memory.
Int [] arr: int indicates the element type; [] Table Array; arr indicates the array type. Arr is an array variable in the stack memory, which is actually the address of the array in the heap memory. In addition, arr exists in the stack memory, then we get the container entity address created by new in the heap memory, pointing to the array in the heap memory.

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.