The Java API
and the String Class
Overview
The Java API (Application Programming Interface)
- Is a large library of classes and interfaces
- Is not part of the core Java language
- Is part of the Java SDK (Software Development Kit) along with the
Java compiler (javac) and JVM (Java Virtual Machine/java).
- Is heavily used by Java developers
- Is a gold mine of well-tested code that can save you countless hours of development time
The String Class
- Is one of the most commonly-used classes in the API
- Is used to create Strings (e.g. String s = new String("hello, world"))
- Has many utility methods for String-processing
Goals
- Learn how to search for useful code in the API
- Become familiar with the methods in the String class
Part 1: Tour the API
- Click
here to go the the Java 1.5 API (note, this is also accessible from
the course web page).
- Explore around for a bit
Part 2: API Explained
- The top left area is listing of all known packages. Scroll though and see if
you recognize any:
- java.io
- java.lang
- java.util
- Others...?
- The area below lists all of the classes in the selected package.
- Note - can also make this show all classes in all packages
- Clicking on a class name will make information appear in the large area
to the right. This is just like the javadoc pages we are used to.
- Take a look at the table below. This shows the general layout of the API system.
JavaTM 2 Platform
Standard Ed. 5.0
Packages
java.applet
...
java.util
... |
Overview ... Tree Index Help |
java.lang
Class String
|
| Field Summary |
| |
| Constructor Summary |
Classes
AbstractAction
...
ArrayList
...
Character
...
Comparable
...
LinkedList
...
List
...
Math
...
Random
...
Scanner
...
String
... |
|
| Method Summary |
char |
charAt(int index) |
| |
|
int |
compareToIgnoreCase(String str) |
| |
|
boolean |
contains(CharSequence s) |
| |
|
boolean |
equals(Object anObject) |
boolean |
equalsIgnoreCase(String anotherString) |
| |
|
int |
indexOf(int ch) |
| |
|
int |
length() |
| |
|
String |
replaceAll(String regex, String replacement) |
| |
|
String[] |
split(String regex) |
Part 3: String Class
- Using the API class listing, navigate to the page for the String class.
- Briefly look through the various declarations that the class makes,
paying special attention to the methods that are available.
- Items that you should definitely look at:
- chartAt(int index)
- compareTo(String other) and compareToIgnoreCase(String other)
- indexOf(String other)
- length()
- replace(char old, char replacement)
- substring(int begin, int end) <--Very Useful!
- toLowerCase() and toUpperCase()
- Many of the other methods can also make your life much easier and
your code simpler
Some code (and questions) utilizing the String class's methods:
String s1 = "Popsicle";
String s2 = "Gibraltar";
int r1 = s2.indexOf("alt");
String r2 = s1.substring(0,3);
s1.concat(" time!");
char c = s1.charAt(5);
What are the values of r1, r2, s1, and c after those 6 lines of code run?
[See bottom of page for answers.]
- Don't forget the "syntactic sugar" that is provided in Java
to make things easier:
- Inline creation of String literals (String s = "Hello";)
- Easy concatenation:
- Also remember
- Strings are immutable
s1 == s2 is not equivalent to s1.equals(s2)
- One other class which you may find useful (especially in the next two homework assignments: StringBuffer
- Basically is a String that you can append to as you build it.
Answers to questions:
r1 = 4
r2 = "Pop" //Remember, substring(x,y) is all characters after and including x that appear before y.
s1 = "Popsicle" //Very important - Strings are immutable! The concat method does not change s1, it returns a new String which can be assigned.
c = 'c'