博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Debug讲解_CodingPark编程公园
阅读量:1887 次
发布时间:2019-04-26

本文共 7821 字,大约阅读时间需要 26 分钟。

Debug简介

  • 什么是程序DeBug?

    • Debug,是程序开发人员必会的一项调试程序的技能。

    • 企业中程序开发和程序调试的比例为1:1.5,可以说如果你不会调试程序,你就没有办法从事编程工作。

  • Debug能帮助我们做什么?

    1. 追踪代码的运行流程。
    2. 程序运行异常定位。
    3. 线上问题追踪。
  • Debug对于程序学习者的意义

    1. 通过调试能够更好的查看程序的执行流程。
    2. 复杂的程序逻辑,通过老师的口述讲解,很难理解清楚,这个时候借助调试能够很好的帮助同学们理解程序。
    3. 定位问题,提高自我解决问题的能力。

IDEA中的Debug步骤

在这里插入图片描述

观察变量

  • 查看变量有三种方式:
  1. 程序区查看变量
  2. Debugger的Variables中查看变量
  3. 鼠标悬停到变量名上会弹出当前变量的值
/** * @Author: TEAM-AG * @Description: 调试基本介绍 * @Date: Created in 14:15 2020-05-28 * @Modified By: */public class Corgi_debug1 {
public static void main(String[] args) {
int num = 1; System.out.println("num = " + num); num ++; System.out.println("num = " + num); num ++; System.out.println("num = " + num); num ++; System.out.println("num = " + num); num ++; System.out.println("num = " + num); num ++; System.out.println("num = " + num); num ++; System.out.println("num = " + num); System.out.println("end..."); }}

在这里插入图片描述

步过和步入调试的使用

  • 步过调试
    步过调试按钮(F8)
    在这里插入图片描述
  • 作用
    步过,一行一行地往下走,如果这一行上有方法不会进入方法。
    常用于调试过程中不想进入调用的方法体的情况。

  • 步入调试

    步过调试按钮(F7)

    在这里插入图片描述

  • 作用

    步入,一行一行地往下走,如果这一行上有方法,则进入方法内部。

    一般用于进入自定义方法内,不会进入官方类库的方法。

import org.jetbrains.annotations.Contract;/** * @Author: TEAM-AG * @Description: 步过和步入调试的使用 * @Date: Created in 15:27 2020-05-28 * @Modified By: */public class Corgi_debug2 {
public static void main(String[] args) {
int a1 = add(1, 2); System.out.println(a1); int a2 = add(2, 3); System.out.println(a1 + a2); int result = a1 + a2; System.out.println(result); } @Contract(pure = true) private static int add(int a, int b){
int result = a+b; return result; }}

强制步入和步出

