CompSci Notes (1/14/26)

Objects of Type A in Memory - Enums
  • Objects of type A (Enumerated types) are fixed in memory.

  • Total Count: Seven (in the context of the example, though enums can have any fixed number).

  • Characteristics:

    • Cannot create new objects at runtime using the new keyword.

    • Cannot delete existing objects.

    • Modification of objects is possible only through editing the source code declaration.

Use Cases
  • Ideal for scenarios where a fixed set of constants is required.

  • Example: Days of the week (Sunday through Saturday).

  • Additional use: Names for months, directions (North, South, East, West), or any categorization that prevents unneeded object creation.

Referencing Objects
  • To create references to existing objects:

    • Use syntax: DataType variableName (e.g., Day today;).

    • Instead of creating new objects, assign the reference using the fully qualified name.

  • Definition of fully qualified name: EnumName.CONSTANT_NAME (e.g., Day.SUNDAY).

Methods and Functions of Enum Types
  • Enumerated types are specialized classes in Java, inheriting from java.lang.Enum.

  • Common methods include:

    • equals(): Returns true if the constants are the same.

    • toString(): Returns the name of the constant as a string.

    • valueOf(String name): Returns the enum constant of the specified string name. This is case-sensitive and must match exactly.

  • Customization:

    • Enums can have fields, constructors, and methods just like regular classes.

    • Constructors are always private or package-private; you cannot invoke an enum constructor outside of the enum itself.

The values Method
  • The values() method is a static method provided by the compiler:

    • Returns an array containing all the constants of the enum type in the order they are declared.

    • Example: Day[] allDays = Day.values(); returns an array where index 00 is Sunday and index 66 is Saturday.

Comparing Enumerated Types
  • Comparison can be performed using:

    • == operator: Checks if both references point to the exact same enum constant in memory.

    • equals() method: For enums, equals() and == are functionally identical because there is only one instance of each constant.

  • Uniqueness: Each enum constant has a unique memory address. It is impossible for Day.SUNDAY to equal Day.TUESDAY.

Printing Enum Values
  • Using System.out.println(Day.TUESDAY):

    • Prints "TUESDAY" because the toString() method is automatically overridden to provide the name of the constant.

    • This is more user-friendly than the default behavior of the Object class.

Ordinal Method
  • The ordinal() method returns:

    • The numerical position (index) of the constant in its enum declaration.

    • Counting starts at 00. (e.g., January is 00, February is 11).

The compareTo Method
  • Enums implement the Comparable interface.

  • It compares constants based on their ordinal values.

  • Example: For Day d1 = Day.TUESDAY and Day d2 = Day.SATURDAY, d1.compareTo(d2) yields:

    • 00 if d1 is the same as d2.

    • A negative integer if d1 comes before d2 (26=42 - 6 = -4).

    • A positive integer if d1 comes after d2.

Implications of Comparison Results
  • Negative values indicate that the calling object appears earlier in the enum declaration than the argument object.

  • Since ordinal values are fixed, the "natural order" of enums is determined by the order in which they are written in the code.

Coming Lab on Playing Cards
  • Objectives:

    • Create a deck of cards using an ArrayList.

    • Implement a PlayingCard class with fields for Rank and Suit.

    • Use enums for Rank (Ace, Two, … King) and Suit (Hearts, Diamonds, Clubs, Spades) to restrict invalid card creation.

Introduction to ArrayLists
  • An ArrayList is a class in the java.util package that provides a dynamic array.

  • Key Characteristics:

    • It can grow and shrink in size dynamically as elements are added or removed.

    • It automatically handles the underlying array resizing and element shifting.

Differences Between Arrays and ArrayLists
  • Fixed vs. Dynamic: Arrays have a fixed size defined at creation. To change size, a new array must be created and data copied. ArrayList handles this internally.

  • Functionality: ArrayList includes built-in methods for common tasks like searching, sorting, and clearing.

  • Types: Arrays can hold primitives (int, char) or objects. ArrayList can only hold objects.

Basic ArrayList Operations
  • Declaration and Instantiation:

    • ArrayList<DataType> listName = new ArrayList<DataType>();

    • The Diamond Operator: In modern Java, you can use: ArrayList<String> names = new ArrayList<>();.

  • Common Methods:

    • add(element): Appends an element to the end.

    • add(index, element): Inserts element at a specific position, shifting existing elements.

    • get(index): Returns the element at the specified index.

    • set(index, element): Replaces the element at the index with a new value.

    • remove(index): Removes the element and shifts subsequent elements to the left.

    • size(): Returns the current number of elements.

    • clear(): Removes all elements.

    • isEmpty(): Returns true if the list contains no elements.

Iterating through ArrayLists
  • You can use a standard for loop with index access:

    • for (int i = 0; i < list.size(); i++) { ... list.get(i) ... }

  • Or the enhanced for-each loop:

    • for (String name : names) { System.out.println(name); }

Understanding ArrayList Capacity and Size
  • Size: The current number of elements stored in the list.

  • Capacity: The size of the internal array used to store the elements. When size exceeds capacity, the ArrayList creates a larger internal array (usually 1.51.5 times larger) and copies the elements.

Differences in Data Type Handling - Wrapper Classes
  • ArrayList requires objects. To store primitive values, Java uses Wrapper Classes:

    • intInteger

    • doubleDouble

    • charCharacter

    • booleanBoolean

    • byteByte, shortShort, longLong, floatFloat.

  • Autoboxing and Unboxing: Java automatically converts between primitives and their wrapper classes (e.g., adding an int to an ArrayList<Integer> automatically wraps it in an Integer object).