Java04switch statements
- Format:
Switch () {
Case: Constant 1
EXECUTE statement
Break
Default
EXECUTE statement
Break
}
- The case satisfies the execution case execution statement, jumps out of the loop, otherwise executes the default execution statement;
- Case: Output Day of the week
- Switch expression type
L JDK 0-1.4 byte short int char
L jdk1.5 byte short int char enum
L jdk1.7 byte short int char enum String (after Sun Company acquired)
- Case penetrating
Case penetration refers to a switch statement that executes from the matching case label based on the value of the expression and executes until the break statement or at the end of the switch statement. If there is no break statement, then it starts at the case label that matches the value of the expression and executes to the end of the switch statement, starting at one of the case, ignoring the matching of subsequent values, and the phenomenon of running the contents of the case directly is the phenomenon of penetrating. No break will always penetrate execution.
Switch () {
CASE1:
CASE2:
CASE3:
CASE4:
EXECUTE statement
Break
}
Array
- A container used to store data
- Data is stored in an array
- Defining rules
L Data type
L Container Size
L Formula: Data type [] variable name =new data type [number of stored elements];
L Data type: The data type of the storage element
l [] Expression array
L New Create a container keyword
l [] represents an array
L Number: Is the number of data stored
L constant array size, cannot be changed once determined
L Reference type
L Memory Issues
- Operating System management hardware (memory)
- JVM Search System
- The operating system is allocated a portion of the JVM's memory, managed by the JVM
- The JVM divides its memory into five regions.
A) Register: between memory and CPU (not our concern)
b) Local method stack: a function that the JVM does not perform, and the JVM invokes the functionality in the system (currently irrelevant)
c) Sharing of methods and data: the memory portion of the run-time class file (related to development)
d) method Stack: When all the methods run, enter the memory portion
e) heap: stored Containers and objects
Java open up memory space do not shut down
C + + Shutdown Memory space requires manual shutdown
One-dimensional arrays
L Array Memory issues
Array Memory Graph:
public class arraydemo{
Main () {
Int[] Arr=new int[3];
}
}
A) First the method area to run the Arraydemo.class file
b) Then main enters the method stack run, i.e. the stack runs (advanced at the bottom), the method stack opens up a part to run the Main method
c) Then create a new array container the JVM will open up a portion of the memory inside the storage array container and according to the memory size of the average, each data in memory memory address, (auto-numbering, the minimum is 0, the maximum storage length-1, automatic numbering is index, subscript, corner superscript); The first address of the array is the first data address , the address of each data is continuous;
d) The virtual machine then returns the address to the defined variable arr, which stores the variable arr, which points to the memory address;
e) Print arr: print multiple times differently because memory is randomly allocated
f) Print finished the main method ends, the ARR variable fails, and the allocation space inside the heap fails
G
L Array Access formula
- Each data in memory has memory address, (auto number, the minimum value is 0, the maximum storage length-1, automatic numbering is index, subscript, corner superscript);
- Variable name [subscript];
L Length Property
- Maximum index Length-1;
L Array Definition method
- Assign a value to an element at the same time as defined
A) data type [] variable name =new data type [] {};
Int [] arr=new inr[]{1,2,3,4,5};
b) do not allow length to be defined in brackets, otherwise compilation fails
c) Avoid the risk of logic errors and space waste resulting from the subsequent elements saying more than or less than the defined space;
- No new keyword
A) data type [] variable name ={};
b) Convenient and simple, recommended use;
L Assign a value to an element
Int [] arr=new int[5];
arr[1]=3;
L Summary
Int[] Arr=new int[5];
arr[1]=2;
Int[] Arr=new int[]{2};
Int[] Arr={2};
L Traversal Array
- For loop implementation
A) for loop has been cycle times, counter thought
b) while unknown loop at this time
- for (i=0;i<arr.length-1;i++) {
Output I
}
L exception
- Out-of-bounds exceptions (negative index and hyper-order access)
A) successful compilation
b) Run error (only the runtime will use the memory load method and perform the operation inside, see memory problems above)
- Null pointer exception
A) normal phenomenon
b) Non-normal phenomenon
The assignment array element is nul,null as a special constant, the reference type can be null, at which point the variable does not save this element address or does not point to this element address, at this time again access that element will be reported null pointer exception, the virtual machine heap cleanup This element memory, cut cannot be reassigned address;
L Array Maximum value thought
- Int[] arr={1,5,8,9,3,}; (not before, can be one more comma,)
Int Max=arr[0];
- for (i=1;i<arr.length-1;i++) {
If (Max<arr[i])
{
Max=arr[i];
}
}
Output Max
Two-dimensional array (stores multiple one-dimensional arrays)
L Define the way
- Int [] [] arr=new int[3] [4];
- One more []
- Representation: In a two-dimensional array, there are three one-dimensional arrays, three one-dimensional arrays, and the length of each array is 4
L Memory diagram
- When it is created, it first allocates a memory space in memory to a two-dimensional array, divided into three parts
- Each one-dimensional array is then allocated one memory space, and each one-dimensional array is divided into four parts; each one-dimensional array has a first address,
- A two-dimensional array contains the first address of each one-dimensional array
- The two-dimensional array variable points to the first address of a two-dimensional array, the address of the first one-dimensional array;
- Defining special cases with two-dimensional arrays
L int[] [] arr=new Int [3][];
L cannot assign a two-dimensional array arr[0]=new int [5] at this time.
- A simple way to define
a) Int [] [] arr={{1,4},{5,6},{7,8,9}};
b) allow one-dimensional array lengths to vary
L Traverse
- Two for loops to traverse
- Outer loop loops through two-bit arrays
- Memory loop traversal one-dimensional array
- for (int i=0;i<arr.length;i++) {
for (int j=0;j<arr[i].length;j++) {
Output ARR[I][J];
}
}
Exercise: Random Name Register
- Function:
A) store the name of the class
b) Overview of the class name
c) A person's name is randomly given
- Comment Amount up to 30% of source code
comments in the Java language
Program comments are an important part of the source code, for a specification of the program source code, the note should be accounted for more than 1/3 of the source code. Almost all programming languages provide a way to annotate, the General programming language provides basic single-line comments and multiline comments, the Java language is no exception, in addition, the Java language provides a document annotation. There are three types of comments in the Java language
Single-line Comment
Multi-line comments
Document comments
- 3. re
Core Vocabulary
English [re?] Beauty [re?]
Prep. About
Pref. Again, re-
N. The second tone of the long scale
- Implementation of the selection function in the method
L If the user enters the function selection input, other than the specified characters, you can use the Else statement to re-execute the method, the implementation of the function is perfect, if the method is only as the interface display, otherwise can not be re-executed;
04.zip
The Java self-study road-DAY04