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
newkeyword.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 is Sunday and index 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.SUNDAYto equalDay.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
Objectclass.
Ordinal Method
The
ordinal()method returns:The numerical position (index) of the constant in its enum declaration.
Counting starts at . (e.g., January is , February is ).
The compareTo Method
Enums implement the
Comparableinterface.It compares constants based on their ordinal values.
Example: For
Day d1 = Day.TUESDAYandDay d2 = Day.SATURDAY,d1.compareTo(d2)yields:if
d1is the same asd2.A negative integer if
d1comes befored2().A positive integer if
d1comes afterd2.
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
PlayingCardclass with fields forRankandSuit.Use enums for
Rank(Ace, Two, … King) andSuit(Hearts, Diamonds, Clubs, Spades) to restrict invalid card creation.
Introduction to ArrayLists
An
ArrayListis a class in thejava.utilpackage 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.
ArrayListhandles this internally.Functionality:
ArrayListincludes built-in methods for common tasks like searching, sorting, and clearing.Types: Arrays can hold primitives (
int,char) or objects.ArrayListcan 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(): Returnstrueif the list contains no elements.
Iterating through ArrayLists
You can use a standard
forloop with index access:for (int i = 0; i < list.size(); i++) { ... list.get(i) ... }
Or the enhanced
for-eachloop: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
sizeexceedscapacity, theArrayListcreates a larger internal array (usually times larger) and copies the elements.
Differences in Data Type Handling - Wrapper Classes
ArrayListrequires objects. To store primitive values, Java uses Wrapper Classes:int➔Integerdouble➔Doublechar➔Characterboolean➔Booleanbyte➔Byte,short➔Short,long➔Long,float➔Float.
Autoboxing and Unboxing: Java automatically converts between primitives and their wrapper classes (e.g., adding an
intto anArrayList<Integer>automatically wraps it in anIntegerobject).