Java String Class String Methods
ÿ More on useful String methods
ÿ String is a class þ Examples. What are the values?
þ Do not need new to create String String demo = ”How are things?”;
demo.substring(8, 12)
String msg = ”hello”; demo.indexOf(”wa”)
ÿ Can join strings (concatenate) with + demo.indexOf(”w a”)
demo.charAt(7);
þ String mail = ”John says ” + msg;
ÿ Most common String methods: þ Other common String methods
þ int length(); // get number of chars in it boolean equals(String s) // equality of contents
int compareTo(String s) // -1, 0, +1 : <, ==, >
þ String substring(int start, int stop);
String substring(int start) // end of string
// substring gets part of string
þ int indexOf(String key); // finds loc of key þ Examples. What are the values?
þ char charAt(int index); // get a single char demo.comparetTo(”how are things?”)
demo.equals (”how are things?”)
demo.substring(10)
CompSci 100E 5.1 CompSci 100E 5.2
Why Inheritance? Example of Inheritance
ÿ Add new shapes easily ÿ What is behavior of a shape?
shape without changing much
code
þ Shape s1 = new Circle();
void doShape(Shape s) {
mammal þ Shape s2 = new Square(); System.out.println(s.area());
ÿ Interface/abstract base class: System.out.println(s.perimeter());
FullHouse, LargeStraight þ interface or abstraction s.expand(2.0);
ScoreEntry þ Function called at runtime System.out.println(s.area());
ÿ concrete subclass System.out.println(s.perimeter());
User’s eye view: think and þ All abstract functions }
program with abstractions, realize implemented
different, but conforming þ Later we'll override Shape s1 = new Circle(2);
implementations, ÿ “is-a” view of inheritance Shape s2 = new Square(4);
þ Substitutable for, usable in Shape s3 = new Rectangle(2,5);
don’t commit to something all cases as-a doShape(s1); doShape(s2); doShape(s3);
concrete until as late as possible
CompSci 100E 5.3 CompSci 100E 5.4
Inheritance (language independent) What can an Object do (to itself)?
ÿ First view: exploit common interfaces in programming ÿ http://java.sun.com/j2se/1.5.0/docs/api/
þ Iterators in Java or C++ þ Look at java.lang.Object
þ Implementation varies while interface stays the same ÿ toString()
þ Used to print (System.out.println) an object, overriding
ÿ Second view: share code, factor code into parent class toString() can result in 'useful' information being printed,
þ Code in parent class shared by subclasses also used in String concatenation: String s = x + y;
þ Subclasses can override inherited method þ Default is basically a pointer-value
o Subclasses can override and call ÿ equals()
þ Determines if guts of two objects are the same, must
ÿ Polymorphism/late(runtime) binding (compare: static) override, e.g., for using a.indexOf(o) in ArrayList a
þ Function actually called determined when program runs, not þ Default is ==, pointer equality
when program is compiled ÿ hashCode()
þ Hashes object (guts) to value for efficient lookup
CompSci 100E 5.5 CompSci 100E 5.6
Objects and Values Objects, Values, Classes
ÿ Primitive variables are boxes ÿ For primitive types: int, char, double, boolean
þ think memory location with value þ Variables have names and are themselves boxes (metaphorically)
þ Two int variables assigned 17 are equal with ==
ÿ Object variables are labels that are put on boxes
String s = new String("genome"); ÿ For object types: String, Sequence, others
String t = new String("genome"); þ Variables have names and are labels for boxes
þ If no box assigned, created, then label applied to null
if (s == t) {they label the same box}
þ Can assign label to existing box (via another label)
if (s.equals(t)) {contents of boxes the þ Can create new box using new
same}
ÿ Object types are references or pointers or labels to storage
s t
What's in the boxes? "genome" is in the boxes
CompSci 100E 5.7 CompSci 100E 5.8
Java Arrays Java Arrays
ÿ Fixed size, once created ÿ Can also create arrays by specifying initial values
þ Can hold primitive types þ Avoids need for new
þ Can hold objects (references) þ Avoids need to count the number of values
ÿ Example: Creating an array of doubles ÿ Example: Creating an array of ints
double[] times; int[] counts = { 3, 12, 0, 8, 10};
times = new double[30]; // or could combine w prev þ Use counts.length to get size of array
ÿ Example: Creating an array of DLicenses ÿ Example: Creating an array of Strings
DLicense[] dls; String[] aHotel = {”Hilton”, ”Swans”, ”Astoria”};
dls = new DLicense[50]; // create array (or combine) String[] bHotel = {”Kwik8”, ”SleepyT”, ”TuckUIn”};
for (int k; k < dls.length; k++) { String[] cHotel = {”DiveX”, ”RRXing”, ”Swampys”};
dls[k] = new DLicense(); // create objects in dls ÿ Example: Creating an array of arrays (matrix)
} String[][] hotelChoice = {aHotel, bHotel, cHotel};
CompSci 100E 5.9 CompSci 100E 5.10
For-Each Loop (new with Java 5) Java ArrayList Class
ÿ For Arrays (and Collections) May Use Special Loop ÿ Flexible Arrays
þ Syntax þ Grows in size as needed!
for (Type name : expression){ þ Many different methods to improved array processing
body of loop ÿ Create with:
}
ArrayList vect = new ArrayList();
þ Type is the type of object returned for use in loop
þ name is of variable that take on value for use in loop
ÿ Uses: (assume dl, sl, are DLicense objects)
vect.add(dl); // add to “end”
þ expresssion is and array or collection
vect.add(k, dl); // insert at position k (shifts!)
ÿ Example: (dl is a DLicense object and dls an array of dl) sl = (DLicense) vect.get(m); // retrieve from
for (DLicense dl : dls) { // position m – note cast to DLicense
System.out.println(dl.getName() + ” ” ÿ Note that [ ] brackets don’t work ! ! !
+ dl.getNum()) þ Also see: remove(), indexOf(), toArray(), contains(),
} size(), ... Look them up!
þ But cannot change entries! (effectively dealing with copy)
CompSci 100E 5.11 CompSci 100E 5.12
Java ArrayList Class
ÿ Generic forms
þ Previous example stored items as Objects
þ On retrieving, needed to cast back to original class
ÿ Create with:
ArrayList<DLicense> vect = new ArrayList<Dlicense>();
ÿ Uses: (assume dl, sl, are DLicense objects)
vect.add(dl); // add to “end”
vect.add(k, dl); // insert at position k (shifts!)
sl = vect.get(m); // get at position m: no cast needed
for (DLicense cl : vect) {
System.out.println(”Number is ” + cl.getNum());
}
CompSci 100E 5.13