Browse By Unit
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:
What is the value of s2 after the following code executes?
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)...
What is the value of answer after the following code executes?
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)
<< Hide Menu
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:
What is the value of s2 after the following code executes?
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)...
What is the value of answer after the following code executes?
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)
© 2024 Fiveable Inc. All rights reserved.