Quiz 4 Start here
Lecture 1 and 2: Course Intro & Java Interfaces (Review)
● Consists of behaviors that classes can implement, class can implement 1+ interfaces
● Interface: abstract method declarations
○ Standard set-up of interface
○ public interface InterfaceName {
public void methodname();
}
○ Classes that implement interfaces
■ public class className/object implements InterfaceName {
■ @Override
■ public void methodNameFromInterface() {
■ ← Insert code here →
■ }
● @override means the method came from the interface
○ MUST HAVE ALL METHODS FROM THE INTERFACE IN THE CLASS OR IT
WILL NOT COMPILE
■ Classes inherit default methods, do not need to include static methods
■ You can override methods
○ Polymorphic variables: represents multiple things
■ Cannot have interfaceName x = new interfaceName();
■ Can have CanEat x = new classThatImplementsInterface();
● Use: className/param.takeOff();
● Cannot use with method not from interface
■ Using in parameter methodName(interFaceName param) {
○ Can make an array of classes that implement the interface
■ Interface[] things = new interfaceName[size]; / {new className(),...}
○ Even if all the classes that implement an interface have the same method, unless the
method is specifically stated in the interface itself, you cannot use
className/param.takeOff();
● Interfaces can contain:
○ Public instance method prototypes
○ Public static method implementations
○ Public final static variables ← MUST BE FINAL
○ Public “default” instance methods
■ Classes that implement the interface inherit method
■ Can override
● Interfaces cannot contain:
○ Constructors
○ Nothing private
○ Instance variables
○ Static variables that are not final
● dynamic dispatch: goes to the specific polymorphic object that implements the class and runs
their version of the method
,Quiz 4 Start here
Lecture 3: API and Encapsulation
● API: application programming interface
○ Features that others have access to (mostly the public features)
○ Public variables, public methods, contracts
○ Cannot access private features unless going through public methods
○ Javadoc defines the API for a class
○ If you change the structure, the api does not change because the way the user goes around
doing things did not change
● Encapsulation: hiding in a protective shell
○ Data: making data private
■ Does not give direct access, must go through public interface to get to data
■ Allows for control and change
○ Procedural: no promises about the algorithm chosen
○ “Encapsulate whatever might change” → no one is dependent on the structure you
choose, gives freedom to change mind without wrecking anything
Lecture 4: Java Wrapper Classes
● Primitive types: boolean, byte, char, int, float, double, long, short
● Most wrapper classes start with a uppercase instead of lowercase
○ Exception: char → Character, int → Integer
● Created wrapper classes to create collections full of values
○ Collection framework can only collect objects, not primitives
● Wrappers are immutable: cannot change data
○ Will not create multiple objects of the same value → aliases to one object
● Autoboxing: expecting an Integer but given int
○ Integer l = 7 same as = Integer.valueOf(5); ← THIS DOES NOT CREATE AN
OBJECT
● Autounboxing: expecting an int but given an Integer
○ Given that l is an integer, unboxes to use + operative
○ Integer d = l + 2
○ This also happens to box again to keep d as an Integer
● Integer l =Integer.valueOf(n);
○ If n is a value -128 to 127, l will be a reference to that value within the Integer class
Lecture 5: Intro to Java Collections
● There is a collections framework that allows easy access to to data structures and data types
○ They are classes and interfaces
○ Built into java, very easy use for data structures and data types
● ArrayList: does all the tricky parts of arrays (adding and removing elements)
○ ArrayList<type> varName = new ArrayList<>();
○ Prevents other types from being added
● For-each loops
○ for (TypeOfList varName : listName) {
○ varName is a reference to the object in the list
,Quiz 4 Start here
○ Removing results in ConcurrentModificationException
● The class Collections has static methods for useful things
○ Can not instantiate Collection varName = new Collection
○ The call on toString on ArrayList returns a string with [value, value, …]
○ Collections.staticMethod(collectionName)
■ .shuffle()
● Uniform distribution: each different permutation is equally likely
■ .max() ,.min(), .sort(), .binarySearch(collectionName, object)
● Collection must be comparable
● .binarySearch requires collection to be sorted, and returns index of object
■ .reverse()
Lecture 6: Enumerated Types
● Enums are ALMOST the same as classes
○ Differences:
■ 1) enum has a fixed number of objects inside, cannot create more
■ 2) constructors are private
● Can have instance and static members
● Specific number of objects (sort of like constants)
● public enum EnumName {
CONSTANT, CONSTANT, ...
● In order to access the constants: EnumName.CONSTANTNAME
● == is faster than .equals
● There is an order to the list in the enumeration, the order you write it in matters
○ You can loop through enumerations
■ EnumName.values() → returns array of the enum
○ In for-each loops
■ for (EnumName varName : EnumName.values()) {
○ Comparing:
■ EnumName.CONSTANTNAME.compareTo(EnumName.CONSTANTNAME)
■ a.compareTo(b) > 0 means a comes after b
■ a.compareTo(b) < 0 means a comes before b
○ Return index
■ EnumName.CONSTANTNAME.ordinal()
● Constructor information
○ Constructors are private
○ When making a constructor call, you attach it when creating the constants
○ constructor call: CONSTANT(instanceVar, instanceVar, ...)
Lecture 7: Iterators
● Iterator and iterable are interfaces
● Iterable: applies to a collection
● ArrayList implements iterable interface
● Collections that implement iterable are able to have an iterator to go through the elements 1 by 1
● Collections are sometimes iterable: collections are able to give an iterator
, Quiz 4 Start here
● Iterator: cycles through all the elements in the collection (like a for-each loop)
● Call iterator on collection list and it will return an iterator that allows to run through
collection
● T is a “parameterized type”
● Set up of iterator: Iterator<Type> iterator = collectionName.iterator();
○ collectionName is a collection of the type
■ It implements iterable interface (allowed to call iterator method)
■ Method returns iterator to go through the type one by one
○ boolean iterator.hasNext()
■ Returns true/false if there is an element after the current one
○ Type value = iterator .next()
■ Moves marker forward and returns an element
■ First calling returns first element
○ Void iterator.remove()
■ Removing through iterators are better than for-each or for loops
■ Call remove on iterator, not the list
■ Will remove the last element you had with next
■ If iterator.next() was 10, it will remove 10
● while(iterator.hasNext()) {
Integer value = iterator.next();
if (value < 50) { // auto-unboxing
iterator.remove();
Lecture 8: Java Memory Diagrams
● Three parts of a memory diagram:
○ Call stack: local variables are stored
■ Each thread has their own call stack
■ As soon as a method is called it gets pushed onto the call stack
■ Stores object variables with references (memory address) to object in heap
■ When a method is terminated, the method pops off and returns to the previous
method
○ Heap: objects are stored
■ Actual object is stored here
● Instance variables stored in object
○ Primitives stored with object
○ Other variables are created in the heap, instance var is reference to
object
○ Static corner: static variables
■ One copy for whole everyone to chare
■ Usually see static variables as ClassName.staticVar
● In a method call:
○ Local copies of primitive values and local copies of the memory address to objects
○ Two different variables point to the same object(aliaising)
○ PRIMITIVE VALUES WILL NOT CHANGE IN METHOD CALLS
■ Changes in current method but not the original method
Lecture 1 and 2: Course Intro & Java Interfaces (Review)
● Consists of behaviors that classes can implement, class can implement 1+ interfaces
● Interface: abstract method declarations
○ Standard set-up of interface
○ public interface InterfaceName {
public void methodname();
}
○ Classes that implement interfaces
■ public class className/object implements InterfaceName {
■ @Override
■ public void methodNameFromInterface() {
■ ← Insert code here →
■ }
● @override means the method came from the interface
○ MUST HAVE ALL METHODS FROM THE INTERFACE IN THE CLASS OR IT
WILL NOT COMPILE
■ Classes inherit default methods, do not need to include static methods
■ You can override methods
○ Polymorphic variables: represents multiple things
■ Cannot have interfaceName x = new interfaceName();
■ Can have CanEat x = new classThatImplementsInterface();
● Use: className/param.takeOff();
● Cannot use with method not from interface
■ Using in parameter methodName(interFaceName param) {
○ Can make an array of classes that implement the interface
■ Interface[] things = new interfaceName[size]; / {new className(),...}
○ Even if all the classes that implement an interface have the same method, unless the
method is specifically stated in the interface itself, you cannot use
className/param.takeOff();
● Interfaces can contain:
○ Public instance method prototypes
○ Public static method implementations
○ Public final static variables ← MUST BE FINAL
○ Public “default” instance methods
■ Classes that implement the interface inherit method
■ Can override
● Interfaces cannot contain:
○ Constructors
○ Nothing private
○ Instance variables
○ Static variables that are not final
● dynamic dispatch: goes to the specific polymorphic object that implements the class and runs
their version of the method
,Quiz 4 Start here
Lecture 3: API and Encapsulation
● API: application programming interface
○ Features that others have access to (mostly the public features)
○ Public variables, public methods, contracts
○ Cannot access private features unless going through public methods
○ Javadoc defines the API for a class
○ If you change the structure, the api does not change because the way the user goes around
doing things did not change
● Encapsulation: hiding in a protective shell
○ Data: making data private
■ Does not give direct access, must go through public interface to get to data
■ Allows for control and change
○ Procedural: no promises about the algorithm chosen
○ “Encapsulate whatever might change” → no one is dependent on the structure you
choose, gives freedom to change mind without wrecking anything
Lecture 4: Java Wrapper Classes
● Primitive types: boolean, byte, char, int, float, double, long, short
● Most wrapper classes start with a uppercase instead of lowercase
○ Exception: char → Character, int → Integer
● Created wrapper classes to create collections full of values
○ Collection framework can only collect objects, not primitives
● Wrappers are immutable: cannot change data
○ Will not create multiple objects of the same value → aliases to one object
● Autoboxing: expecting an Integer but given int
○ Integer l = 7 same as = Integer.valueOf(5); ← THIS DOES NOT CREATE AN
OBJECT
● Autounboxing: expecting an int but given an Integer
○ Given that l is an integer, unboxes to use + operative
○ Integer d = l + 2
○ This also happens to box again to keep d as an Integer
● Integer l =Integer.valueOf(n);
○ If n is a value -128 to 127, l will be a reference to that value within the Integer class
Lecture 5: Intro to Java Collections
● There is a collections framework that allows easy access to to data structures and data types
○ They are classes and interfaces
○ Built into java, very easy use for data structures and data types
● ArrayList: does all the tricky parts of arrays (adding and removing elements)
○ ArrayList<type> varName = new ArrayList<>();
○ Prevents other types from being added
● For-each loops
○ for (TypeOfList varName : listName) {
○ varName is a reference to the object in the list
,Quiz 4 Start here
○ Removing results in ConcurrentModificationException
● The class Collections has static methods for useful things
○ Can not instantiate Collection varName = new Collection
○ The call on toString on ArrayList returns a string with [value, value, …]
○ Collections.staticMethod(collectionName)
■ .shuffle()
● Uniform distribution: each different permutation is equally likely
■ .max() ,.min(), .sort(), .binarySearch(collectionName, object)
● Collection must be comparable
● .binarySearch requires collection to be sorted, and returns index of object
■ .reverse()
Lecture 6: Enumerated Types
● Enums are ALMOST the same as classes
○ Differences:
■ 1) enum has a fixed number of objects inside, cannot create more
■ 2) constructors are private
● Can have instance and static members
● Specific number of objects (sort of like constants)
● public enum EnumName {
CONSTANT, CONSTANT, ...
● In order to access the constants: EnumName.CONSTANTNAME
● == is faster than .equals
● There is an order to the list in the enumeration, the order you write it in matters
○ You can loop through enumerations
■ EnumName.values() → returns array of the enum
○ In for-each loops
■ for (EnumName varName : EnumName.values()) {
○ Comparing:
■ EnumName.CONSTANTNAME.compareTo(EnumName.CONSTANTNAME)
■ a.compareTo(b) > 0 means a comes after b
■ a.compareTo(b) < 0 means a comes before b
○ Return index
■ EnumName.CONSTANTNAME.ordinal()
● Constructor information
○ Constructors are private
○ When making a constructor call, you attach it when creating the constants
○ constructor call: CONSTANT(instanceVar, instanceVar, ...)
Lecture 7: Iterators
● Iterator and iterable are interfaces
● Iterable: applies to a collection
● ArrayList implements iterable interface
● Collections that implement iterable are able to have an iterator to go through the elements 1 by 1
● Collections are sometimes iterable: collections are able to give an iterator
, Quiz 4 Start here
● Iterator: cycles through all the elements in the collection (like a for-each loop)
● Call iterator on collection list and it will return an iterator that allows to run through
collection
● T is a “parameterized type”
● Set up of iterator: Iterator<Type> iterator = collectionName.iterator();
○ collectionName is a collection of the type
■ It implements iterable interface (allowed to call iterator method)
■ Method returns iterator to go through the type one by one
○ boolean iterator.hasNext()
■ Returns true/false if there is an element after the current one
○ Type value = iterator .next()
■ Moves marker forward and returns an element
■ First calling returns first element
○ Void iterator.remove()
■ Removing through iterators are better than for-each or for loops
■ Call remove on iterator, not the list
■ Will remove the last element you had with next
■ If iterator.next() was 10, it will remove 10
● while(iterator.hasNext()) {
Integer value = iterator.next();
if (value < 50) { // auto-unboxing
iterator.remove();
Lecture 8: Java Memory Diagrams
● Three parts of a memory diagram:
○ Call stack: local variables are stored
■ Each thread has their own call stack
■ As soon as a method is called it gets pushed onto the call stack
■ Stores object variables with references (memory address) to object in heap
■ When a method is terminated, the method pops off and returns to the previous
method
○ Heap: objects are stored
■ Actual object is stored here
● Instance variables stored in object
○ Primitives stored with object
○ Other variables are created in the heap, instance var is reference to
object
○ Static corner: static variables
■ One copy for whole everyone to chare
■ Usually see static variables as ClassName.staticVar
● In a method call:
○ Local copies of primitive values and local copies of the memory address to objects
○ Two different variables point to the same object(aliaising)
○ PRIMITIVE VALUES WILL NOT CHANGE IN METHOD CALLS
■ Changes in current method but not the original method