Six, debugging and optimization
1, junit
Before this, when writing code test, usually start from the main function, go through the process test I can see the effect, here can use the method of unit test, simplify the steps of module test;
1.1 Usage
For unit testing with IDEA IDE, you first need to download the JAR file.
1) Create a lib folder
2) Paste the JAR file into the lib folder
3) Introduce Jar files
4) Create the test folder
5) Set the test folder as the test folder
6) Press Shift + CTRL + T in any of the classes to test to generate the test class
7) Select the method and version to test
8) After the test class is created, write the test code
9) Perform the test to observe the effect
1.2 Example
1) Create a new package in SRC and a class Demo. The Demo contains the methods you want to test
2) Shift + CTRL +T create test class
3) Write test methods
4) Operation effect
1.3 Using Assertions
1) Add method
2) Add assertions
3) Running results
2, the debug
1) Source code
package com.kaikeba; import bean.Express; import java.util.ArrayList; import java.util.Collection; import java.util.concurrent.*; public class Demo1 { static ArrayList<String> data = new ArrayList<>(); public static void main(String[] args) { int count = 10; for(int i = 0; i < 10; i++) { count++; data.add("item" + i); } for(int i = 0; i < data.size(); i++) { count--; String text = data.remove(i); System.out.println(" delete: "+ text); }}}Copy the code
2) Add breakpoints
3) Debug Debugging
4) By observing the results, you can see the variables in each line of statements
5) Introduction of each button
3. Shortcut keys of IDEA
3.1 Common Shortcut Keys
1) Tips and code generation
Alt + Enter IDEA Provides a quick repair option based on the problem where the cursor is located
Ctrl + Alt + T popup layer around selected code options (try, if, etc.)
Alt + Insert code is generated automatically, such as set/get methods, constructors, toString(), etc
Ctrl + O selects a method that can be overridden
Ctrl + I selects the implementable method
Ctrl + / Comment the line of code where the cursor is. It uses different comment symbols according to the current file type
2) Select/move/copy code:
Ctrl + W progressively selects code blocks (press multiple times to select name, current line code content, current line, code block, and so on)
CTRL +Shift+ W Progressive deselect code block
Ctrl + D copies the line where the cursor is or the selection and inserts the copy below the cursor position
Ctrl+Shift+↑ or ↓ : Moves the current line or code structure
Ctrl + Y deletes the line where the cursor is or deletes the selected line
Ctrl + X Cuts the line where the cursor is or cuts the selection
3) Code optimization
Ctrl + Alt + O optimizes the imported classes, which can be used for the current file and the entire package directory
Ctrl + Alt + L formatting code that can be used for the current file and the entire package directory
4) Find and replace
Ctrl + F to find text in the current file
Ctrl + Shift + F Searches the entire project based on the input or specifies the file in the directory Ctrl + R replaces the text in the current file
Ctrl + Shift + R Replaces the entire project or files in a specified directory with the input
5) Read the code section
Ctrl + F12 pop up the current file structure layer (class method attributes, etc.), you can pop up the layer directly input, filter
Ctrl + Q displays the contents of the document above the variable/class/method name where the cursor is located (or when prompted to add)
3.2 Shortcut Keys
1) Ctrl correlation
Ctrl + F to find text in the current file (required)
Ctrl + R to replace text in the current file (required)
Ctrl + Z Undo (required)
Ctrl + Y Deletes the line where the cursor is or deletes the selected line (required)
Ctrl + X Cuts the line where the cursor is or cuts the selection
Ctrl + C copies the line where the cursor is or the selection
Ctrl + D copies the line where the cursor is or copies the selection and inserts the copy below the cursor position (required)
Ctrl + W selects code blocks progressively. Select the word or paragraph where the cursor is located. Pressing continuously will expand the selection on the basis of the original selection (required).
Ctrl + E displays a list of recently opened file records
Ctrl + N Finds the class file based on the entered class name
Ctrl + G jumps to the specified line in the current file
Ctrl + J inserts custom dynamic code templates
Ctrl + P method parameter prompt is displayed
Ctrl + Q displays the contents of the document above the variable/class/method name where the cursor is located (or when prompted to add)
Ctrl + U goes to the method/interface definition of the parent class of the method where the current cursor is located
Ctrl + B to enter the interface or definition of the method/variable where the cursor is, equivalent to Ctrl + left-click
Ctrl + K Version control submits an item that is available only if it has been added to version control
Ctrl + T Version control to update an item that has been added to version control to be available
Ctrl + H displays the hierarchy of the current class
Ctrl + O selects a method that can be overridden
Ctrl + I selects an inheritable method
Ctrl + + expands the code
Ctrl + – Folds the code
Ctrl + / Comment the code on the line where the cursor is located, using different comment symbols based on the current file type (mandatory)
Ctrl + [Moves the cursor to the beginning of the curly braces of the current code
Ctrl +] moves the cursor to the end of the curly brace in the current code
Ctrl + F1 displays an error message at the error code where the cursor is
Ctrl + F3 goes to the next reference location of the selected word
Ctrl + F4 closes the current edit file
Ctrl + F8 In Debug mode, set the breakpoint for the cursor action. If the breakpoint already exists, remove the breakpoint
Ctrl + F9 to Make Project
Ctrl + F11 select the file/folder and set/unbookmark using the mnemonic
Ctrl + F12 pop up the current file structure layer, you can pop up the layer directly input, screening
Ctrl + Tab editing window switch. If delete is added in the switching process, the selected window will be closed
Ctrl + Enter Intelligently delimits lines
Ctrl + End jumps to the End of the file
Ctrl + Home jumps to the file header
Ctrl + Space basic code completion, default Windows input method occupied, need to change, you are advised to change ALT+/ (mandatory)
Ctrl + Delete Deletes the word or Chinese sentence after the cursor
Ctrl + BackSpace deletes words or Chinese sentences in front of the cursor
Ctrl + 1, 2, 3… 9 Locate the bookmark corresponding to the value
Ctrl + Left click on the title of the open file to display the file path
Ctrl + cursor Location Press Ctrl without releasing it to display a summary of the class where the cursor is located
Ctrl + Left arrow key Cursor jumps to the left beginning of the current word/Chinese sentence
Ctrl + Right arrow key Cursor moves to the right beginning of the current word/Chinese sentence
Ctrl + front arrow key is equivalent to mouse wheel forward effect
Ctrl + back arrow key is equivalent to mouse wheel backward effect
2) Alt correlation
Alt + ‘displays a pop-up layer of common version control menus
Alt + Q pops up a prompt that displays declaration/context information for the current class
Alt + F1 shows the current file selection target pop-up layer, which has many targets to choose from
Alt + F2 For the previous page, display various browser open target selection pop-up layer
Alt + F3 select text, scroll down the same text one by one, and highlight it
Alt + F7 finds the method/variable/class where the cursor is called
Alt + F8 In the Debug state, select the object, and view the debugging result in the input calculation expression debugging box
Alt + Home locate/display Navigation Bar to current file
Alt + Enter IntelliJ IDEA Provides a quick fix option based on the problem of the cursor position. The prompt result varies with the cursor position (mandatory)
Alt + Insert code generated automatically, such as set/get methods for generating objects, constructors, toString(), etc. (required)
Alt + Left arrow key Press the left arrow key to switch to an open file view
Alt + Right arrow key Press the right arrow key to switch to an open file view
Alt + arrow key The current cursor jumps to the previous method name in the current file
Alt + arrow keys The current cursor moves to the last method name in the current file
Alt + 1, 2, 3… 9 displays tabs with corresponding values, where 1 is the most commonly used for Project
3) Shift correlation
Shift + F1 connects external documents if there are any
Shift + F2 jumps to the previous highlighted error or warning location
Shift + F3 searches for a match in search mode
Shift + F4 For the currently open file, use the new Windows window to open, the old window remains
Shift + F6 renames files/folders
Shift + F7 In Debug mode, smart step in. If there are multiple method calls on the breakpoint line, which method will pop up
– Shift + F8 when in Debug mode, it will display the same effect as F9
Shift + F9 is equivalent to clicking the Debug button in the toolbar
Shift + F10 is equivalent to clicking the Run button in the toolbar
Shift + F11 brings up the bookmark display layer
Shift + Tab cancels indentation
Shift + ESC hides the current or last active tool window
Shift + End selects the cursor to the End of the current line
Shift + Home select cursor to current position
Shift + Enter starts a new line. A row is left below the cursor, and the cursor is positioned on the new row
Press this shortcut key on an open file name to close the currently open file
Scroll the current file in the horizontal scroll axis
4) Ctrl + Alt correlation
Ctrl + Alt + L formatting code that can be used for the current file and the entire package directory (mandatory)
Ctrl + Alt + O optimizes the imported classes, which can be used with the current file and the entire package directory (required)
Ctrl + Alt + I Automatically indents the line or selected portion of the cursor, similar to formatting
Ctrl + Alt + T pops the surround option pop-up layer on the selected code
Ctrl + Alt + J brings up the template selection window and adds the selected code to the dynamic template
Ctrl + Alt + H call hierarchy
Ctrl + Alt + B on the method name of a call jumps to the implementation, skipping the interface
Ctrl + Alt + V quickly introduce variables
Ctrl + Alt + Y To synchronize and refresh
Ctrl + Alt + S opens IntelliJ IDEA system Settings
Ctrl + Alt + F7 shows where to use it. Find where the class or variable is called and use a pop-up box to find it
Ctrl + Alt + F11 To switch to the full-screen mode
Ctrl + Alt + Enter A line is displayed indicating that the cursor is positioned on a new line
Ctrl + Alt + Home pops up the file pop-up layer associated with the current file
Ctrl + Alt + Space class name completes automatically
Ctrl + Alt + left arrow key to return to the previous action (required)
Ctrl + Alt + right arrow key to go to the previous action (required)
Ctrl + Alt + Arrow key In search mode, skip to the last searched file
Ctrl + Alt + arrow keys in search mode, skip to the next search file
5) Ctrl + Shift related
Ctrl + Shift + F Find the entire project or files in the specified directory based on the input (required)
Ctrl + Shift + R Replaces the entire project or files in the specified directory with the input (required)
Ctrl + Shift + J automatically merges the next line to the end of the current line (mandatory)
Ctrl + Shift + Z Cancel Cancel (mandatory)
Ctrl + Shift + W Progressive deselect code block. Select the word or paragraph where the cursor is located. Pressing continuously will expand the unselected range on the basis of the original selection (required).
Ctrl + Shift + N Locate/open a file/directory by filename. Opening a directory requires a forward slash (mandatory).
Ctrl + Shift + U converts the selected code in turn to lower/upper case (mandatory)
Ctrl + Shift + T Generates a unit test class for the current class and selects one if it already exists
Ctrl + Shift + C Copies the current file disk path to the clipboard
Ctrl + Shift + V pops up the content manager pop-up layer for the most recent copy of the cache
Ctrl + Shift + E displays the pop-up layer for the list of recently modified files
Ctrl + Shift + H displays the method hierarchy
Ctrl + Shift + B jumps to the type declaration
Ctrl + Shift + I Quickly view the definition of the method or class where the cursor is located
Ctrl + Shift + A Find actions/Settings
Ctrl + Shift + / code block comments (mandatory)
Ctrl + Shift + [Select from the cursor position to its top bracket position
Ctrl + Shift +] select from the cursor position to its bottom bracket position
Ctrl + Shift + + expands all code
Ctrl + Shift + – Folds all code
Ctrl + Shift + F7 highlights all the selected text. Press Esc to highlight and disappear
Ctrl + Shift + F8 In Debug mode, specify breakpoint entry conditions
Ctrl + Shift + F9 compiles the selected file/package/Module
Ctrl + Shift + F12 editor maximized
Ctrl + Shift + Space Intelligent code prompt
Ctrl + Shift + Enter Auto-end code, auto-end semicolon (mandatory)
Ctrl + Shift + Backspace returns to the last modification
Ctrl + Shift + 1,2,3… 9 Quickly add bookmarks with specified values
Place the cursor on a class variable. Press this shortcut key to navigate directly to the class. (Required)
Ctrl + Shift + Left Arrow key On the code file, move the cursor to the beginning of the current word/Chinese sentence and select the word/Chinese sentence
Ctrl + Shift + Right Arrow Key On the code file, move the cursor to the beginning of the current word/Chinese sentence and select the word/Chinese sentence
Ctrl + Shift + Left arrow key When the cursor focus is on the Tools TAB, shrink the TAB area
Ctrl + Shift + Right Arrow Key the cursor focus is on the tools TAB to enlarge the TAB area
Ctrl + Shift + front arrow cursor over method name, move method before previous method, adjust method sorting
Ctrl + Shift + move the cursor over the method name, move the method before the next method, and adjust the sorting of methods
6) Alt + Shift correlation
Alt + Shift + N Select/Add task
Alt + Shift + F shows Add to Favorites pop-up layer/Add to Favorites
Alt + Shift + C View the list of recent changes
Alt + Shift + I View the current file of the project
Alt + Shift + F7 In Debug mode, the next step is to enter the current method body. If there are other methods in the method body, the method body will enter the embedded method
Alt + Shift + F9 Displays the Debug menu
Alt + Shift + F10 brings up the selectable Run menu
Alt + Shift + left click to select the word/Chinese sentence to be double-clicked. Hold down and select other words/Chinese sentence at the same time
Alt + Shift + Arrow key Move the cursor to move up
Alt + Shift + Arrow keys Move the cursor down the row
7) other
F2 Jump to the next highlighted error or warning location (mandatory)
F3 locates to the next match in search mode
F4 editing source
F7 In Debug mode, go to the next step. If the current line breakpoint is a method, it goes to the current method body. If the method body has methods, it does not go to the embedded method body
F8 In Debug mode, go to the next step. If the current line breakpoint is a method, do not enter the current method body
F9 In Debug mode, the program resumes running, but stops at the next breakpoint if the code below that breakpoint still has breakpoints
F11 Add bookmarks
F12 returns to the previous tool window
Tab indent
ESC from the tools window to the code file window Ctrl + Shift + Alt + V formatting paste
Ctrl + Shift + Alt + N to the specified variable/method
Ctrl + Shift + Alt + S opens the current project Settings
Ctrl + Shift + Alt + C Copy reference information
The chapters are summarized here (the better we understand you guys, the better we understand you guys).
Have a question welcome to ask, everyone together in learning Java on the road to play strange upgrade! (O ゜ – ゜▽゜)