Think Java: How to Think Like a Computer Scientist Study Guide

Core Principles of Computer Science

  • Problem Solving: The foundational skill of computer scientist involving the formulation of problems, creative solution-finding, and clear expression of solutions.

  • Programming: The process of breaking down complex tasks into smaller subtasks executable by hardware electronic circuits.

  • Computer Components:

    • Processor (CPU): Performs simple calculations/logic.

    • Memory (RAM): Circuits for temporary data storage while the computer is active.

  • Algorithms: Sequences of steps used to solve problems. Computer science is the study, discovery, and analysis of these algorithms.

Java Programming Basics

  • Programs: Sequences of instructions (Class definitions $\rightarrow$ Methods $\rightarrow$ Statements).

  • High-level vs Low-level Languages:

    • High-level (e.g., Java, Python): Easy to read, portable across different hardware.

    • Low-level (Machine Language): Difficult to read, specific to one hardware type.

  • Translation Process:

    • Compiler: Translates entire source code into object code/executable before running.

    • Interpreter: Reads and executes code one line at a time.

    • Java Approach: Java is both compiled and interpreted. The javac compiler translates .java files into portable Byte Code (.class files). The Java Virtual Machine (JVM) then interprets this byte code for specific hardware.

Variables and Types

  • Variable: A named storage location in memory for values.

  • Primitive Types:

    • int: Integers (e.g., -5, 42).

    • double: Floating-point numbers with decimal precision (e.g., 3.14159).

    • boolean: Logical values true or false.

    • char: Single characters enclosed in single quotes (e.g., 'A').

  • Immutable Objects:

    • String: A sequence of characters. Operations like .toUpperCase() create NEW strings rather than modifying the original.

  • Constants: Declared using the final keyword (e.g., final double CM_PER_INCH = 2.54;). Conventional names are uppercase.

  • Casting: Explicit type conversion, e.g., int x = (int) pi;. This truncates the decimal (rounds toward zero).

Operators and Expressions

  • Mathematics: Basic operators include +, - , *, and /.

  • Integer Division: Dividing two int values results in an int (rounds toward zero). E.g., 59 / 60 = 0.

  • Modulo (%): Returns the remainder of division. Useful for checking divisibility or extracting digits.

  • Comparison (Relational): ==, !=, <, >, <=, >=. Result is a boolean.

  • Logical Operators: && (and), || (or), ! (not).

  • Concatenation: Using + with a String joins values end-to-end.

Control Flow

  • Conditionals: if, else if, and else blocks execute code based on boolean expressions.

  • Switch Statements: Useful for making a series of decisions based on a single expression value.

  • Loops:

    • while: Repeats code while a condition is true. Used when the number of iterations is indefinite.

    • for: Encapsulates initialization, condition, and update. Used for definite iteration.

    • Enhanced for: Iterates through every value in an array/collection (e.g., for (int value : values)).

  • Recursion: A method that invokes itself to solve a smaller version of the original problem. Requires a Base Case to prevent infinite recursion.

Objects and Memory Management

  • Attributes: Named data items within an object (also called fields).

  • Methods: Named sequence of statements.

    • Static: Belongs to the class (invoked as Math.sqrt()).

    • Instance: Belongs to a specific object (invoked as pixel.draw()).

  • References and Aliasing: Object variables store the memory address of the object. If two variables point to the same address, they are aliases; changes to one affect the other.

  • null: A special value indicating a variable does not refer to any object.

  • Garbage Collection: The JVM automatically reclaims memory from objects that no longer have active references.

Arrays and Collections

  • Arrays: Fixed-size sequences of elements of the same type. Indexes start at 0 and end at length - 1.

  • Multidimensional Arrays: Arrays of arrays (e.g., int[][] counts = new int[rows][cols];). Usually accessed via nested loops.

  • ArrayList: A collection that can grow and shrink in size. Requires wrapper classes (e.g., ArrayList<Integer>) for primitives.

Class Design and Inheritance

  • Encapsulation: Grouping related data (instance variables) and methods into a single class. Usually involves making variables private and providing getters/setters (Information Hiding).

  • Constructors: Special methods used to initialize new objects. They share the class name and have no return type.

  • Inheritance:

    • Subclass: Extends a superclass to inherit its attributes and methods while adding or specializing functionality.

    • IS-A vs HAS-A: Inheritance represents an "is-a" relationship (a Hand is a collection); composition represents a "has-a" relationship (a Game has a Scanner).

  • Abstract Classes: Declared using abstract. They cannot be instantiated and may contain abstract methods that subclasses MUST implement.

  • Interfaces: Define a set of method signatures that a class must provide using the implements keyword. Supports polymorphism.

Graphics and Event Handling

  • Coordinate System: Java uses an origin (0, 0) at the upper-left corner of the screen. X increases rightward, Y increases downward.

  • GUI (AWT/Swing): Uses JFrame for windows and Canvas for drawing surfaces.

  • Event-Driven Programming: Responding to user input (keyboard/mouse). Standard interfaces like KeyListener define methods like keyPressed and keyReleased.

Debugging and Testing

  • Compile-time Errors: Syntax violations detected by the compiler.

  • Run-time Errors (Exceptions): Occur during execution (e.g., NullPointerException, ArrayIndexOutOfBoundsException).

  • Logic Errors: The program runs but produces incorrect results.

  • Unit Testing: Using frameworks like JUnit to verify that individual methods work correctly by comparing expected and actual results.