Records of problems and difficulties in the Java learning process

Record the learning process of the algorithm, and record the problems and difficulties encountered~~

1 code

在这里插入代码片

2 problems encountered

1 Static fields and static methods of a class

1. Static field:
If the field is set as static, then there is only one such field in each class,
and each object has its own copy of all instance fields,
that is, it belongs to the class, not Belonging to any independent object
The static domain is called the class domain
2. Static constants:
For example: public static final double PI = 3.14159265358979323846;
Change static constants: The storage control mechanism of the Java language can be bypassed through local methods
3. Static methods:
static methods It is a method that cannot perform operations on objects,
that is, there are no implicit parameters.
It can be considered that a static method is a method without this parameter
. A static method can access the static field in its own class, and the method can also omit the keyword static
3.1 Using the static field There are two cases:
a method does not need to access the object state, and its required parameters are provided through display parameters;
a method only needs to access the static field of the class

2 Create an array

public static void main(String[] args) {
    
    

	//创建数组的第一种方法
	int[] arr=new int[6];//数组初始化全是0
	int intValue=arr[5];
	//System.out.println(intValue);
	
	//创建数组的第二种方法
	int[] x={
    
    1,2,3,4};
	//System.out.println(x[1]);

	//创建数组的第三种方法。
	int[] y= new int[]{
    
    1,2,3,4,5};
	int m=0;
	boolean length = isLength(m,y);
	if(length){
    
    
		System.out.println(y[m]);
	}else{
    
    
		System.err.println("数组标越界");
	}	

}
//判断数组下标是否越界
public static boolean isLength(int m,int arr[]){
    
    
	boolean flag=false;
	int length = arr.length;
	if(m<length)
		flag=true;
	return flag;
}

3 eclipse debugging java program

Introduce how to use Eclipse to debug a Java program, and use an example to illustrate the specific process.

3.1 Setting breakpoints

The most common debugging step is setting breakpoints, which allow you to examine variables and values ​​within conditional statements or loop statements.
Double-click on the left side of a line of code, or place the cursor on this line and press the shortcut key Ctrl+Shift+B to set a breakpoint.
The way to cancel a breakpoint is the same as adding a breakpoint.
The following figure sets a breakpoint on line 13 of the program:
insert image description here

3.2 Start debugging

Method 1: Right-click the .java file in Project Explorer, select Debug as —> 1 Java Application
Method 2: Click the green bug in the window.
Method 3: Press the shortcut key F11
to enter debugging, the window is as follows: insert image description here
You can see the variable name and variable value (variable view) from the small window on the right: you
insert image description here
can also see the breakpoint you set (breakpoint view) :
insert image description here

3.3 Single-step execution

Thread stack view (Debug View):
insert image description here

  1. Indicates that the current implementation continues to run until the next breakpoint, and the shortcut key is F8.
  2. It means to interrupt the whole process means to enter the current method, and the shortcut key is F5.
  3. It means to run the next line of code, and the shortcut key is F6.
  4. Indicates to exit the current method and return to the calling layer, and the shortcut key is F7.
  5. Represents the stack of the current thread, from which you can see which code is running, the entire calling process, and the line number of the code.
    insert image description here

3.4 End debugging

Use the Terminate command to terminate the debugging of this program.

Guess you like

Origin blog.csdn.net/funzmg/article/details/104396110