JAVA/JAVACORE

[TOOL] Debugging on Eclipse

기록해연 2024. 10. 20. 23:28

My Eclipse IDE version

 

기술면접에서 추가적으로 받았던 질문 중에 개발툴에서 디버깅을 자주 사용하는지, 사용한다면 어떤 식으로 사용하는지에 대한 질문이 있었는데, 사실 그렇게 어려운 로직을 짜는 것도 아니고 보통의 오류들은 작성 단계나, 컴파일 단계에서 걸러지고 있어서 로깅 외에는 일할 때 잘 안씀. 그러다보니 기억이 정말 안나는 거 같아서 정리하려고 함.


 

브레이크포인트 설정

  • 브레이크포인트(Breakpoint)는 프로그램이 실행 중일 때 멈추는 지점
  • 코드의 왼쪽 마진(라인 번호가 있는 곳)을 더블 클릭하거나, 해당 라인에 커서를 놓고 Ctrl + Shift + B를 누르면 브레이크포인트가 설정됨.
  • Breakpoint 뷰에서 전체 브레이크포인트를 한눈에 확인 가능.

Debugging points

디버그 모드 실행

  • 브레이크포인트를 설정한 후, Run 메뉴에서 Debug As > Java Application을 선택하거나, F11을 누르기.
  • 디버그 모드로 실행시 브레이크포인트에서 실행을 멈추게 되고, 이 상태에서 프로그램 상태를 확인.

디버깅 도구

  • Step Into (F5): 메서드 내부로 진입하여 실행 흐름을 디버깅.
  • Step Over (F6): 메서드 호출을 건너뛰고, 다음 라인으로 이동.
  • Step Return (F7): 현재 메서드에서 빠져나와 호출한 메서드로 회귀.
  • Resume (F8): 다음 브레이크포인트까지 계속 실행.

변수 및 스택 상태 확인

  • Variables View: 현재의 변수 값을 확인할 수 있으며, 변수 값을 수동으로 변경할 수도 있다. 

Break상태인 경우, Variables 뷰에서 Value 값 수정 가능

  • Expressions View: 특정 표현식이나 변수를 추가해 해당 값들을 추적할 수 있다.
  • Debug Perspective: 디버깅을 쉽게 할 수 있는 전용 디버깅 퍼스펙티브로 전환하면 스택 상태, 변수, 스레드 상태를 쉽게 확인할 수 있다.


(이하 내용은 예시 코드)

 

 예시 

package Test;

// An example for debugging test
public class Player {
	private String name;
    private int hits;
    private int atBats;

    // Constructor
    public Player(String name, int hits, int atBats) {
        this.name = name;
        this.hits = hits;
        this.atBats = atBats;
    }

    // Method to calculate the player's batting average
    public double calculateBattingAverage() {
        if (atBats == 0) {
            return 0.0; // Avoid division by zero
        }
        double battingAverage = (double) hits / atBats;

        // Round to 3 decimal places
        return Math.round(battingAverage * 1000) / 1000.0;
    }
    
    // Method to display the player's stats
    public void displayStats() {
        System.out.println("Player: " + name);
        System.out.println("Hits: " + hits);
        System.out.println("At Bats: " + atBats);
        System.out.println("Batting Average: " + calculateBattingAverage());
        System.out.println("=============================");
    }

    // Getters and Setters
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getHits() {
        return hits;
    }

    public void setHits(int hits) {
        this.hits = hits;
    }

    public int getAtBats() {
        return atBats;
    }

    public void setAtBats(int atBats) {
        this.atBats = atBats;
    }
}

Player.java

package Test;

public class BaseballGame {
	public static void main(String[] args) {
	// 시환 has 143 hits in 526 at-bats
        Player player1 = new Player("노시환(No SiHwan)", 143, 526); 
        // 진혁 has 76 hits in 289 at-bats
        Player player2 = new Player("장진혁(Jang JinHyuk)", 76, 289); 
        // 후디 has no hits in 70 at-bats (to test zero hits)
        Player player3 = new Player("이후디(Lee Hoodie)", 0, 70); 
        // 수리 has no at-bats (to test division by zero)
        Player player4 = new Player("이수리(Lee Suri)", 40, 0);   

        // Display statistics for each player
        player1.displayStats();
        player2.displayStats();
        player3.displayStats();
        player4.displayStats();
    }
}

BaseballGame.java

'JAVA > JAVACORE' 카테고리의 다른 글

[OVERVIEW] OOP  (0) 2024.10.16
[COMPARISON] State / Behavior in Object  (2) 2024.10.03
[COMPARISON] Object / Class  (2) 2024.10.03