Chapter 6 Flashcards
What does the Java class library documentation include
The interface of the class:
-class name
-class description
-list of constructors and methods
-return values & parameters for constructors & methods
-a description of purpose of each constructor & method
The Java class library documentation does not include___
The implementation of the class:
-private fields
-private methods
-the bodies (source code) of methods
Using library classes
- classes organized into packages
- classes from the library must be imported using an import statement (except classes from the java.lang package)
- can then be used like classes from the current project
Methods from string
- contains
- ends with
- index of
- substring
- to UpperCase
- trim
T or F: strings are mutable
F: strings are immutable
Which is the correct way to modify a string?
input.toUpperCase();
or
input = input.toUpperCase();
2
strings cannot be modified
the correct one doesn’t modify but returns a new string
What’s the difference?
if(input == “bye”)
and
if(input.equals(“bye”))
1 tests identity
#2 tests equality - whether 2 objects contain the same state
Always use .equals for text equality
== vs .equals
- The main difference between the .equals() method and == operator is that one is a method, and the other is the operator.
- We can use == operators for reference comparison (address comparison) and .equals() method for content comparison. In simple words, == checks if both objects point to the same memory location whereas .equals() evaluates to the comparison of values in the objects.
using random
- Class can be used to generate random numbers
Random rand = new Random();
Parameterized classes (generic)
- the documentation includes provision for a type parameter:
ArrayList<E></E> - These type names reappear in the parameters & return types
E get (int index)
boolean add(Ee) - the types in the documentation are placeholders for the types we use in practice
An Array List <Ticket> actually has methods:
TicketMachine get(int index)
boolean add(TicketMachine e)</Ticket>
Packages and import
- single classes may be imported
import java.util.ArrayList; - whole packages can be imported:
import java.util.*; - importation does not involve source code insertion
- Some classes are used so frequently that almost every class would import them like class string
maps
- collections that contains pairs & values
- pairs consist of a key and a value
- lookup works by supplying a key & removing a value
- ideal for one way lookup not reverse
i.e. telephone book
List, map and set
- alternative ways to group objects
- varying implementations available
- but hashMap is unrelated to Hashset
Set
A collection that stores each individual element at most once
- does not maintain any specific order
set vs ArrayList
- Identical to ArrayList except we used Hashset
- A list will keep all elements entered in the desired order but set doesn’t
- List provides access to elements by an index & can contain same element multiple times
- each element in a set at most once