💻AP Computer Science A
8 min read•Last Updated on July 22, 2024
Avanish Gupta
user_sophia9212
Avanish Gupta
user_sophia9212
Although there is a lot you can do with the factory version of Java, which is the version you get that comes right with the Java installation, there is a lot more to Java as well. You can import external libraries and APIs (application program interfaces) from the Java installation that aren't pre-loaded into your IDE or from the internet for use in your programs. These will significantly improve the functionalities of the programs that you can make.
All Java classes, libraries, and APIs come with documentation, which lists the methods of the class and also how to use the class and its methods. Here is the official Java documentation for the String class:
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html
Courtesy of Java.
The String class is a class in the java.lang package that comes when you install Java on your computer. For your convenience, any classes from the java.lang package does not need to be imported to the program as we did with the Scanner class back in Unit 1. You can see the documentation for the String class above!
With strings, we can access substrings and characters in the string. A substring is a string that is included within a larger string, while a character is a substring with a length of 1. First, we need to figure out the length of a string, which is the number of characters in the string. To do this, we use the following method:
int length(String str) This returns the length of the string. With this number, we can finally access substrings! To use this, we use this method:
String substring(int beginIndex, int endIndex) This method returns the substring starting from the beginning index and ending just before the ending index. By convention, you may think that the first letter will have index 1, the second letter will have index 2, and so on, but this is not the case. Java is a zero-indexed language, which means that the first character has index 0, the second has index 1, and so on such that the last has index length() - 1. For example in the string String str = "Peter Cao", here are the characters and their indices:
| | | | | 0 | P | | 1 | e | | 2 | t | | 3 | e | | 4 | r | | 5 | | | 6 | C | | 7 | a | | 8 | o |
Although the total length of the string is 9, notice how the last character, "o", is at index 8. To get the first word as a substring with that string, I would do:
str.substring(0,5);
This is because in "Peter", the "P" is at index 0 and the "r" is at index 4, which is before the space, which is index 5.
The substring() method has also been overloaded as well. With the overloaded method, we can return the remainder of the string with this version of the method:
To get the nth character, we can use substring() method as follows: str.substring(n-1, 1) because we have to remember that the nth character has index n-1 and we stop before the next index.
Sometimes, we want to see whether two strings are equal or see which string is alphabetically first. We use two methods to do this:
stringOne.equals(stringTwo)
The method will return true if the two strings are equal and false if the two strings are not. Keep in mind that the equals() method is case-sensitive, meaning that “hi” and “Hi” will return false.
Other times, we want to check to see if a string comes before or after another string alphabetically. This is done using the compareTo() method as follows:
stringOne.compareTo(stringTwo)
Keep in mind that what is returned is relative to stringTwo. If stringOne comes before stringTwo alphabetically, a negative number is returned. If the strings are the same, 0 is returned. Finally, if stringOne comes after stringTwo alphabetically, a positive number is returned. This will be useful to sort multiple strings later.
Worried about remembering all those String methods for the AP CS A exam? Don’t be! You get to use this AP CS A Java Quick Reference Sheet during the exam. These ⬇️ are all the methods we just went over from the reference sheet!
Reminder: The method indexOf returns the first position of the passed str in the current string starting from the left (from 0).
What is the value of pos after the following code executes?
String s6 = "hello world";
int pos = s6.indexOf("l");
A. 2
B. 1
C. 4
D. -1
Answer: A. 2
What is the value of pos after the following code executes?
String s7 = "java programming";
int pos = s7.indexOf("p");
A. 2
B. 1
C. 5
D. -1
Answer: C. 5
What is the value of pos after the following code executes?
String s8 = "computer science";
int pos = s8.indexOf("c");
A. 2
B. 0
C. 4
D. -1
Answer: B. 0
What is the value of pos after the following code executes?b
String s9 = "software development";
int pos = s9.indexOf("f");
A. 2
B. 1
C. 4
D. -1
Answer: A. 2
What is the value of pos after the following code executes?
String s10 = "artificial intelligence";
int pos = s10.indexOf("i");
A. 2
B. 3
C. 4
D. -1
Answer: B. 3
Reminder: Length returns the number of characters in the string.
What is the value of len after the following code executes?
String s2 = "abcdef";
int len = s2.length();
A. 2
B. 3
C. 6
D. -1
Answer: C. 6
What is the value of len after the following code executes?
String s3 = "dog";
int len = s3.length();
A. 2
B. 3
C. 4
D. -1
Answer: B. 3
What is the value of len after the following code executes?
String s4 = "abc";
int len = s4.length();
A. 2
B. 3
C. 4
D. -1
Answer: B. 3
What is the value of len after the following code executes?
String s5 = "school";
int len = s5.length();
A. 2
B. 3
C. 6
D. -1
Answer: C. 6
What is the value of len after the following code executes?
String s6 = "book";
int len = s6.length();
A. 2
B. 3
C. 4
D. -1
Answer: C. 4
Reminder:
String s3 = "cat";
String s2 = s3.substring(1,3);
A. cat
B. c
C. ca
D. at
Answer: D. at
What is the value of s2 after the following code executes?
String s4 = "hello";
String s2 = s4.substring(3);
A. lo
B. o
C. el
D. ell
Answer: A. lo
What is the value of s2 after the following code executes?
String s5 = "abcdefg";
String s2 = s5.substring(2,5);
A. abcdefg
B. cde
C. def
D. c
Answer: B. cde
What is the value of s2 after the following code executes?
String s6 = "dog";
String s2 = s6.substring(1);
A. og
B. g
C. do
D. d
Answer: A. og
What is the value of s2 after the following code executes?
String s7 = "book";
String s2 = s7.substring(2,4);
A. book
B. oo
C. ok
D. bo
Answer: C. ok
Reminder:
For stringOne.compareTo(stringTwo)...
String s3 = "Banana";
String s4 = "Apple";
int answer = s3.compareTo(s4);
A. positive (> 0)
B. 0
C. negative (< 0)
Answer: A. positive (> 0)
What is the value of answer after the following code executes?
String s5 = "Blue";
String s6 = "Blue";
int answer = s5.compareTo(s6);
A. positive (> 0)
B. 0
C. negative (< 0)
Answer: B. 0
What is the value of answer after the following code executes?
String s7 = "Cat";
String s8 = "Dog";
int answer = s7.compareTo(s8);
A. positive (> 0)
B. 0
C. negative (< 0)
Answer: C. negative (< 0)
What is the value of answer after the following code executes?
String s9 = "Hello";
String s10 = "World";
int answer = s9.compareTo(s10);
A. positive (> 0)
B. 0
C. negative (< 0)
Answer: C. negative (< 0)
What is the value of answer after the following code executes?
String s11 = "Sunday";
String s12 = "Monday";
int answer = s11.compareTo(s12);
A. positive (> 0)
B. 0
C. negative (< 0)
Answer: A. positive (> 0)
The compareTo method is used to compare two strings lexicographically. It returns an integer value that indicates the relationship between the two strings.
Term 1 of 9
substring()
method in Java handle string indices differently from human intuition when extracting substrings?The compareTo method is used to compare two strings lexicographically. It returns an integer value that indicates the relationship between the two strings.
Term 1 of 9
The compareTo method is used to compare two strings lexicographically. It returns an integer value that indicates the relationship between the two strings.
Term 1 of 9
substring()
method in Java handle string indices differently from human intuition when extracting substrings?External libraries are pre-written code that can be imported into a program to provide additional functionality. They contain a collection of classes and methods that can be used to perform specific tasks without having to write the code from scratch.
APIs (Application Programming Interfaces): APIs are sets of rules and protocols that allow different software applications to communicate with each other. They define how different components should interact and exchange data.
java.lang package: The java.lang package is a built-in package in Java that provides fundamental classes and basic functionalities such as String manipulation, math operations, and exception handling.
Frameworks: Frameworks are pre-designed structures or templates that provide a foundation for building applications. They often include libraries and tools that help developers streamline the development process by providing ready-made solutions for common tasks.
The String class is a built-in class in Java that represents a sequence of characters. It provides various methods to manipulate and work with strings.
substring: A substring is a smaller part of a larger string. It refers to extracting a portion of characters from a string.
length: The length is the number of characters in a string. It helps determine the size or amount of content within the string.
concatenation: Concatenation is combining two or more strings together to create one longer string.
A substring refers to extracting a smaller part of a larger string by specifying its starting and ending positions.
index: An index represents the position or location of each character within a string. It helps identify where specific characters are located.
charAt(): The charAt() method returns the character at a specified index within a given string.
indexOf(): The indexOf() method returns the first occurrence's index position of a specified character or substring within another string.
In computer programming, zero-indexed language refers to systems where counting starts from 0 instead of 1 when accessing elements in an ordered collection (e.g., arrays).
Array: An array is an ordered collection of elements stored in contiguous memory locations. Each element can be accessed using its index.
Index out of bounds: This term refers to trying to access an element outside the valid range of indices for a given data structure, resulting in an error.
Loop: A loop is a programming construct that allows executing a set of instructions repeatedly until a specific condition is met. It often involves iterating over elements in an ordered collection using their indices.
The compareTo method is used to compare two strings lexicographically. It returns an integer value that indicates the relationship between the two strings.
equals(Object obj): This method checks if two objects are equal by comparing their values.
length(): This method returns the length of a string, which is the number of characters it contains.
substring(int beginIndex, int endIndex): This method extracts a portion of a string based on specified start and end indexes.
The length() method is a built-in function in Java that returns the number of characters in a string.
substring(int beginIndex, int endIndex): This method extracts a portion of a string based on the specified beginning and ending indexes.
equals(String other): This method compares two strings and returns true if they are equal, and false otherwise.
charAt(int index): This method returns the character at the specified index position in a string.
The indexOf(String str) method is used to find the index position of the first occurrence of a specified substring within a larger string.
lastIndexOf(String str): This method finds the index position of the last occurrence of a specified substring within a larger string.
contains(CharSequence sequence): This method checks if a sequence of characters exists within another string and returns true or false accordingly.
replace(CharSequence target, CharSequence replacement): This method replaces all occurrences of one sequence with another sequence within a given string.