Java Debugging, Number Systems, and Foundational Coding Exercises

Debugging in Java

Debug mode is a development tool used to inspect program flow and trace execution for troubleshooting purposes.

Workflow

  • Setting Breakpoints: Click the left margin next to the line number in you're IDE.
  • Starting Debug: Right-click the editor and select the Debug option.
  • Monitoring: Inspect the Debugger panel for variable states and the Console panel for output.
  • Stepping: Use the Step Into function (often F7) to proceed line by line.
  • Managing Breakpoints: Click the margin again to toggle a single breakpoint, or remove all at once via the breakpoint management view.

Number Systems

Representation in Code

public class NumberSystemDemo {
    public static void main(String[] args) {
        // Decimal (default)
        System.out.println(10);
        
        // Binary (prefix 0b)
        System.out.println("Binary 0b10 -> " + 0b10);
        
        // Octal (prefix 0)
        System.out.println("Octal 010 -> " + 010);
        
        // Hexadecimal (prefix 0x)
        System.out.println("Hex 0x10 -> " + 0x10);
    }
}

Converting Decimal to Other Bases

To convert a decimal number to another base, repeated divide the number by the target radix (base) and collect the remainders. Stop when the quotient is 0, then read the remainders in reverse order.

  • To Binary (Base 2): Convert 11.

    • 11 / 2 = 5 remainder 1
    • 5 / 2 = 2 remainder 1
    • 2 / 2 = 1 remainder 0
    • 1 / 2 = 0 remainder 1
    • Result: 1011
  • To Hexadecimal (Base 16): Convert 60.

    • 60 / 16 = 3 remainder 12 (C)
    • 3 / 16 = 0 remainder 3
    • Result: 3C

Quick Conversion with 8421 Code

The 8421 code (BCD) assigns weights to binary digits: 8, 4, 2, 1. Summing the weights of positions containing '1' yields the decimal value.

Signed Number Representation

Computers use Two's Complement for arithmetic.

  • Sign-Magnitude: The leftmost bit indicates sign (0 positive, 1 negative). The rest represent magnitude.
  • Ones' Complement: Positive numbers remain the same; negative numbers flip every bit (excluding the sign bit in some definitions, but generally flip all for math).
  • Two's Complement: Positive numbers are identical to sign-magnitude. Negative numbers are derived by inverting the bits of the positive number and adding 1.

Bitwise Operators

Bitwise operations work on the binary level.

public class BitwiseOperations {
    public static void main(String[] args) {
        int x = 6; // 00000110
        int y = 2; // 00000010

        // AND (&): 1 only if both bits are 1
        // 00000110 & 00000010 = 00000010 (2)
        System.out.println(x & y);

        // NOT (~): Inverts all bits
        // ~00000110 = 11111001 (in 32-bit int, this represents -7)
        System.out.println(~x);
    }
}

Shift Operators

  • Left Shift (<<): Shifts bits left, padding with zeros. Equivalent to multiplying by 2 per shift.
    • 12 << 1 results in 24.
  • Right Shift (>>): Shifts bits right, preserving the sign bit. Equivalent to dividing by 2 per shift.
  • Unsigned Right Shift (>>>): Shifts bits right, always padding with zeros.
public class ShiftDemo {
    public static void main(String[] args) {
        System.out.println(12 << 1); // 24
        System.out.println(12 << 2); // 48

        // XOR (^) property: a ^ b ^ a == b
        System.out.println(10 ^ 5 ^ 10); // 5
    }
}

Foundational Exercises

Swapping Two Variables

public class SwapDemo {
    public static void main(String[] args) {
        int num1 = 10;
        int num2 = 20;

        int holder = num1;
        num1 = num2;
        num2 = holder;

        System.out.println("num1=" + num1);
        System.out.println("num2=" + num2);
    }
}

Reversing an Aray

public class ReverseArray {
    public static void main(String[] args) {
        int[] data = {19, 28, 37, 46, 50};
        
        int left = 0;
        int right = data.length - 1;
        
        while (left < right) {
            int temp = data[left];
            data[left] = data[right];
            data[right] = temp;
            left++;
            right--;
        }
        
        for (int val : data) {
            System.out.println(val);
        }
    }
}

Two-Dimensional Arrays

A 2D array is essentially an array of arrays.

Dynamic Initialization:

// Creates a 3x3 grid of integers
int[][] matrix = new int[3][3];

// Assigning values
matrix[0][0] = 11;
matrix[0][1] = 22;

// Printing memory reference of inner array
System.out.println(matrix[0]); 

Static Initialization:

int[][] grid = { 
    {11, 22, 33}, 
    {44, 55, 66} 
};

// You can also assign pre-existing 1D arrays
int[] rowA = {1, 2, 3};
grid[0] = rowA;

Traversal and Summation:

public class SalesAnalysis {
    public static void main(String[] args) {
        // 4 Quarters, 3 Months each
        int[][] quarterlySales = {
            {22, 66, 44},
            {77, 33, 88},
            {25, 45, 65},
            {11, 66, 99}
        };
        
        int totalRevenue = 0;
        
        for (int i = 0; i < quarterlySales.length; i++) {
            for (int j = 0; j < quarterlySales[i].length; j++) {
                totalRevenue += quarterlySales[i][j];
            }
        }
        
        System.out.println("Total Revenue: " + totalRevenue + "万元");
    }
}

Tags: java debugging Number Systems Bitwise Operations Arrays

Posted on Wed, 19 Aug 2026 16:58:28 +0000 by Rhysickle