import java.util.ArrayList;import java.util.Iterator;/** * @Author: TEAM-AG * @Description: 强制步入和步出&并发修改异常 * @Date: Created in 16:13 2020-05-28 * @Modified By: */public class Corgi_debug3 {
public static void main(String[] args) {
System.out.println("begin..."); operatorList(); System.out.println("end..."); } private static void operatorList() {
ArrayList
list1 = new ArrayList<>(); list1.add("Corgi"); list1.add("CC"); list1.add("Jim"); list1.add("JACK"); //获取集合迭代器 Iterator
iterator1 = list1.iterator(); while (iterator1.hasNext()) {
String name = iterator1.next(); if(name.equals("Jim")) {
//改变集合元素个数 list1.add("JimAdd"); } } }}

计算表达式

/** * @Author: TEAM-AG * @Description: 计算表达式 * @Date: Created in 16:53 2020-05-28 * @Modified By: */public class Corgi_debug4 {
public static void main(String[] args) {
User user = new User("Corgi", "123456","超级管理员"); login(user); } private static void login(User user) {
String userType = user.getUserType(); switch (userType) {
case "超级管理员": System.out.println("超级管理员业务逻辑"); break; case "董事长": System.out.println("董事长业务逻辑"); break; case "经理": System.out.println("经理业务逻辑"); break; case "员工": System.out.println("进入员工业务逻辑"); break; default: System.out.println("角色不存在"); break; } }}class User{
private String userName; private String pwd; private String userType; public User(String userName, String pwd, String userType) {
this.userName = userName; this.pwd = pwd; this.userType = userType; } public String getUserName() {
return userName; } public void setUserName(String userName) {
this.userName = userName; } public String getPwd() {
return pwd; } public void setPwd(String pwd) {
this.pwd = pwd; } public String getUserType() {
return userType; } public void setUserType(String userType) {
this.userType = userType; }}

在这里插入图片描述

条件断点

在这里插入图片描述

  • 作用

    通过设置断点条件,在满足条件时,才停在断点处,否则直接运行。

/** * @Author: TEAM-AG * @Description: 条件断点 * @Date: Created in 17:10 2020-05-28 * @Modified By: */public class Corgi_debug5 {
public static void main(String[] args) {
for (int i = 1; i < 1000; i++) {
execute(i); } } private static void execute(int num) {
int rs = ((num + 3) * 5 + 9) / num; System.out.println(rs); }}

多线程调试

在这里插入图片描述

案例1

/** * @Author: TEAM-AG * @Description: 多线程调试-案例1 * @Date: Created in 21:04 2020-05-28 * @Modified By: */public class Corgi_debug6 {
public static void main(String[] args) {
Thread thread1 = new Thread(new MyRunnable(), "曹操"); Thread thread2 = new Thread(new MyRunnable(), "刘备"); Thread thread3 = new Thread(new MyRunnable(), "孙权"); thread1.start(); thread2.start(); thread3.start(); }}class MyRunnable implements Runnable {
@Override public void run() {
System.out.println(Thread.currentThread().getName() + "____进入"); try {
Thread.sleep(1000); } catch (InterruptedException e) {
e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "____离开"); }}

案例2

在这里插入图片描述

package Milk;/** * @Author: TEAM-AG * @Description:多线程调试-案例2 * @Date: Created in 10:58 2020-05-29 * @Modified By: */public class MainClass {
public static void main(String[] args) {
Box box = new Box(); Producer producer = new Producer(box); Consumer consumer = new Consumer(box); Thread thread1 = new Thread(producer, "生产者"); Thread thread2 = new Thread(consumer, "消费者"); thread1.start(); thread2.start(); }}
package Milk;/** * @Author: TEAM-AG * @Description: BOX * @Date: Created in 10:58 2020-05-29 * @Modified By: */public class Box {
private int milk; private boolean state = false; public void put(int milk){
System.out.println("put begin"); synchronized (this){
if (state){
try{
System.out.println("put wait"); wait(); }catch (InterruptedException e){
e.printStackTrace(); } System.out.println("put wait end"); } this.milk = milk; System.out.println("送奶工将第" + this.milk + "瓶牛奶放入奶箱"); this.state = true; notifyAll(); } System.out.println("put end"); } public void get() {
System.out.println("get begin"); synchronized (this) {
if (!state) {
try {
wait(); } catch (InterruptedException e) {
e.printStackTrace(); } } System.out.println("用户拿到第" + this.milk + "瓶牛奶"); this.state = false; notifyAll(); } System.out.println("get end"); }}
package Milk;/** * @Author: TEAM-AG * @Description: Producer * @Date: Created in 10:58 2020-05-29 * @Modified By: */public class Producer implements Runnable {
private Box box; public Producer(Box box) {
this.box = box; } @Override public void run() {
for (int i=1; i<=5; i++){
box.put(i); } }}
package Milk;/** * @Author: TEAM-AG * @Description: Consumer * @Date: Created in 10:58 2020-05-29 * @Modified By: */public class Consumer implements Runnable {
private Box box; public Consumer(Box box) {
this.box = box; } @Override public void run() {
for (int i=1; i<=5 ;i++){
box.get(); } }}

更多信息

📍IDEA_Debug

https://www.bilibili.com/video/BV1LJ41187hu?p=1

在这里插入图片描述

转载地址:http://nnzdf.baihongyu.com/

你可能感兴趣的文章
git 使用报错: fatal: Couldn‘t find remote ref master的解决方法
查看>>
php 魔术方法 __invoke()
查看>>
php_sapi_name
查看>>
set_include_path
查看>>
react 中 redux
查看>>
Vue 节流(throttle)和防抖(debounce)
查看>>
小程序初始页面地址修改
查看>>
微信小程序使用腾讯地图
查看>>
http-server的安装和使用(vue 打包后项目运行)
查看>>
vuex中使用typescript
查看>>
vue 使用 typescript ref中的方法找不到(uniapp 中使用 typescript ref)
查看>>
vue使用typescript / uniapp 使用typescript
查看>>
uniapp 使用typescript 运行小程序正常 h5 报错/ keepAliveInclude is not defined
查看>>
webpack打包html里面img后src为“[object Module]”
查看>>
使用webpack打包发现css单行注释报错
查看>>
webpack 打包拆分原则和理解(SplitChunksPlugin)
查看>>
Object.fromEntries方法报错
查看>>
typeof 的复习
查看>>
2021-05-06 最近的生活
查看>>
前端面试题总结
查看>>