2.1 Objects: Introduction
Object: a grouping of variables and methods
Abstraction, information hiding, or encapsulation: to have a user interact with an item at a high-level, with lower-level internal details hidden from the user
Abstract data type (ADT): a data type whose creation and update are constrained to specific well-defined operations; can be implemented using a class
NOTE: abstraction allows user to focus on few high-level operations to user and hides low-level operations (Ex., no direct read and write; use accessor and mutator methods instead)
2.2 Obejcts and references
Reference: a variable type that referes to an object
// Person: reference type
// p: reference variable
// Person(): constructor call
// new Person(): instantiate the Person class
Person p = new Person();2.3 Primitive and reference types
Primitive type: a variable directly stores the data for that variable type
Reference type: a variable can refer to an instance of a class (object)
NOTE: many of Java’s built-in classes only wok with objects
Java wrapper class: built-in reference types that agument the primitive types and provide methods
Wrapper class objects and String objects are immutable (cannot change the object via methods or variable assignments after object creation)
A new memory allocation occurs every time when assign value to an Integer variable
Wrapper classes (except for Character and Boolean) declare MAX_VALUE and MIN_VALUE
NOTE: Java caches Integer obejcts for literal values [-128, 127]
NOTE: do not use == or != to compare two reference variables because these operators compare object references, use .equals() and .compareTo() instead
2.4 Parameters of reference types
Reference variable: variable that points (refers) to an object or array
NOTE: wrapper class objects are stored on heap
Parameter: a variable in function signiture
Argument: the actual passed data
2.5 Wrapper class conversions
Autoboxing: automatic coversion of primitive types to corresponding wrapper classes
Unboxing: automatic conversion of wrapper class objects to the corresponding primitive types
// autoboxing (assign primitive type to wrapper class variable)
Integer finalScore = 95;
int quizScore = 88;
Integer recordedScore = quizScore;
// autoboxing (pass primitive type to a method with wrapper class parameter)
void print(Integer score) {...}
int examScore = 90;
print(100);
print(examScore);
// unboxing (assugn wrapper class variable to primitive type)
int numericScore = finalScore;
// unboxing (pass wrapper class variable to a method with primitive type parameter)
void print(int score) {...}
print(finalScore);
// unboxing (combine wrapper class and primitive types in expression)
int adjustedScore = finalScore - 10;Integer distance = 10;
// converting wrapper class objects to primitive types
distance.intValue();
distance.doubleValue();
distance.longValue();Integer velocity = 2;
velocity.toString();
Integer.toString(5);
Integer.parseInt("12"); // returns int
Integer.valueOf("24"); // returns Integer object\
Integer.toBinaryString(32);2.6 ArrayList
ArrayList: an ordered list of reference type items
ArrayList<Integer> list = new ArrayList<Integer>();
// add element at the end
list.add(1);
list.add(2);
list.add(3);
// returns element at index
list.get(0);
// replace element at index with specified element
list.set(0, 5);
// returns the size
int size = list.size();2.7 Introduction to memeory management
ArrayList stores a list of elements in contiguous memory locations
Add and remove elements in the middle of an ArrayList take O(n) time because elements must shift
Linked list: a list wherein each item contains not just data but also a reference to the next item in the list
| ArrayList | Linked list | |
|---|---|---|
| Memeory management | Stores items in contiguous memory locations | Stores each item anywhere in memory, with each item referring to the next item in the list |
| Memory usage | Less | More |
| Insert and remove | Slow | Fast |
| Access | Fast | Slow |
2.8 Memory regions: Heap/Stack
Code: program instructions region
Static memory: static field region (allocate once and last duration of a program’s execution)
Stack (automatic memory): local variables region, allocated during a method call (a method call adds local variables to stack and return removes them)
Heap (free storage): region where the new operator allocates memory for objects
Method area: the region that code and static memory regions integrated into, also stores information for every class type used in the program
2.9 Basic garbage collection
Garbage collection: finds all unreachable allocated memory locations and automatically frees such memory locations in order to enable memory reuse
Reference count: count of all reference variables that are currently referring to an object
Unreachable object: zero reference count for the object
2.10 Garbage collection and variable scope
The JVM can automatically infor a null reference once the variable goes out of scope