1/19
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
What type is an array in Java?
Arrays in Java are homogeneous: Can only hold items of a single type
For-each
• e will take the value of each
element in the array nums.
• Written this way, the loop will
automatically go through each
element in nums.
• Don’t need to keep track of index
or conditions, it’s done for us.
what does clone() do?
The clone() method in Java creates a copy of an array or object, duplicating its elements and structure. This allows for independent manipulation of the cloned object without affecting the original.
What does == do?
• When we use == with objects, we test object equality.
• “Are these two variables referring to the same object?”
• Declaring arrays with identical literals yields different objects
What do use to check content equality?
Use Arrays.equals()
What is a class?
A Class is a blueprint for creating objects
What are objects?
If the class is the blueprint, the actual car is the object.
Objects are instances of Classes:
• A string object is an instance of the String class
in the same way that 7 is an instance of int
• Classes may implement methods (behaviors)
that their objects can invoke.
• I.e. subString(), charAt(), etc. from String class
Access specifier: private
Access specifier:
• Private means this variable cannot be
accessed from outside the Counter class.
Let’s add some properties and behaviors to Counter
Access specifier: Public
Access specifier:
Public means this method can be
accessed from outside the Counter class
Mutator
Also known as the setter method. Modifies object data
Accessor
Also known as the getter method. Retrieves object data.
Set method
• Set methods ensure all possible operations upon the data fields of a given class fall within the intended domain of that class
Constructors
A constructor can be used to initialize
object values or perform some behavior
upon object creation.
Every class must have a constructor. We
haven’t created one yet, but Java
synthesizes a default, empty constructor
for you if you don’t provide your own. • When we use new to create an object, we invoke its constructor.
Can two methods have the same name
Two methods can have the same name IF
their parameter lists are different
Arrays.sort()
is a method used to sort elements in an array in ascending order. It can also sort arrays of objects that implement the Comparable interface.
s.toCharArray()
is a method that converts a string into an array of characters.
Arrays.toString()
is a method that returns a string representation of the contents of an array, displaying its elements in a readable format.
‘==’ vs .equals()
The ‘==’ operator compares reference equality, checking if two references point to the same object, while the .equals() method checks for value equality, determining if two objects are logically equivalent.
constructors
are special methods used to initialize objects in a class, setting initial values for instance variables. Every class must have a constructor. haven’t created one yet, but Java
synthesizes a default, empty constructor
for you if you don’t provide your own. • A constructor looks like a method.
• The name of a constructor must
match the name of the class