explain the importance of APIs, documentation and packages in Java
A string holds characters in a sequence.
Each character is at a position or index which starts at 0 as shown below.
An index is a number associated with a position in a string.
The length of a string is the number of characters in it including spaces or special characters.
The following string has a length of 14 (notice the index of the last character is 13):
For the exam, you only need to know how to use the following String methods.
All of these method descriptions are included in the Java Quick Reference Sheet that you get during the exam, so you don't have to memorize them.
int length() method returns the number of characters in the string, including spaces and special characters like punctuation.
String substring(int from, int to) method returns a new string with the characters in the current string starting with the character at the from index and ending at the character before the to index (if the to index is not specified it will contain the rest of the string).
int indexOf(String str) method searches for the string str in the current string and returns the index of the beginning of str in the current string or -1 if it isn't found.
int compareTo(String other) returns a negative value if the current string is less than the other string alphabetically, 0 if they have the same characters in the same order, and a positive value if the current string is greater than the other string alphabetically.
I'm pretty sure that the number used to describe how the strings are greater than or less than come from the ASCII encoding scheme:
boolean equals(String other) returns true when the characters in the current string are the same as the ones in the other string. This method is inherited from the Object class, but is overridden which means that the String class has its own version of the method.
In the following code, I used all of the String methods described above:
IMPORTANT: Remember that the substring(int from, int to) method does not return the character at the "to" index. So, to return a character at a specific index, use substring(index, index + 1).
Also, Strings are immutable which means they can't change. Anything that you do to modify a string (like creating a substring or appending strings) returns a new string.
There are also methods you can use to make all of the characters in a String uppercase or lowercase:
COMPARETO AND EQUALS
We can compare primitive types like int and double using operators like == and > or <, however, with reference types like String, you MUST use the equals or compareTo methods.
The equals method compares the two strings character by character and returns true or false whereas the compareTo method returns a number describing the distance between the first letter that is different (positive or negative).
Both methods (compareTo and equals) are case-sensitive but there are some useful case-insensitive versions of these methods, compareToIgnoreCase and equalsIgnoreCase, which are not on the exam:
There are lots of other methods in the String class.
Here is a link to the Java documentation for the String class:
You don't have to know ALL of the String methods for the exam, but you can use them if you want!
An Application Programming Interface (API) is a library of prewritten classes that simplify complex programming tasks for us.
These classes are grouped together in a package like java.lang and we can import these packages (or individual classes) into our programs to make use of them.
We already imported the Scanner class into our programs using import java.util.Scanner; but we could have imported the entire java.util package using import java.util.*;
The String library is built into the default java.lang package which we do not need to import and takes care manipulating strings for us.
There are many other useful library packages as well; documentation for APIs and libraries are essential to understanding how to use these classes.
COMMON MISTAKES WITH STRINGS
Here are some common mistakes made with Strings:
1) Thinking that substrings include the character at the "to" index.
2) Thinking that strings can change when the can't. They are immutable.
3) Trying to access part of a string that is not between index 0 and length - 1. This will throw and IndexOutOfBoundsException:
4) Trying to call a method like indexOf on a string reference that is null. You will get a null pointer exception.
5) Using == to test if two strings are equal. This is actually a test to see if they refer to the same object. Usually, you only want to know if they have the same characters in the same order so you should use equals or compareTo instead.
6) Treating upper and lower case characters the same in Java. If s1 = "Hi" and s2 = "hi" then s1.equals(s2) will return false.
SUMMARY
index - A number that represents the position of a character in a String. The first character in a String is at index 0.
length -the number of characters in a String.
substring - A new String that contains a copy of part of the original String.
A String object has index values from 0 to length - 1. Attempting to access indices outside this range will result in an IndexOutOfBoundsException.
String objects are immutable, meaning that String methods do not change the String object. Any method that seems to change a String actually creates a new String.
The following String methods and constructors, including what they do and when they are used, are part of the Java Quick Reference Sheet (can be found on the Resources page here on this website) that you will be provided with during the exam:
String (String str) constructs a new String object that represents the same sequence of characters as str.
int length( ) returns the number of characters in a String object.
String substring(int form, int to) returns the substring beginning at index from and ending at index (to -1).
int indexOf(String str) searches for str in the current string and returns the index of the first occurrence of str; returns -1 if not found.
boolean equals(String other) returns true if this (the calling object) is equal to other; returns false otherwise.
int compareTo(String other) returns a value < 0 if this is less than other; returns zero if this is equal to other; returns a value > 0 if this is greater than other.
str.substring(index, index + 1) returns a single character at index in string str.
EVIDENCE
1) Complete the following Google Form. This form must be 100% correct and includes released AP practice questions. To stop working and return later, hit submit! You can "edit your response" and continue where you left off.
2) I challenge you to the String-Thing game! Make sure to click "Strings", and check the box for "Timer" (you can check the box for "Sound" too if you would like). Send a screenshot to coombs_d@milfordschools.orgof your high score! The MINIMUM score is 50.