2_cps209_classes_objects_1
2_cps209_classes_objects_1
Computer Science II
Prof. Alex Ufkes
Lists in Python are heterogeneous: They can hold any type, any object
Arrays in Java are homogeneous: Can only hold items of a single type
Use Arrays.equals()
“Hello”
We get different
“Hello” objects! “Hello”
© Alex Ufkes, 2020, 2025 35
Why Does This Matter?
?
© Alex Ufkes, 2020, 2025 37
Strings in Java: Equality
“Hello” “Hello”
© Alex Ufkes, 2020, 2025 38
Strings in Java: Equality
string1.equals(string2)
In Python: In Java:
• Strings are immutable • Strings are immutable
• == tests content equality • == tests object equality
• Lists are heterogeneous (when comparing objects)
• Lists are mutable • Arrays are homogeneous
• Lists can grow or shrink • Arrays are mutable
(append(), remove()) • Arrays are fixed in size
Next week we’ll see Java’s ArrayList class, which more closely
mirrors the utility and capability of Python Lists
Properties
Behaviors
Access specifier:
• Private means this variable cannot be
accessed from outside the Counter class.
Access specifier:
Public means this method can be
accessed from outside the Counter class.
Accessor
Why bother with sets and gets when we can just make the instance variable public?
?
© Alex Ufkes, 2020, 2025 62
But Why?
Why bother with sets and gets when we can just make the instance variable public?
For example:
• Were value public, the programmer would be free to add 17 to it.
• This is not possible given the actual physical system we are trying to
represent with our Clicker class.
public:
Member (variable or method)
is visible to all classes.
Visible!
• Static members
• More useful built-in
Java classes
• More class examples