Home  >  Article  >  Java  >  Java basic syntax

Java basic syntax

巴扎黑
巴扎黑Original
2017-07-24 14:12:141048browse

1.switch: The accepted type is byte short int char (suitable for specific values, but not many values.)
When the condition is established, execute the statement after the case. If no break is encountered after execution; or '}', Then the executable statements will continue to be executed. At this time, the condition of the case will not be judged until 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; loop conditional expression; operation expression after loop)
 {
  Execution statement;
 }

2.1. The difference between while and for about variables:

  Variables have their own scope. For for: If the increment used to control the loop is defined in the for statement, then the variable is only valid within the for statement. After the for statement is executed, the variable is released in memory.

2.2 for and while can be interchanged. If you need to define a loop increment, it is more appropriate to use for.


3. Function: An independent small program with specific functions defined in a class, also called a method.
3.1 Format of function:
                                         int     return return value; The data type of the result after running.
 Parameter type: It is the data type of the formal parameter.
 Formal parameter: It is a variable used to store the actual parameters passed to the function when calling the function.
 Actual parameter: the specific value passed to the formal parameter.
 Return: used to end the function.
 Return value: This value will be returned to the caller. (The keyword void is used when a function returns no specific value.)
3.2. Overloading of functions: In the same class, more than one function with the same name is allowed, as long as their number of parameters or parameter types are different. .
Specificity of overloading: It has nothing to do with the return value type, only the parameter list (it also depends on the parameter order).
Benefits of overloading: easier to read and optimized program design.
  Overload example:
   // Return the sum of two integers
   int add(int x, int y) { return x+y; }
   // Return the sum of three integers
   int add(int x, int y, int z ) { return x+y+z; }
     //Return the sum of two decimals
   double add(double x, double y) { return x+y; }

4. Memory structure:
When the Java program is running, it needs Allocate space in memory. In order to improve computing efficiency, the space is divided into different areas, because each area has a specific way of processing data and memory management.
 Stack memory: used to store local variables. When the data is used up, the space occupied will be automatically released.
Heap memory:

1>. Arrays and objects, and instances created through new are all stored in heap memory.

  2>. Each entity has a memory address value.
  3>.Variables in entities have default initial values.
  4>. The entity is no longer in use and will be recycled by the garbage collector within an uncertain period of time.


5. Array (reference data type): a collection of data of the same type. In fact, an array is a container.
Benefits of arrays: You can automatically number the elements in the array starting from 0, making it easier to operate 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 entity in the heap memory space and create Each element is assigned a specific value.
  int [] arr = {1,2,5,3};//It can be abbreviated if the data is clear
 The function of new: used to generate a container entity in the heap memory.
 int [] arr: int represents the type of element; [] table array; arr represents the array type. arr is an array variable in the stack memory. What is assigned is actually the address of the array in the heap memory. In addition, arr is stored in the stack memory, and then the container entity address created by new in the heap memory is obtained, which points to the heap memory. array.

The above is the detailed content of Java basic syntax. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn