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
Situations to use wrapper classes
int, boolean & char
- separate from objects
- their values are not instances of classes
- so not normally possible to add them to a collection
- Wrapper classes are the solution
- wrapper class for int = Integer
Integer iwrap = new Integer(ix);
Autoboxing
- performed automatically when a primitive type value is used in a context requiring a wrapper type
- It makes storing primitive values on a object collection easier
- applied whenever a primitive type value is passed as a parameter to a method that expects a wrapper type & when a primitive type value is stored in a wrapper type variable
Unboxing
- reverse of autoboxing, also automatic
- applied when a wrapper type value is passed as a parameter to a method that expects a primitive type value & when stored in a primitive type variable
Ways to use javadoc
- switching the pop-up selector @ the top right of the editor window from source code to documentation (or by using toggle documentation view from the editor’s tools menu)
- Use project documentation function from main windows tools menu to generate documentation for all classes in the project
What should class documentation include?
- class name
- comment describing overall purpose & characteristics of the class
- a version #
- author’s names
- documentation for each constructor & each method
What should constructor & method documentation include?
- method name
- return type
- parameter names & types
- descriptor of purpose & function of the method
- description of each parameter
- description of value returned
public vs private
- public elements are accessible to objects of other classes- fields, constructors & methods
- fields should not be public
- private elements are accessible only to objects of the same class
- only methods that are intended for other classes should be public
information hiding
- Data belonging to on object is hidden from other objects
- know what an object can do, not how it does it
- increases the level of independence
- independence of modules is important for large systems & maintenance
- private keyword enforces info hiding
code completion
- the BlueJ editor supports lookup of methods
- use ctrl-space after a method-call dot to bring up a list of available methods
- use return to select a highlighted method
class variables
- shared b/t all instances of the class
- it belongs to the class & exists independent of any instances
- designated by the static keyword
- public static variables are accessed via the class name
Constants
- a variable, once set, can have its value fixed
- designated by final keyword
final int max = list.size(); - final fields must be set in their declaration or the constructor
- combining static & final is common
class constants
- static: class variable
- final: constant
- private static final int gravity = 3;
- Public visibility is less of an issue with final fields
- upper case names often used for class constants:
public static final int BOILING_POINT = 100;
class methods
- class methods are conceptually related to class variables & use a related syntax (the static keyword)
public static int getNumberOfDaysThisMonth()
limitations of class methods
- can’t access any instances fields defined in the class
- can’t call an instance method from the class
main method
public static void main (String args [])
- to access program without BlueJ- user specifies class and Java will then invoke a method called Main in that class